Coding style for conditionals

R

robertwessel2

  To me, the meaning of both is immediately obvious.


And to me. *But* the second form will immediately send me off trying
to figure out if assignment was actually intended instead of
comparison, and thus is a hindrance to understanding the code, because
of the time taken on the side trip. The "if ((y=f())!=0)" form at
least makes it obvious what was intended.
 
J

Juha Nieminen

mzdude said:
if( 0 == x )

The only reason to do that instead of (x == 0) is to possibly catch a
few mistakes some beginner C/C++ programmers might make. There are many
problems with that, such as:

- Any decent compiler will issue a warning if you do a "if(x = 0)", so
trying to avoid it with a stupid coding convention is quite useless.

- It won't help if you are comparing two variables ("if(a = b)"), so
the cases where it might help are rather limited.

- Do you really want beginner programmers in your project? Even if you
do, if you try to shield them from a rare stupid beginner mistake, and
force the same stupid convention on everybody, you are doing it wrong.
 
R

robertwessel2

  Not to me! Therefore, I have to ask:

      - What is it that was intended?

      - And how does it make this obvious?


By making both the assignment and the comparison explicit, it's clear
(FSVO "clear") that both were intended.
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Stefan said:
if( o )...

for a class instance o can mean anything, which has to be
looked up in the documentation of the class.

It is unclear because it uses one-letter variable name. If the name is more
descriptive, the meaning will be clear.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iEYEARECAAYFAk2ga+EACgkQZ1Turg5KUClSagCeMaRv/KbRXK17IPP/MeeeAkEw
WQgAn1L7sLGuWtr8H5NA2e0k0WrlZG1h
=JRup
-----END PGP SIGNATURE-----
 
Ö

Öö Tiib

By making both the assignment and the comparison explicit, it's clear
(FSVO "clear") that both were intended.

Its irrelevant if you make it explicit. I think putting that
assignment into if is wrong itself and you just try to fix it there
instead of moving it out of if. For me ... most natural is following
paranoid idiom:

y = f(); // is intended to do something
if ( y == 0 ) // is intended to check for problem
{
// is intended for dealing with found problem
}
// if we get here then we have good y

If to follow it and names used are readable then no comments are
needed about wtf is going on.

All the "if((y=++f())==0)", "if(true==!(y=f()))" etc. feels like ...
wtf ... that guy should work in some other team.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,781
Messages
2,569,616
Members
45,306
Latest member
TeddyWeath

Latest Threads

Top