Should I use two IF’s or an operator?

Asked

Viewed 121 times

8

Best practice for checking two conditions?

In the example I’m going to give, are not very long checks but what is the best practice and what is the difference? (It happened to me that I found myself having a if huge so I decided to sector and divide into several ifs. This is correct?

Example:

if ( A > X AND  A > Y)
    instrucao aqui....
ENDIF

Or:

if ( A > X )
    if ( A > Y)
         instrucao aqui....
    ENDIF
ENDIF

4 answers

3

The functioning of the two are different, but both should work.

if ( A > X AND  A > Y)
  instrucao aqui....
ENDIF

In this case depending on the language it will optimize the check by consuming less clocks cycles.

if ( A > X )
   if ( A > Y)
     instrucao aqui....
   ENDIF
ENDIF

In this other case he will check the first to enter the if and check the second. If it is a compiled language the syntactic tree will be more complex spending more clock cycles.

Of course it all depends on the language and how it works.

In general the two are equivalent to the logic level, but not to the implementation level.

3


In this specific example and only in it (if it has another form it may not hold) it is usually more advantageous to do the first, for a simple reason: it is simpler.

Some may even question whether it is actually simpler. I consider simplicity, within the knowledge that every programmer should have, and the use of AND is necessary. If the person does not understand it, he may even find the second form simpler, but there is by ignorance.

There are controversies, but for me, simpler is less lines. The code becomes more readable when you write less. Of course, it doesn’t mean that piling up is more readable, but putting fewer expendable lines can become more readable. I’ve seen people say that if you separate things more it becomes more readable, but if that were true it would program in high-level language almost as if it were assembly where each line is a single instruction.

If you have many relational operators in the condition you can separate into several lines, but keep a single command if, it will be even more interesting.

But it actually ends up being like, again, in this particular case. The most important thing is to be consistent, because it’s horrible to see a code that changes the way you write, like this code.

  • thanks. in that case even the conditions/checks were small but I came across very large conditions/checks hence the doubt.

2

The two checks are valid, but if there is a need to divide an if into two the best is to abstract in a function.

If the verification is simple:

if( A< X || B < X )
    instrução....
endif

But if it is a complex validation:

if(valido(parametros))
    instrução...
endif

function valido(parametros)
    validação complexa...
endfunction

This helps a lot in maintaining the code, as a developer doesn’t have to read all the validation whenever they change something in this instruction. only know that there is a validation, and if it is necessary to consult the validation is only read the function responsible for it.

1

In general, I believe it is easier to read a single if instead of one or more of them.

Here are some scenarios to compare the two options.

Many conditions

Thinking of cases with more ifYou’ll see that this:

if ( A > X AND  A > Y AND A > Z)
    instrucao aqui....
ENDIF

It ends up being simpler to understand that:

if ( A > X )
    if ( A > Y)
        if ( A > Z)
            instrucao aqui....
        ENDIF
    ENDIF
ENDIF

Extract method

Using a single if, you can simplify the meaning of the conditions by creating a single method, which is not possible with a if within another if.

As an example:

if ( A > X AND  A > Y)
    instrucao aqui....
ENDIF

You could have a method isAMaiorQueYeX(A, X, Y), giving an explanatory name to the condition:

if ( isAMaiorQueYeX(A, X, Y))
    instrucao aqui....
ENDIF

For more complex condition cases, this type of artifice becomes increasingly useful.

Code debugging and errors

A small advantage that exists using a single if is in troubleshooting. If there is a "Nullpointer" error or something like that, the line shown in the error will straightforward in the problematic condition. Now, using multiple conditions on the same line, you will not be sure which condition caused the problem and will have to debug the application to understand.

And speaking of debugging, it’s also facilitated with several ifs. As debugging is done line by line, it is a little easier to observe what is happening in the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.