Are there logically any difference between
if (name.startsWith("a"){
return true;
} else if (name.startsWith("b") {
return true;
} else if (name.startsWith("c") {
return true;
}
and
if(name.startsWith("a") || name.startsWith("b") || name.startsWith("c") ){
return true;
}
I prefer the second one as it is elegant to me. I'd like to understand "are there any differences?"
They're the same.
The second one is definitely easier to read, and readability is incredibly important in programming.
The rule I like to go by is that if multiple branches of an if-else statement produce the same behavior, I combine them. (Be sure that they're the EXACT same behavior)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With