Why an if can be redundant?

6

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 corrected for:

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

and it worked.

As you did not use if I was perplexed to know that it worked, since it is not clear in what situation it would return a FALSE. My question is: how? What is the logic of the second method declaration?

NOTE: Both statements work.

    
asked by anonymous 19.04.2016 / 21:02

4 answers

14

The if requests a boolean condition right?

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

Forget if , what is the function of your Method?

Return true if 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;
}
    
19.04.2016 / 21:22
7

You can say that Netbeans is a very boring IDE, because it does not let you program and does everything yourself. kkkk

It's the following, some IDEs, are smart and do their best to reduce the code or fix possible bugs . In your case IDE wanted to eliminate the extra amount of code, in this case the condition, so it saved the result of the condition in a variable and returned.

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

As you will not be calling commands within conditions, there is no need to have them, and you can 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 a lot of checks, it has a necessary sequence to perform the condition, so it is recommended to do everything to have fewer lines of written code. p>     

19.04.2016 / 21:17
7

In fact NetBeans was as smart as it could be. It should look like this:

return num >= 0;

Now, what is the result of the expression num >= 0 ? He's a Boolian, right? That is, the result of this "account" will be false , or true . It is either "greater than or equal to zero," or it is not. This can only happen.

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

One thing I realize is that it has a lot of programmers thinking that if is a magic thing, and that you have a condition in there. In fact if expects a Boolean expression, a conditional expression. It can be a literal , a simple variable , or a complex expression whose result is Boolean.

All this is valid (even if the first one does not make 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 is for flow control, it only decides if it should execute one block or another, if available. The decision is given by any Boolean value, whence it comes, it does not matter. The code of this method does not need a flow control, it just needs a Boolean result.

The same goes for while and for . It is only flow control whose decision is to deviate to another line or continue in the next is determined by a Boolean value that can be obtained by one of the three forms mentioned above.

Do as you will be simplifying the code and getting the same result:

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

Further reading .

    
19.04.2016 / 21:17
5

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

    
19.04.2016 / 21:19