Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it harmful to explicitly state ranges in if-then-else statements? [closed]

(I searched for "else if range" but did not come up with any posts that answered my question).

When using if .. elseif .. else to select what to do based on a variable being in certain (mutually exclusive) ranges, I like to specify the ranges explicitly:

int num = 55;
String message;

if (num >= 20) {
    // update message variable
    message = "Bigger or equal to than 20";
}
else if (10 <= num && num < 20) {
    // update message variable
}
else if (0 <= num && num < 10) {
   // update message variable
}
else if (num < 0) {
   // update message variable
}

System.out.println(message);

But all the textbooks and lecture notes I see write such an example like this:

int num = 55;
String message;

if (num >= 20) {
    // update message variable
    message = "Bigger or equal to than 20";
}
else if (num >= 10) {
    // update message variable
}
else if (num >= 0) {
   // update message variable
}
else {
   // update message variable
}

System.out.println(message);

I understand why they make sure to use an else at the end (i.e. to prevent Java compiler from thinking that a variable like message might not get initialized, if that variable was a primitive), but given that all textbooks and lecture notes show the other style, are there any issues caused by explicitly writing out the ranges for all the other conditions, as I like to do?

like image 803
silph Avatar asked Oct 21 '16 18:10

silph


2 Answers

Don't repeat yourself.

With your approach, you're repeating the same value twice in the code, unnecessarily. This has several disadvantages, including:

  • It makes it easier to introduce errors during maintenance, by changing one of the values, but not another.
  • During execution it performs a redundant check. If you're in the else if, you already know the prior condition was false.
  • The code is longer without adding any additional information. It takes longer to read, and longer to inspect.

Later maintenance may change one of the values, but not the other, leading to some surprising behavior.

if ( num >= 30 ) {
    // update message variable
}
else if (10 <= num && num < 20 ) { // Whoops! 
    // update message variable
}
...

You could avoid this by defining a constant for each value. But then your code is still specifying a redundant check.

final int HIGH = 20;
final int MEDIUM = 10;
final int LOW = 0;

if (num >= HIGH ) {
   // update message variable
}
else if (MEDIUM <= num && num < HIGH ) { // We already know num<HIGH
    // update message variable
}
...
like image 134
Andy Thomas Avatar answered Nov 15 '22 18:11

Andy Thomas


If I'm building some code that will run on some kind of vehicle to be landed successfully on another planet then I'd always plump for clarity. Remember that a line of code will be written once but could be read hundreds of times. Your first snippet will be far easier to understand during a 4am debugging session.

Your first way is indeed much clearer and bugs will not be introduced if an errant refactorer reorders the conditional checks.

I'd always finish with an } else { though, ideally with some kind of assertion if program control ought not reach that.

like image 2
Bathsheba Avatar answered Nov 15 '22 19:11

Bathsheba