In GoLang you can have a switch without an expression, where the cases replace a series of cascading ifs.
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("It's before noon")
default:
fmt.Println("It's after noon")
}
In Java 19 I can simulate this with this kind of hack by using switch pattern matching with "guarded pattern when
" and when the main expression is always true and the guarded pattern does the real work.
( Or in Java 17 but using &&
instead of when
)
int num=5;
switch (System.out) {
case Object o when num>100 -> System.out.println("Big");
case Object o when num>50 -> System.out.println("Med");
default -> {System.out.println("Small");}
}
Obviously these simple cases can be written without a switch, but when you have a long series of if-else-if=else, not necessarily on the same variable, this kind of structure can be more concise.
Anyone know a nicer way to do this?
There a few principles that I can think of that can guide decisions for situations like this:
The switch statement is used for executing different code paths based on possible values of a particular variable. The
when
extension was invented to add some flexibility and terseness to this mechanism, but the original purpose of the mechanism is still there: to switch on possible values of a particular variable.The very concept of a "switch" is based on the idea of more than one outcome which is controlled by something; in the case of the
switch
statement, that something is the switch expression. If there is no switch expression, then it is not a switch, it is a collection of mutually exclusive rules.Another idea behind the invention of the
switch
statement was the possibility of optimization. The original implementations ofswitch
statements used highly efficient lookup tables, that's why they could only operate onint
. Modern switch statements work on more complex values, but when there is a single controlling switch expression there is still some possibility of optimization. No such thing happens when there is no switch expression.When I see a switch statement, I expect to see various different things done based on different values of a particular variable; I do not expect to see just about anything being done in there. If I study the code and I notice that my expectation is not met, I will be mighty surprised. Disappointed even.
So, the nicer way of doing this is to go back to the good old, familiar, tried-and-true, cascaded
if-else
statement.The fact that golang allows the creation of meaningless
switch
statements means absolutely nothing to me. Many languages make useless choices. Many languages do not even have any particular reason of existence. Many people consider the "idiomatic" way of doing things in a particular language to be a goal in and of itself, while it is not. If you feel tempted to take an idiomatic feature of your favorite language and apply it in other languages, first ask yourself whether the feature had any merit even in the original language. Often, it does not.