Skip to content

Commit

Permalink
added note about checking strcmp() as a boolean
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.parrot.org/parrot/trunk@24013 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information
petdance committed Dec 17, 2007
1 parent 437e411 commit a62a309
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/pdds/pdd07_codingstd.pod
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,14 @@ Avoid double negatives, e.g. C<#ifndef NO_FEATURE_FOO>.
Do not compare directly against NULL, 0, or FALSE. Instead, write a boolean
test, e.g. C<if (!foo) ...>.

However, the sense of the expression being checked must be a boolean.
Specifically, C<strcmp()> and its brethren are three-state returns.

if ( !strcmp(x,y) ) # BAD, checks for if x and y match, but looks
# like it is checking that they do NOT match.
if ( strcmp(x,y) == 0 ) # GOOD, checks proper return value
if ( STREQ(x,y) ) # GOOD, uses boolean wrapper macro

(Note: C<PMC *> values should be checked for nullity with the C<PMC_IS_NULL>
macro, unfortunately leading to violations of the double-negative rule.)

Expand Down

0 comments on commit a62a309

Please sign in to comment.