Why can an if be redundant?

Asked

Viewed 870 times

11

Using a method that returns a boolean the system would determine whether a number is positive or negative. So I did it this way:

    public boolean isPositive(float num) {
    boolean positive;
    if (num >= 0) {

    positive = true;
        } else {

    positive = false;

    }return positive;

Then Netbeans declared that "if statement is redundant" and fixed it to:

    public boolean isPositive(float num) {
    boolean positive;
    positive = num >= 0;
    return positive;

and it worked.

How did you not use if I was perplexed to know q worked, since it is not evident in which situation he would return a FALSE. My question is: how? The logic of the second method declaration?

NOTE: The two statements work.

4 answers

18


The if requires a certain Boolean condition?

then when you pass an expression (num >= 0), this will return a boolean.

Forget the if, what is the function of your Method?

Return true if it’s greater than zero, right?

Then you pass this information to the variable and return it.

Another way to implement this method would be:

public boolean isPositive(float num) {
    return num >= 0;
}

11

Actually Netbeans was as smart as it could be. It should be like this:

return num >= 0;

Now, what is the result of the expression num >= 0? It is a boolean, right? That is, the result of this "account" will be or false, or will be true. Either it is "greater than or equal to zero", or it is not, only this can occur.

So why not use this direct result? If what you need is to define a true when this expression results in true and you need to false when the expression results in false, then use the result of the expression and simplify the logic.

I understand that a lot of programmers think that the if is a magical thing, and that you have a condition inside. In fact the if expects a boolean expression, a conditional expression. It can be a literal, one variable simple (with boolean type value), or a complex expression whose outworking be a boolean.

All this is valid (even if the former makes no sense except for testing):

//literal
if (true)

//variável
variavel = outraVariavel > 0 && maisUmaVariavel.equals("teste");
if (variavel)

//expressão
if (outraVariavel > 0 && maisUmaVariavel.equals("teste"))

The if serves for flow control, it decides only whether it should run a block or another, if available. The decision is made by any boolean value, where it comes from, it doesn’t matter. The code of this method does not need a flow control, it just needs a boolean result.

The same goes for the while and for, are only flow controls whose decision will deviate to another line or continue in the following is determined by a boolean value that can be obtained by one of the three forms cited above.

Do so by simplifying the code and getting the same result:

public boolean isPositive(float num) {
    return num >= 0;
}

I put in the Github for future reference.

Further reading.

9

You could say that Netbeans is a very boring IDE because it doesn’t let you program and does it all yourself. kkkk

The thing is, some Ides are smart and do their best to reduce code or fix possible bugs. In your case the IDE wanted to delete the extra amount of code, in the case of the condition, then it saved the result of the condition into a variable and returned.

public boolean isPositive(float num) {
    boolean positive;
    positive = num >= 0; // (num >= 0) returna true ou false
    return positive;
}

as it will not be calling commands within the conditions, there is no need to have them, and it is possible to further reduce the code.

public boolean isPositive(float num) {
    return (num>=0); // retorna diretamente true ou false
}

The less code on your system, the faster it will go through the methods, when the software does many checks, it has a sequence needed to perform the condition, so it is recommended to do everything to have fewer lines of code written.

9

num >= 0; is an expression that can be evaluated with return of true or false It is possible to simplify a little more by playing the expression directly in Return, return num >= 0;

Browser other questions tagged

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