News for hackers. Stuff that matters.
Re:“Clean” Code, Horrible Performance by yakubin on Tuesday February 28, 2023 @06:34PM (#34976486)
Even in C compiler will emit warnings for unhandled cases in switch statements as long as you don’t provide a default case (as you shouldn’t).
Re:“Clean” Code, Horrible Performance by e28eta on Tuesday February 28, 2023 @07:36PM (#34977124)
Depends on the type of code you’re writing. If your `switch` is tightly coupled to the code that defines the cases and they’ll definitely be changed in lockstep, a default is more likely to be harmful.

If your cases are defined externally, and you need to be forwards compatible, omitting a default is wrong.

The Swift language specifically added `@unknown default` for switching over enums.

Re:“Clean” Code, Horrible Performance by Aeolun on Tuesday February 28, 2023 @07:48PM (#34977225)
Depends on your language I suppose. I haven’t worked with a ton of compiled languages.

But we can just re-up the problem by adding 100 different shapes instead of the one. Now you have switch statements with 104 cases each spread through your codebase.

Re:“Clean” Code, Horrible Performance by yakubin on Tuesday February 28, 2023 @08:17PM (#34977478)
I prefer to have those 104 cases all in one place (as is the case, when it is a switch statement) rather than each in a separate file, that I need to jump around between now (as is the case with polymorphism). This situation is a bit analogous to organising things column-wise vs row-wise. And in practice I find that I need to jump around a lot less with code that uses switches than with code that uses polymorphism. Tangentially, the latter is also more prone to turning into spaghetti, as the whole is obscured by indirection levels between the parts, but you don't see the spaghetti until you try to step through the code, when debugging an issue or just trying to familiarise yourself with a new codebase.