Hacker News new | ask | show | jobs
by tabtab 2312 days ago
The C-style switch/case construct is obsolete and awkward, and cleaner alternatives exist, such as VB.net's technique. There is no need for "Break".

The following could be added to C-style languages without overlapping with the existing syntax:

     select(a) {
        when 1,2,3 {...}
        when 4 {...}
        when 5,6 {...}
        ...
        otherwise {...}
     }
C# recently added a pattern-matching alternative, but it's still a bit verbose for most needs.
1 comments

GCC has had case ranges as an extension for a long time now: https://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html

It’s a far cry from general pattern matching (a la Rust, Scheme) but it’s helpful in some circumstances. Otherwise, you can also stick multiple case labels together, i.e.

    switch(x) {
        case 1 ... 4:
        case 7:
        case 9 ... 16:
            ...
        default:
            ...
    }
I don’t see why you need to call the C-style construct obsolete and awkward, though. It works plenty well for enumerations, which is basically what it was designed for. If you need to do something more fancy, if/else if chains always work.
Ranges are nice, but not a replacement for sets. (Allow both!) The main "fix" I propose is to get rid of the Break statement and the very need for it. If you have set-based lists, you don't need "fall through" behavior because you can have more than one item per matching expression.

The Break statement is error prone because it's easy to forgot. Some languages "solved" that by making Break required; but if it's required, then it's superfluous: code-bloating eye clutter. If you never used a language that didn't need "Break" you may not understand how annoying it is to go back.

My suggestion above allows C-ish languages to add a modernized version and yet keep the existing construct for backward compatibility. In other words, the old one would be "deprecated".

> If you have set-based lists, you don't need "fall through" behavior because you can have more than one item per matching expression.

This is not entirely true. Ranges and sets allow you to run the same code for different inputs. Fallthrough allows you to run partially the same code for different inputs.

It's a rare case, so I don't think its utility warrants making fallthrough behavior the default. But it's nice to have it explicitly, like "goto case" in C#.

Anyway, the reason why it is the way it is in C, is because case labels are literally labels, and the switch statement itself is just a branched goto - which is why e.g. Duff's Device is a thing. And that, in turn, is probably because it's a descendant of "switch" in Algol-60, which was basically an array of labels.

> Fallthrough allows you to run partially the same code for different inputs.

True, but it's a screwy way to manage such overlaps. The volume of errors and confusion exceeds the benefits in my opinion. There are other ways to do such. For non-trivial conditionals (such as overlaps), if-else is often the better tool anyhow. Case/Switch should only be used for simple mutually-exclusive value look-ups.