onehundredfeet / hmecs

Super lightweight Entity Component System framework for Haxe
MIT License
14 stars 2 forks source link

Use pattern matching to simplify `switch` blocks. #4

Open player-03 opened 2 years ago

player-03 commented 2 years ago

(Plus various other cleanup.)

I noticed a lot of nested switch blocks that really don't need to be that way. I didn't test this (I don't care to install dependencies like tink_macro), but it should all be equivalent.

onehundredfeet commented 1 year ago

Sorry for taking FOREVER to look at the PR. I'm so unused to people posting them that I rarely look.

Regarding the nested switches, is there a reason you prefer combining the switches at a unified level? I like the use of pattern matching, but in some cases it makes some duplication. I'm not sure which is clearer TBH.

switch (field) {
            case { kind: FFun(_), name: '__update__' }:
                Context.error('Do not override the `__update__` function! Use `@:update` meta instead! More info at README example', field.pos);
            case { kind: FFun(_), name: '__activate__' }:
                Context.error('Do not override the `__activate__` function! `onactivate` can be overridden instead!', field.pos);
            case { kind: FFun(_), name: '__deactivate__' }:
                Context.error('Do not override the `__deactivate__` function! `ondeactivate` can be overridden instead!', field.pos);
            default:
        }

End up duplicating the FFun each time.

player-03 commented 1 year ago

I don't feel like it's that much duplication, but in that specific case there's an easier option. Just disallow all fields named one of those things, function or otherwise. I know this doesn't work for EConst, but I don't think those are that bad either.

As I see it, there are two main benefits here. First, you don't have to scroll as far, either vertically or horizontally. Second, you can see the whole object in one place, with variables and values side by side.

ETA: Many programmers consider deep nesting an anti-pattern.