Should I use two IF's or an operator?

7

What is the best practice for checking two conditions?

In the example I am going to give, these 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 sectorize and divide into several if s.Is that correct?

Example:

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

Or:

if ( A > X )
    if ( A > Y)
         instrucao aqui....
    ENDIF
ENDIF
    
asked by anonymous 29.08.2018 / 12:33

4 answers

3

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

Some may even wonder if it's 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 can even find the second form simpler, but there is unknown.

There are controversies, but for me simpler is fewer lines. The code becomes more readable when you type less. Of course, it does not mean that heap is more readable, but putting fewer dispensable lines can become more readable. I've seen people say that separating things more 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 statement.

If you have many relational operators in the condition you can separate into multiple rows, but keeping a single command if , will still be more interesting.

But in fact it ends up being like, again, in this specific case. The most important thing is to be consistent, because it is horrible to see a code that changes the way you write, as it did in this code.

    
29.08.2018 / 14:45
1

The operation of the two is 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 scan by consuming fewer clock cycles.

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

In this other case it will check the first enter inside the if and verify the second. If it is a compiled language the syntax tree will be more complex by spending more clock cycles.

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

In general, the two are equivalent to the level of logic, but not at the execution level.

    
29.08.2018 / 13:37
1

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

If the check is simple:

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

But if it's a complex validation:

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

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

This helps a lot in maintaining the code, since a developer does not need to read all the validation whenever something changes in that statement. only to know that there exists a validation, and if it is necessary to consult the validation, just read the function responsible for it.

    
29.08.2018 / 14:54
0

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

Here are some scenarios for comparing the two options.

Many conditions

Thinking about cases with if s, you'll see that this:

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

It's 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 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 isAMaiorQueYeX(A, X, Y) method, giving an explanatory name for the condition:

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

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

Debugging code and errors

A small advantage that exists using a single if is debugging problems. If a "NullPointer" error or something similar occurs, the line pointed to in the error will direct in the problematic condition. Now, using multiple conditions on the same line, you're not sure what condition caused the problem, and you'll have to debug the application to understand.

And speaking of debugging, it is also made easy with many if s. Because debugging is done line by line, it's a little easier to see what's happening in the code.

    
06.09.2018 / 18:20