0
votes

I actually don't know how to phrase this question properly, apologize in advance.

In my code review, people suggested to me that if and else conditions should be comparable by nature, like the following:

if (a == 0)..
else if (a == 1)
else if (a != 3)
else...

basically, these are evaluating within the same business scope - var a in this case.

A extreme counter example would be:

if (a == 0)
else if (window.size != server.config.size)
else if (user.b == this.user)
else...

Some thing in the grey area:

if (a == 0)
else if (b == c)

vs.

if (a == 0)
else { // implying a != 0
    if (b == c)
}

I have not found any programming practice/guide in something this basic. Please let me know there is any reference to how people organize their if else.

2
Your code reviewers were trying to establish a coding guideline to help people understand the code. A block of if-else statements that reference the same variable is easy for a person to understand. Your second (made up) example would be impossible for a person to understand without context, The best general programming guideline is this: Write code to make it easy for people, including yourself six months from now, to understand the code and the context of the code. - Gilbert Le Blanc
Did you find any answer useful than at least vote or feedback on the answer. - Pranav P

2 Answers

0
votes

Sometimes it's really hard to find what is a proper way to write any logic. Especially, there is more than one way to achieve same things. I think, it is the same situation for you.

I am also facing these situations in my day to day coding experience, during code review as well. You are right here, that you want to know about programming practice, Even I try to find proper programming practice or guide in these situations but we/you should first understand our requirements very properly to decide what approach we should follow.

I am not sure what "comparable by nature" means but your colleagues try to keep separate if/else statement as per variables or conditions and for different variables/conditions, they use different if/else statement.

Means, you should break your if/else into two different if/else for different variables/conditions. You can break very properly by understanding your requirements.

But you can think more about it and ask yourself as much as possible why and you will find a proper answer about it.

Let me know if it is helpful for you or not. Happy coding...!

-1
votes

Bracketing/ inserting braces in appropriate positions is instrumental in executing correct codes. One needs to keep this in mind while writing codes.

Talking about your case of 'if/else if', the case you mentioned where:

if(a==0)
else if(a==1)
else if(a!=3)

The above code will work as long as there are only ONE statement of execution after the cases. In the sense, if your code looks like:

if(a==0)
    System.out.print("CASE 1");
else if(a==1)
    System.out.print("CASE 2");
else if(a!=3)
    System.out.print("CASE 3");

The above code would work normally, but in case you have multiple statements, you would require brackets, like:

if(a==0)
    {
      System.out.print("CASE 1");
      System.out.print("The value of a equals to 0.");
    }
else if(a==1)
    {
      System.out.print("CASE 2");
      System.out.print("The value of a equals to 1.");
    }
else if(a!=3)
    {
      System.out.print("CASE 3");
      System.out.print("The value of a does NOT equal to 3.");
    }