3 ms·
Yes, as a positive, I see that switch(true) will cosmetically align the tests of 'possible value 1' and 'possible value 2'. But the negative is that you've add
by jasode 7y ago
Yes, as a positive, I see that switch(true) will cosmetically align the tests of 'possible value 1' and 'possible value 2'.
But the negative is that you've added the requirement to always remember to include "break;" after each block. The code is prettier but also more fragile. This type of vigilant programmer discipline required to keep the code error-free (especially across multiple programmers) is not easy. Missing "break" statements causing unintended fallthrough are common bugs discussed in many "traps & pitfalls" books/blogs.
The "else if" while not as vertically aligned, includes the invisible "break;" for free.
(Side note that some languages like C#'s and Rust's switch statement don't have fallthrough behavior -- but this article uses PHP which does.)