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
.
I use Eclipse, I haven’t used Netbeans for centuries, but I’m curious to know what happened in your case.
– Math
In fact, in this case, the correct suggestion would be
selectionPaths.length > j
and notselectionPaths.length >= j
.– Franchesco
Because it is a software that does not look at the context, for this there is the programmer.
– Maniero
Yeah. Maybe that’s why it’s there as a 'hint' and not refactoring. Refactoring is vaguely improving the code without changing its behavior.
– Franchesco
@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
@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
– Franchesco
I agree with the previous comments, and I would like to add that this
for
may have to be improved: if neither the functionisDescendant
nor otherthread
alterselectionPaths
, can save the value oflength
before thefor
, avoiding the successive call to this property.– Vinícius Gobbo A. de Oliveira