Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Netbeans suggesting I "Flip operands of the binary operators" in my Java code

Netbeans frequently suggests that I "flip operands of the binary operator" when I'm doing mathematical calculations. For example, in the following line of code:

    change = 100 - price;

    quarters = change / 25;
    dimes = change % 25 / 10;
    nickels = change % 25 % 10 / 5;
    pennies = change % 25 % 10 % 5;

Netbeans makes the suggestion for every mathematical symbol (so it does it three times in the 'pennies' line.

I'm not sure I understand why it's making the suggestion. If I were to flip the operands while performing division I would get a different result (if "flip" means what I think it does, which is switch the order of the two values). Why does it suggest this?

like image 566
Lauren Stephen Avatar asked Feb 17 '16 09:02

Lauren Stephen


2 Answers

Had this issue also, and just today recognized that this is not a warning and not even a suggestion. The icon looks like a warning but actually it is a "refactoring utility" to for example flip operator or invert if without having to type, but choosing from actions in the menu opened when clicking the icon. The icon appears only when you click on a symbol for which such thing can be done. The icon is same as a warning icon, the only difference is that for warnings there is also a yellow underline and it is permanent. I would suggest Netbeans devs to change the icon, this really confused me for years :).

like image 80
user1132655 Avatar answered Oct 09 '22 13:10

user1132655


Your thought that this is a Netbeans feature for quickly flipping the operands is correct. This is one of the relatively few "suggestions/actions" available in Java Hints (http://wiki.netbeans.org/Java_Hints), as opposed to the more numerous "hints/inspections".

In Netbeans 8.2 I have verified that when pressing Alt + Enter in the pennies line in your snippet, there is a menu option to "Flip operands of '%' (may alter semantics)'. Actually, it may bring up multiple such menu options because there are multiple binary operators. If you choose to flip the operands then the hint will remain, and you can flip them again, over and over, by the same means.

Apparently Netbeans is smart enough to at least be aware that flipping the operands for this type of operator could change the semantics (although it doesn't mention the behavior). For '==' it doesn't carry that warning.

like image 40
McKay G Avatar answered Oct 09 '22 12:10

McKay G