lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


"Intuitive" is not enough: the order of evaluation of conditions matters (the fact they are not mutually exclusive and create complex conditions that are split and spread across large spans of code, can be challenging for refactoring. For this reason if the switch branches are complex (and cannot just call a single function or perform a single assignment but contain structured statements or many assignments and calls, it's safer for refatoring to first evaluate the conditions and use goto's with clear labels, then develop each label.

There are caveats however due to local variable scopes when they are declared or initialized in specific subbranches. There's no general "good" solution that matches all needs.

For finite state automatas, it's generally better to use many gotos and group the code for testing all conditions tests together before the code for all conditional branches. Switch'es are good by the fact that the conditions they test is simple and don't require declaring unique labels: they are then more compact, and easier to maintain. I do like switch'es.

I don't care at all if they are finally compiled as computed gotos (indirection via an array of label pointers), or series of if-tests and many jumps (the same also applies to if/elseif/else/end blocks that can compile using all the same options):

This is a compiler implementation detail anyway. For programmers in alny language, it has to be concise, clean, easy to read and manage for refactoring. Lua can perfectly be built for the programmer's ease of understanding, any compiler will be able to find all the possible tricks itself.


Le lun. 23 déc. 2019 à 22:00, Soni "They/Them" L. <fakedme@gmail.com> a écrit :


On 2019-12-23 4:42 p.m., Egor Skriptunoff wrote:
> On Mon, Dec 23, 2019 at 5:30 PM Soni "They/Them" L. wrote:
>
>     if op == OP_TAILCALL then
>        ...
>        -- fallthrough
>     orif op == OP_CALL then
>        ...
>     end
>
>     because it looks nicer this way I guess.
>
>
>
> The meaning of "orif" keyword is not intuitive-clear to a reader.
> I would prefer to use explicit "goto" in case I need to implement
> fallthrough.


it's definitely more intuitive than switch-case fallthrough by a long shot.