10

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?

Lauren Stephen
  • 101
  • 1
  • 1
  • 9
  • Are you sure it is on that line? Can you show us a few lines up/down from that specific line? – Idos Feb 17 '16 at 09:07
  • Yes, I'm fairly sure it's on that line. The suggestion only appears when I click directly on a mathematical operator. – Lauren Stephen Feb 17 '16 at 09:11
  • No, no change to the order of operands if the operator symbol is clicked. It will change them if I click on the warning and select 'flip operands'. – Lauren Stephen Feb 17 '16 at 09:18
  • @Craig90 are you sure all of the variables are ints/doubles? – DonyorM Feb 17 '16 at 09:23
  • 5
    You know what, I'm starting to think this is a Netbeans feature to give you the option of easily flipping two operands rather than having to retype them. I'm reading the yellow lightbulb as a warning/suggestion, but it may just be giving me the option to do something rather than suggesting it. – Lauren Stephen Feb 17 '16 at 09:31
  • 1
    I think it's just a suggestion in case you want to quickly change them or for readability. You can disable it by pressing the > to the right and disable. – anaxin Feb 17 '16 at 09:34
  • which netbeans version are you using, it seems to be a bug which was reported in version 7.4 and was fixed later, i dint have this issue in 8.x https://netbeans.org/bugzilla/show_bug.cgi?id=229738 – shabby May 30 '16 at 06:59
  • 2
    @LaurenStephen You can answer your own question here. http://stackoverflow.com/help/self-answer – Leonardo Castro Sep 29 '16 at 13:19

2 Answers2

1

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.

McKay G
  • 391
  • 4
  • 9
  • but why would you need to flip operands in an == OR === comparison, – dresh Dec 31 '19 at 06:10
  • 1
    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...") – McKay G Jan 09 '20 at 04:12
1

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 :).

user1132655
  • 243
  • 4
  • 14
  • Correct! Only appears when caret is on the line to which this applies. Devs do need another indicator for this. Thanks for clarifying! – Ari Black Mar 20 '22 at 14:59