0

I have a question in my mind. In many places I saw

int a=2;
if(2==a){
  //Some operation
}

Can you tell me what is the advantage on comparing by 2==a in place of a==2 .

Deepu--Java
  • 3,742
  • 3
  • 19
  • 30
  • 1
    no difference, no advantage. :) – codingenious Mar 24 '14 at 12:43
  • 5
    That is called [yoda conditons](http://stackoverflow.com/questions/22106713/what-is-the-difference-between-if-null-pointer-vs-if-pointer-null/22106732#22106732) and with a modern compiler it should not be necessary anymore. – Shafik Yaghmour Mar 24 '14 at 12:44
  • 3
    It is supposed to be so you avoid making this mistake: `if (a = 2)`. Personally, I find it hard to read such code, and the compilers I use warn about the erroneous assignment. – juanchopanza Mar 24 '14 at 12:44
  • @juanchopanza with the exception that your coding style actually uses or requires this (e.g. in a CAS loop). Not that this is good style, but it does force the problem this will catch. – djechlin Mar 24 '14 at 12:48

2 Answers2

11

Some people like to do that to force an error, rather than incorrect behaviour, if they accidentally typed = instead of ==. However:

  • it only helps in some circumstances (when one of the values to be compared is modifiable and the other isn't)
  • any decent compiler will give a warning if you make that mistake (add parentheses and/or an explicit comparison if you really do want assignment)

so the only real purpose is to make the code confusing to read.

A better habit might be to declare all variables const, unless you specifically want them to be mutable. That would also prevent accidental assignment, while making the code easier rather than harder to reason about.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Yes - it helps detect typo (2 = x) when (2 == x) is meant No - compiler won't detect (x = 2) when (x == 2) is meant It is remarkably easy for the human reader to adjust to the literal first (and thus not confusing to read), AND remarkably difficult for the human writer to avoid the typo (so it still happens). Getting the compiler to help detect / prevent this error is useful. Why wait for a run time error to debug? – 2785528 Mar 24 '14 at 13:11
  • 2
    @DOUGLASO.MOEN: Indeed, don't wait for a run-time error. As I said, any decent compiler will give a warning if you make this mistake (despite your apparent assertion that they won't). – Mike Seymour Mar 24 '14 at 13:12
  • Oh yeah. About that - in two environments at which I have contracted, the locals seemed to believe that all comments were trivial, and thus ignored them. I suppose they were simply overwhelmed. The last time I checked, the build I was working on had 25,000 warnings. If there were any of these typo's I'm sure they did not find them until run time. 25000/300 = 83. I set up a macro (in emacs) and looked at every 83rd warning, and tabulated each. FYI: 97% of the warnings were (more or less) harmless. Finding that other 3%, well I had other assignments. My code had no warning's. – 2785528 Mar 24 '14 at 15:23
  • 1
    @DOUGLASO.MOEN: Indeed, if you're already ignoring thousands of warnings then there's little the compiler can do to help. My advice is for at those attempting to build and maintain clean code; silly conventions like this (which can't be enforced any more than correctly typing `==` can) might be of some marginal benefit if you're already in an unmaintainable quagmire, but are not something I'd ever recommend. – Mike Seymour Mar 24 '14 at 15:33
3

This has became famous after the story mentioned in preface/introduction in the book Expert C Programming: Deep C Secrets By Peter van der Linden.There was $20 Million Bug related to this concept. So experts from this book

if (i=3)
instead of:
if (i==3)

Some programmers have developed the habit of writing the literal first, like this: if (3==i). Then, if an equal sign is accidentally left out, the compiler will complain about an"attempted assignment to literal." This won't protect you when comparing two variables, but every little bit helps.

In Spring 1993, in the Operating System development group at SunSoft, we had a "priority one" bug report come in describing a problem in the asynchronous I/O library. The bug was holding up the sale of $20 million worth of hardware to a customer who specifically needed the library functionality, so we were extremely motivated to find it. After some intensive debugging sessions, the problem was finally traced to a statement that read :

x==2;

It was a typo for what was intended to be an assignment statement. The programmer 's finger had bounced on the "equals" key, accidentally pressing it twice instead of once. The statement as written compared x to 2, generated true or false, and discarded the result .

Mantosh Kumar
  • 5,659
  • 3
  • 24
  • 48
  • How come the compiler didn't warn you? Not all warnings activated or too many spurious warnings? – Deduplicator Mar 24 '14 at 13:36
  • 1
    @Deduplicator: About that - in two environments at which I have contracted, the locals seemed to believe that all comments were trivial, and thus ignored them. I suppose they were simply overwhelmed. The last time I checked, the build I was working on had 25,000 warnings. If there were any of these typo's I'm sure they did not find them until run time. 25000/300 = 83. I set up a macro (in emacs) and looked at every 83rd warning, and tabulated each. FYI: 97% of the warnings were (more or less) harmless. Finding that other 3%, well I had other assignments. My code had no warning's. – 2785528 Mar 24 '14 at 15:31