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

13,763

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.

Share:
13,763
Lauren Stephen
Author by

Lauren Stephen

Software Development student at Mohawk College. Adjunct Professor of English at McMaster University.

Updated on July 20, 2022

Comments

  • Lauren Stephen
    Lauren Stephen almost 2 years

    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?

  • dresh
    dresh over 4 years
    but why would you need to flip operands in an == OR === comparison,
  • McKay G
    McKay G over 4 years
    I'm not aware of any reason for needing to flip operands in an == or === comparison, but one reason why you might want to is for readability or convention. Many people prefer to place the more variable value on the left hand side of an equality comparison and the less variable value on the right, as applicable. So (myVar == MY_CONSTANT) would be considered preferable to (MY_CONSTANT == myVar), for example. The first value in the expression is the value in question, which aligns more with our speaking and thinking patterns. ("If they come at 8:00..." vs "If 8:00 is when they come...")
  • Ari Black
    Ari Black over 2 years
    Correct! Only appears when caret is on the line to which this applies. Devs do need another indicator for this. Thanks for clarifying!