Why does Netbeans suggest improving the for?

Asked

Viewed 189 times

5

When I was doing this for Netbeans Showcased That Lamp of Tips to Improve Code on the Line of for. The suggestion was to reverse the condition. Follow the code:

Before of improvement:

for(int j = 0; j < selectionPaths.length; j++) {
   if( isDescendant(selectionPaths[j], path) )
      return true;
}

Afterward improvement suggested by Netbeans:

for(int j = 0; selectionPaths.length >= j; j++) {
   if( isDescendant(selectionPaths[j], path) )
      return true;
}

Update: after the "improvement" the program launches the exception ArrayIndexOutOfBoundsException.

But then why did Netbeans give this suggestion if after accepting it the program gives error? Is there a specific reason or just a matter of code convention? Or is it a Netbeans failure?

Note: The Netbeans version is 8.0.1. The suggestion appears when the cursor is positioned in the condition, more precisely in the variable selectionPaths.

  • 1

    I use Eclipse, I haven’t used Netbeans for centuries, but I’m curious to know what happened in your case.

  • In fact, in this case, the correct suggestion would be selectionPaths.length > j and not selectionPaths.length >= j.

  • 7

    Because it is a software that does not look at the context, for this there is the programmer.

  • Yeah. Maybe that’s why it’s there as a 'hint' and not refactoring. Refactoring is vaguely improving the code without changing its behavior.

  • @Earendul I tried to reproduce the same scenario and the "correction/suggestion" resulted in another result (other than yours). He put one up for improvement. What’s your version again?

  • @Cold Yes, for me also he gave this suggestion. Try to put the cursor in the condition and see if it gives another suggestion. My version is 8.0.1

  • I agree with the previous comments, and I would like to add that this for may have to be improved: if neither the function isDescendant nor other thread alter selectionPaths, can save the value of length before the for, avoiding the successive call to this property.

Show 2 more comments

2 answers

2


Netbeans analyzes only the code, not the context in which it is inserted, i.e., if your code can be refactored, it will give you a hint, but it won’t check all your code to suggest a "decent" refactoring. It is therefore the choice of the programmer whether or not to accept the choice of the IDE.

If you accept any suggestions from the IDE, keep in mind that a code reset will be required to make it plausible to function. In other words, in 70% of cases the Netbeans IDE gives some good suggestions, but in the other 30% it says some nonsense that will depend on the programmer’s experience to work.

As has already been said in the comments, this is subject to a grid discussion about tastes, there are people who prefer codes in one way and people who prefer otherwise, it is the same thing to have:

If() {

}

and

if()
{

}

Going to taste.

  • 1

    If he really is suggesting Refactoring then he is suggesting wrong, because in this case, there is behavior change with the suggested change, which goes against the concept of Refactoring.

  • It is a matter of software intelligence I believe. As it does not analyze all your code it is subject to some local errors of specific blocks.

0

In that particular case it is for the sake of good practice and conventions.

Ex:

The way you did j < selectionPaths.length; if by an oversight put the sign = instead of < you would be assigning the selectionPaths.length at the j.

Already the suggested example selectionPaths.length >= j also assigns in javascript but in other languages or if selectionPaths.length was a method (selectionPaths.length()) and made a mistake by putting the = instead of >= would occur a syntax error being therefore easier to identify than a logic error of this level.

  • 2

    Actually this is not possible to happen in Java. If you do if(1) it simply does not compile, because the if expects a boleano, and does not accept anything other than that at any time. The same goes for the for and the while. So if you do j=selectionPaths.length you will have a whole where you should have a boleano and consequently the IDE itself will warn you of the error before you even try to compile. Therefore, his answer does not justify the inversion of the order of the comparison.

  • 1

    Also "avoid the risk of the programmer getting confused and making an undue assignment" is a marginal improvement at a high cost that is the inversion of comparison logic, and is far from being a good practice or convention (I at least have never seen this recommendation anywhere). It is different for example to do "A".equals(string) instead of string.equals("A") which in this case has a clear and consistent objective (eliminate the risk of calling the method equals from a potentially variable null).

Browser other questions tagged

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