Much of the stories and code we see here are objectively bad. Many of them are bad when placed into the proper context. Sometimes we're just opinionated. And sometimes, there's just something weird that treads the line between being an abuse of code, and almost being smart. Almost.
Nic was debugging some Curses-based code, and found this "solution" to handling arrow key inputs:
switch (val) {
case ERR:
// [snip]
default:
switch (val) {
case 65:
// [snip]
case 66:
// [snip]
case 67:
// [snip]
case 68:
// [snip]
}
}
The obvious badness here is the nested switch. Clearly, this isn't necessary. Putting the ERR
check at the top makes sense. But burying a second switch under the default
clause is pointless and weird. Given that this is C, and switches have weird power, I'm sure there's probably some bad side effect of doing this. Even if there isn't, it isn't needed.
But. It's almost smart. There's one thing that leaps out to me in this code: that there are two distinct paths, one for error-handling, and one for processing input. It's a weird way to accomplish that goal, mind you, but it does accomplish that goal. I'd still never do it, because almost smart is way worse than actually smart (and also way worse than really stupid). But I guess I can sympathize with the developer.