{"thread":{"id":"13627","subject":"[PATCH] CodingGuidelines: Add a note to avoid assignments inside if()","startedAt":"2008-05-22T23:26:09Z","lastAt":"2008-05-25T05:17:26Z","messageCount":2,"participants":["Miklos Vajna","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"77513","messageId":"1211498769-26871-1-git-send-email-vmiklos@frugalware.org","threadId":"13627","inReplyTo":null,"subject":"[PATCH] CodingGuidelines: Add a note to avoid assignments inside if()","fromName":"Miklos Vajna","fromEmail":"vmiklos@frugalware.org","sentAt":"2008-05-22T23:26:09Z","receivedAt":"2008-05-22T23:26:09Z","isPatch":true,"sender":{"key":"vmiklos@frugalware.org","avatar":"https://gravatar.com/avatar/401c1cbbb3a5d13e650c691a2c71d6fd0b80df1a01bc74d9f1972675dd58f2bd?d=mp&s=160"},"body":"Signed-off-by: Miklos Vajna <vmiklos@frugalware.org>\n---\n\nI just had to ask Dscho about this. Better if it's documented, I\nsuppose.\n\n Documentation/CodingGuidelines |    2 ++\n 1 files changed, 2 insertions(+), 0 deletions(-)\n\ndiff --git a/Documentation/CodingGuidelines b/Documentation/CodingGuidelines\nindex 994eb91..d2a0a76 100644\n--- a/Documentation/CodingGuidelines\n+++ b/Documentation/CodingGuidelines\n@@ -89,6 +89,8 @@ For C programs:\n    of \"else if\" statements, it can make sense to add braces to\n    single line blocks.\n \n+ - We try to avoid assignments inside if().\n+\n  - Try to make your code understandable.  You may put comments\n    in, but comments invariably tend to stale out when the code\n    they were describing changes.  Often splitting a function\n-- \n1.5.5.1.357.g1af8b.dirty\n"},{"id":"77693","messageId":"7vd4nbj72x.fsf@gitster.siamese.dyndns.org","threadId":"13627","inReplyTo":"1211498769-26871-1-git-send-email-vmiklos@frugalware.org","subject":"Re: [PATCH] CodingGuidelines: Add a note to avoid assignments inside if()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2008-05-25T05:17:26Z","receivedAt":"2008-05-25T05:17:26Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Thanks.\n\nFYI, recently in a nearby mailing list, we had this gem:\n\n    From: Linus Torvalds <torvalds@linux-foundation.org>\n    Subject: Re: [PATCH 3/5] isdn: fix integer as NULL pointer warning\n    Date: Fri, 23 May 2008 08:08:57 -0700 (PDT)\n    Message-ID: <alpine.LFD.1.10.0805230803290.3081@woody.linux-foundation.org>\n\n    ...\n    >  \tlen += sprintf(page+len, \"%-16s %s\\n\", \"type\", s);\n    > -\tif ((s = cinfo->version[VER_DRIVER]) != 0)\n    > +\tif ((s = cinfo->version[VER_DRIVER]) != NULL)\n    >  \t\tlen += sprintf(page+len, \"%-16s %s\\n\", \"ver_driver\", s);\n\n    For thigns like this (ie testing an assignment), I personally much prefer\n\n            s = cinfo->version[VER_DRIVER];\n            if (s)\n                    len += sprintf(page+len, \"%-16s %s\\n\", \"ver_driver\", s);\n\n    over the uglier and unreadable version.\n\n    IOW, testing assignments is good only when:\n\n     - you have to do it because of syntax (ie notably in a \"while()\" loop)\n\n     - there's some reason you want it to be a single statement (eg doing a \n       macro or other thing)\n\n     - of the assignment is really simple, and the test is not against NULL or \n       zero.\n\n    The reason for that \"the test is not against NULL or zero\" is that testing \n    for NULL and 0 is better done with just a \"if (x)\", and in an assignment \n    that just means either (a) a incomprehensible extra parenthesis just to \n    shut the compiler up or (b) changing the simple test into a stupid test \n    (ie doing \"if (x != NULL)\").\n\n    (b) is much preferable to (a), but just doing it as two statements is \n    much preferable to either!\n"}]}