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

Lauren Stephen picture Lauren Stephen · Feb 17, 2016 · Viewed 11.7k times · Source

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?

Answer

McKay G picture McKay G · Jul 20, 2017

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.