Is it harmful to explicitly specify ranges in if-then-else statements?

(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 that is in certain (mutually exclusive) ranges, I would like to explicitly specify the ranges:

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 lectures that I see write this example:

 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 should use else at the end (i.e. do not let the Java compiler think that a variable like a message cannot be initialized if this variable was primitive), but given that all the tutorials and lectures show a different style, there is Are there any problems caused by explicit spelling of ranges for all other conditions as I like?

+5
source share
2 answers

Do not repeat yourself.

With your approach, you repeat the same value twice in the code, without need. This has several disadvantages, including:

  • This makes it easier to enter errors during maintenance by changing one of the values, but not the other.
  • At runtime, it performs redundant validation. If you are in else if , you already know that the previous condition was false.
  • The code is longer without adding additional information. It takes more time to read and longer to check.

Later maintenance may change one of the values, but not the other, resulting in unexpected behavior.

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

You can avoid this by specifying a constant for each value. But then your code still indicates redundant validation.

 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 } ... 
+7
source

If I create some kind of code that will run on some vehicle in order to successfully land on another planet, then I would always be chubby for clarity. Remember that a line of code will be written once, but can be read hundreds of times. Your first snippet will be much easier to understand during a 4am debugging session.

Your first way is really much clearer and no errors will be introduced if the erroneous refactor changes conditional checks.

I would always end with } else { , although ideally with some kind of statement, if control of the program should not go so far.

+2
source

Source: https://habr.com/ru/post/1258618/


All Articles