4 ms·
> What problem is this code trying to solve? I think you missed the point of this hack - the problem he's trying to solve is described in the article: > The c
by zik 10y ago
> What problem is this code trying to solve?
I think you missed the point of this hack - the problem he's trying to solve is described in the article:
> The common issue associated with switch statement is typically maintenance; especially where the set of ‘valid’ values needs extending.
He wants the efficiency of the switch statement for all the cases he knows about and a fallback to the less efficient if clauses if that fails due to new cases being added. It's a pretty silly example but let's face it - it's more for fun than elegance.
- jmts 10y agoI see your point, but I'd argue that even then it's still the wrong the problem. 1. If you don't enjoy maintaining your switch statement, it's probably huge anyway and you need to refactor. 2. If you don't enjoy maintaining this switch statement, you probably also don't enjoy maintaining the switch statement inside next function that gets called that gives you your full set of valid values. 3. The number of types of messages used is probably much fewer than unique messages, so you are optimising on a small N, which will give minimal returns. 4. You never need to look at this code again and it will just sit there adding overhead. Other switch statements will be maintained because they're the first place you'll notice missing entries. Fun, sure. Like setting a fistful of matches on fire. It takes a bit of effort to set up, and it looks interesting. I'll agree to that.