Hi guys
Is it true that writing
is better thanCode:if(3 == a) {}
?Code:if(a == 3) {}
Thanx 'n greez
Hi guys
Is it true that writing
is better thanCode:if(3 == a) {}
?Code:if(a == 3) {}
Thanx 'n greez
I can't see why it would. You're not checking AND there, so the order shouldn't matter.
Quzah.
Hope is the first step on the road to disappointment.
Depends on how you classify better.
It is "better" in the sense that it prevents the mistake of if (x = 3), because if (3 = x) is a compiler error.
On the other hand, it doesn't work for if (x = y), because if x and y are both simple variables, if (y = x) is equally valid (and most likely incorrect).
And many people argue that if you can remember to write if (3 == x) then you can remember to write two equal signs.
It also looks weird doing if (3 > x) instead of if (x < 3)...
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
yah but its kinda akward seeing those signs for me its better to use normal when you excerise alot you should never forget
ya, it may look awkward but believe me when you have a long code and by chance you make such a mistake it will take you hours to identify that ... where as in 3 == a.. compiler will make your life easy .. []
well, it happens to me... also a < 3 is ok , there s no benifit of writing 3>a, but in case of a == 3 it is the asignment , we are talking about ...
Agreed. The problem is that there are so many other situations where this can go wrong (x = = y is no better than y == x, if x and y are simple variables), and as Quzah says, it's often possible to test the code to determine that it does the right thing for equality tests.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
ya, this can help only if we are comparing with constants , so it is better to practice not making a mistake rather than practicing to write wierd looking expressions...
Another "besides" is that "trick" is about 30 years old, from when compilers only actually complained about errors, rather than things which might be suspect (the traditional job of lint).
Modern compilers with a reasonable warning level will tell you about if ( a = 3 ) without any further prompting.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.