From: Junio C Hamano <gitster@pobox.com>
To: Miklos Vajna <vmiklos@frugalware.org>
Cc: git@vger.kernel.org, Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [PATCH] CodingGuidelines: Add a note to avoid assignments inside if()
Date: Sat, 24 May 2008 22:17:26 -0700 [thread overview]
Message-ID: <7vd4nbj72x.fsf@gitster.siamese.dyndns.org> (raw)
In-Reply-To: <1211498769-26871-1-git-send-email-vmiklos@frugalware.org> (Miklos Vajna's message of "Fri, 23 May 2008 01:26:09 +0200")
Thanks.
FYI, recently in a nearby mailing list, we had this gem:
From: Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [PATCH 3/5] isdn: fix integer as NULL pointer warning
Date: Fri, 23 May 2008 08:08:57 -0700 (PDT)
Message-ID: <alpine.LFD.1.10.0805230803290.3081@woody.linux-foundation.org>
...
> len += sprintf(page+len, "%-16s %s\n", "type", s);
> - if ((s = cinfo->version[VER_DRIVER]) != 0)
> + if ((s = cinfo->version[VER_DRIVER]) != NULL)
> len += sprintf(page+len, "%-16s %s\n", "ver_driver", s);
For thigns like this (ie testing an assignment), I personally much prefer
s = cinfo->version[VER_DRIVER];
if (s)
len += sprintf(page+len, "%-16s %s\n", "ver_driver", s);
over the uglier and unreadable version.
IOW, testing assignments is good only when:
- you have to do it because of syntax (ie notably in a "while()" loop)
- there's some reason you want it to be a single statement (eg doing a
macro or other thing)
- of the assignment is really simple, and the test is not against NULL or
zero.
The reason for that "the test is not against NULL or zero" is that testing
for NULL and 0 is better done with just a "if (x)", and in an assignment
that just means either (a) a incomprehensible extra parenthesis just to
shut the compiler up or (b) changing the simple test into a stupid test
(ie doing "if (x != NULL)").
(b) is much preferable to (a), but just doing it as two statements is
much preferable to either!
prev parent reply other threads:[~2008-05-25 5:18 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-05-22 23:26 [PATCH] CodingGuidelines: Add a note to avoid assignments inside if() Miklos Vajna
2008-05-25 5:17 ` Junio C Hamano [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=7vd4nbj72x.fsf@gitster.siamese.dyndns.org \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=torvalds@linux-foundation.org \
--cc=vmiklos@frugalware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).