git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Nieder <jrnieder@gmail.com>
To: Leila Muhtasib <muhtasib@gmail.com>
Cc: git@vger.kernel.org, Thomas Rast <trast@student.ethz.ch>,
	konglu@minatec.inpg.fr
Subject: Re: [PATCH/RFC] Documenation update: use of braces in if/else if/else chain
Date: Sun, 10 Jun 2012 15:22:05 -0500	[thread overview]
Message-ID: <20120610202205.GA2052@burratino> (raw)
In-Reply-To: <1339349190-84552-1-git-send-email-muhtasib@gmail.com>

Hi,

Leila Muhtasib wrote:

> --- a/Documentation/CodingGuidelines
> +++ b/Documentation/CodingGuidelines
> @@ -117,9 +117,26 @@ For C programs:
>  
>     is frowned upon.  A gray area is when the statement extends
>     over a few lines, and/or you have a lengthy comment atop of
> -   it.  Also, like in the Linux kernel, if there is a long list
> -   of "else if" statements, it can make sense to add braces to
> -   single line blocks.
> +   it.  Also, like in the Linux kernel, if one of the
> +   "if/else if/else" chain has a multiple statement block, use {}
> +   even for a single statement block in that chain. And "else"
> +   should come on the same line as the closing "}" of its "if" block.

I don't think that's quite accurate.  Current best practice in both
git and the Linux kernel is a little looser than that.

> +
> +	//correct
> +	if (bla) {
> +		x = 1;
> +		...
> +	} else {
> +		x = 2;
> +	}

True.

> +
> +	//incorrect
> +	if (bla) {
> +		x = 1;
> +		...
> +	}
> +	else
> +		x = 2;

Also true.  But:

	/* correct */
	if (bla) {
		x = 1;
		...
	} else
		x = 2;

And:

If you have a long "if" with a one-line "else", consider whether you
are needlessly keeping the reader in suspense about something simple.
It might be more pleasant to read with the exceptional case up front:

	if (!bla) {
		x = 2;
	} else {
		x = 1;
		...
	}

This is especially true when the exceptional case returns or exits.

	if (bla && no_bla)
		return error("--blah and --no-blah cannot be used together");
	x = 1;
	...

Hope that helps,
Jonathan

  reply	other threads:[~2012-06-10 20:22 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-10 17:26 [PATCH/RFC] Documenation update: use of braces in if/else if/else chain Leila Muhtasib
2012-06-10 20:22 ` Jonathan Nieder [this message]
2012-06-10 20:52   ` Leila

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=20120610202205.GA2052@burratino \
    --to=jrnieder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=konglu@minatec.inpg.fr \
    --cc=muhtasib@gmail.com \
    --cc=trast@student.ethz.ch \
    /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).