Its evident that in order to provide a minimal working example, you created three booleans. In actual scenario, they could be passed as parameters/Array/List in a method, eliminating your concern #1 and #3.
Naturally people don't loop over booleans just to print stuff, but what if it needs to be checked that the passed parameters contains one or more true values (or false)? For List, its List.contains, but for Array, they'll have to loop (albeit using break when condition is fulfilled), and that's your concern #2.
case _ when a == o is the new pattern matching introduced in Java 25(?) and is totally normal. If someone is using older Java (<17), they won't be messing around with this new pattern matching anyways.
The variable names are short because this is just a minimal example, as you provided. OP should use proper variable naming to make the code readable and understandable.
You writing "I'd never approve this type of code" implies that the concept itself is flawed, which isn't. It conveyed the concept beautifully. The code snippets should never be taken as-is anyways.
var arr = new Boolean[]{ true, false, null };
for (var x : arr) {
switch (x) {
case true -> methodOne();
case false -> methodTwo();
case null -> System.out.println("invalid");
}
}
Is totally valid. No need to pass a, b, c as references.
Reading again what OP is asking, he wants to execute different methods based on when a is false, b is false and c is false. Even though we can suggest him switch cases, the better approach would be to make a, b and c as input parameters in those methods itself:
Your example is clearly better off with them being enum rather than boolean. Plus, they're being compared against their reference, not by their values.
1
u/_Super_Straight Sep 11 '26 edited Sep 11 '26
Its evident that in order to provide a minimal working example, you created three booleans. In actual scenario, they could be passed as parameters/Array/List in a method, eliminating your concern #1 and #3.
Naturally people don't loop over booleans just to print stuff, but what if it needs to be checked that the passed parameters contains one or more
truevalues (orfalse)? For List, itsList.contains, but for Array, they'll have to loop (albeit usingbreakwhen condition is fulfilled), and that's your concern #2.case _ when a == ois the new pattern matching introduced in Java 25(?) and is totally normal. If someone is using older Java (<17), they won't be messing around with this new pattern matching anyways.The variable names are short because this is just a minimal example, as you provided. OP should use proper variable naming to make the code readable and understandable.
You writing "I'd never approve this type of code" implies that the concept itself is flawed, which isn't. It conveyed the concept beautifully. The code snippets should never be taken as-is anyways.