[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Switch/Case statements revisited
- From: Fabien <fleutot+lua@...>
- Date: Wed, 7 Nov 2007 16:37:47 +0100
On Nov 7, 2007 3:05 PM, Jeremy Darling <firstname.lastname@example.org> wrote:
I like the fall through abilities of C and wish that they existed in pascal without having to
hack them into place.
I think most C developers will agree that unwanted fall-through are a dangerous cause of bugs, so if it were really required it should at least require a dedicated keyword, so that it doesn't happen by accident.
In my experience, it doesn't happen very often, and when there are shared parts between cases, there is no case ordering in the switch that satisfy all sharing constraints simultaneously.
I think the optimal refactoring for this is to put the shared parts in local functions, and to simply put a series of calls in the switch. That's the most readable solution, as it makes sharing and ordering visible and maintainable.
And before a speed fetishist starts arguing about closure cost: this is only for Lua (your time critical stuff ought to be in C), this is only for very special and contrived cases (which often aren't in the critical optimization path), and you can always switch back to a lower level representation if it really is critical wrt optimization. I wish every coder arguing about speed without attaching a bunch profiling logs were shot dead :)