Position of test values in conditional expressions

  • Thread starter =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
  • Start date
G

Gerry Quinn

As a code reviewer, I'd accuse you of inconsistent style. Why do you
explicitly check buf against 0, but implicitly check fp? (Personally I
prefer to _always_ explicitly check, and I prefer using the NULL macro to
help reinforce that I'm dealing with a pointer and not an int).

I'd accept the accusation! I was trying to keep it similar to the
original, but I did it both ways for some reason - not sure why. I also
use NULL, but I thought the implicit zero might be a C thing. As I
said, I don't use that language.

- Gerry Quinn
 
C

CBFalconer

Programmer said:
Gerry said:
I would consider your example to be obfuscated code.

I would agree if we insert the word, "mildly". (-:
Your example:
if (!(buf = malloc(count * sizeof *buf))) err("No mem");

...if I were writing that it [...]:

buf = malloc( count * sizeof( *buf ) );
if ( buf == 0 )

Ditto!

Obviously tastes vary. However I was trying to illustrate the
sequence of tests:

if (precondition1fails) announce();
else if (precondition2fails) announce();
else if (precondition3fails) announce();
else if (precondition4fails) announce();
else { /* all preliminaries satisfied */
/* do it */
}

which often requires the test of an assignment value.
 
P

Programmer Dude

CBFalconer said:
if (!(buf = malloc(count * sizeof *buf))) err("No mem");

...if I were writing that it [...]:

buf = malloc( count * sizeof( *buf ) );
if ( buf == 0 )

Ditto!

Obviously tastes vary. However I was trying to illustrate the
sequence of tests:

if (precondition1fails) announce();
else if (precondition2fails) announce();
else if (precondition3fails) announce();
else if (precondition4fails) announce();
else { /* all preliminaries satisfied */
/* do it */
}

which often requires the test of an assignment value.

Agreed. I might in this case, do the same. Rules of thumb are just
for the usual practice. Other factors may obtain.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top