Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #3825

Re: compiler switch to indicate "end of function"?

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: compiler switch to indicate "end of function"?
Date 2011-05-12 11:49 -0700
Organization None to speak of
Message-ID <lnr583kb3s.fsf@nuthaus.mib.org> (permalink)
References <e3632e70-5adf-4f04-807a-30634a5fc405@s41g2000prb.googlegroups.com> <78ab67fd-3a5e-49f2-8179-9501f22cbe26@n10g2000yqf.googlegroups.com> <lnaaeum5x3.fsf@nuthaus.mib.org> <53430786-cd17-4f69-8b9e-8de7fae5458d@35g2000prp.googlegroups.com>

Show all headers | View raw


David Mathog <dmathog@gmail.com> writes:
> On May 10, 5:34 pm, Keith Thompson <ks...@mib.org> wrote:
>>
>> My only quibble is that I'd put a space in front of the opening '{'.
>>
>
> Why do you favor that form over the "spaceless" variant?

To be clear we're talking about

    if(condition){
        ...
    }

vs.

    if (condition) {
        ...
    }

(I also added a space between "if" and "(").  I just find the latter
form significantly easier to read.  Too many consecutive puncuation
characters tend to blend together visually.  The space preceding the
"{" makes it easier to see.  It doesn't (IMHO) need to be on a line
by itself, but it does need to stand out.

And I like a space after the "if" partly because the spaceless
version looks too much like a function call.  It may also be partly
because, before learning C, I used a language (ok, it was Pascal)
that didn't use parentheses for if statements, and not having some
white space after "if" is a bit jarring.

And it's how K&R do it.

I don't suggest that my opinion is right and yours is wrong; it's
largely a matter of personal taste, K&R precedent, and my own
failing visual acuity.

> The (long story) background for the first post is that once upon a
> time I used to use:
>
> if(condition)do_something();
>
> but when "do_something" was too long wrote it as
>
> if(condition)
>   do_something();
>
> which, in nested code when the condition and do_something lines were
> long enough that the right side wasn't normally visible, sometimes led
> me to think this was:
>
> if(condition){
>   do_something();}
>
>
> so that another line could be added within the conditional
> if(condition){
>   do_something_first();
>   do_something();}
>
> when it was actually
>
> if(condition)
>   do_something_first();
>   do_something();
>
> which is not at all the same thing.

In my own style, and in every common style I've seen, a closing brace is
always on a line by itself.  The only exception to this is the use, in
some styles (not mine), of "} else {".

>                                      Other times the confusion was:
>
> if(long_condition)actual_do_something_off_right_side();
>    looks_like_conditional_but_not_do_something();
>
> I'm not saying this happened even once a week, but it happened often
> enough that I changed my style to avoid both of those, instead always
> putting braces on the expression following an if:
>
> if(condition){ do_something(); }

Personally, I almost always use braces for if, for, while, et al (a
habit I picked up from Perl, which requires them, but I think it's a
good idea regardless).  I'll only omit the braces if the whole thing
fits on a single line *and* is clearer that way, so I'd write:

    if (condition) do_something();

I'll violate my usual rules and write things on one line if there
are several consecutive similar statements, and aligning the code
vertically makes it easier to read:

    if (condition1) do_something(1);
    if (condition2) do_something(2);
    if (condition3) do_something(3);

> But it turns out that while this eliminates the preceding issue, it is
> relatively easy for an errant key stroke to nip off the closing brace
> since it is the last character on the line, leading to the issue that
> started this thread.  Particularly hard to see in long lines where the
> closing brace is often off the right of the screen.   As others have
> noted, one can reduce the frequency of this issue by using the form:
>
> if(condition)
> {
>   do_something();
> }
>
> which is fine in low density code, but is on the losing end of the
> readability tradeoff (in my opinion) in code that more closely
> resembles a table, like this:
>
> if(condition1){       a=1;  b=2; c=3; /*more terms*/ }
> else if(condition2){  a=1;  b=0; c=3; /*more terms*/ }
> else if(condition3){  a=1;  b=2; c=0; /*more terms*/ }
> /*etc.*/

There I would have aligned the "{"s, and possibly the conditions:

if      (condition1) { a=1;  b=2; c=3; /*more terms*/ }
else if (condition2) { a=1;  b=0; c=3; /*more terms*/ }
else if (condition3) { a=1;  b=2; c=0; /*more terms*/ }

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

compiler switch to indicate "end of function"? David Mathog <dmathog@gmail.com> - 2011-05-10 09:00 -0700
  Re: compiler switch to indicate "end of function"? Tom St Denis <tom@iahu.ca> - 2011-05-10 10:08 -0700
  Re: compiler switch to indicate "end of function"? Ben Pfaff <blp@cs.stanford.edu> - 2011-05-10 10:30 -0700
    Re: compiler switch to indicate "end of function"? David Mathog <dmathog@gmail.com> - 2011-05-10 15:54 -0700
    Re: compiler switch to indicate "end of function"? David Mathog <dmathog@gmail.com> - 2011-05-10 15:56 -0700
      Re: compiler switch to indicate "end of function"? Keith Thompson <kst-u@mib.org> - 2011-05-10 17:41 -0700
        Re: compiler switch to indicate "end of function"? blp@cs.stanford.edu (Ben Pfaff) - 2011-05-10 21:07 -0700
    Re: compiler switch to indicate "end of function"? Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-05-10 19:32 -0400
      Re: compiler switch to indicate "end of function"? Michael Press <rubrum@pacbell.net> - 2011-05-10 23:32 -0700
        Re: compiler switch to indicate "end of function"? Datesfat Chicks <datesfat.chicks@gmail.com> - 2011-05-11 11:49 -0400
          Re: compiler switch to indicate "end of function"? Michael Press <rubrum@pacbell.net> - 2011-05-11 14:19 -0700
  Re: compiler switch to indicate "end of function"? Keith Thompson <kst-u@mib.org> - 2011-05-10 10:37 -0700
  Re: compiler switch to indicate "end of function"? "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-05-10 13:34 -0700
    Re: compiler switch to indicate "end of function"? Seebs <usenet-nospam@seebs.net> - 2011-05-10 21:22 +0000
      Re: compiler switch to indicate "end of function"? "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-05-10 16:00 -0700
        Re: compiler switch to indicate "end of function"? Seebs <usenet-nospam@seebs.net> - 2011-05-11 03:16 +0000
      Re: compiler switch to indicate "end of function"? Ian Collins <ian-news@hotmail.com> - 2011-05-11 12:32 +1200
        Re: compiler switch to indicate "end of function"? Keith Thompson <kst-u@mib.org> - 2011-05-10 17:56 -0700
        Re: compiler switch to indicate "end of function"? blp@cs.stanford.edu (Ben Pfaff) - 2011-05-10 21:09 -0700
          Re: compiler switch to indicate "end of function"? Ian Collins <ian-news@hotmail.com> - 2011-05-11 16:14 +1200
            Re: compiler switch to indicate "end of function"? Ben Pfaff <blp@cs.stanford.edu> - 2011-05-10 21:19 -0700
              Re: compiler switch to indicate "end of function"? Ian Collins <ian-news@hotmail.com> - 2011-05-11 16:26 +1200
              Re: compiler switch to indicate "end of function"? Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-11 07:10 +0100
                Re: compiler switch to indicate "end of function"? Ian Collins <ian-news@hotmail.com> - 2011-05-11 20:50 +1200
                Re: compiler switch to indicate "end of function"? Dr Nick <3-nospam@temporary-address.org.uk> - 2011-05-11 18:53 +0100
              Re: compiler switch to indicate "end of function"? Sherm Pendley <sherm.pendley@gmail.com> - 2011-05-11 11:23 -0400
            Re: compiler switch to indicate "end of function"? Seebs <usenet-nospam@seebs.net> - 2011-05-11 05:30 +0000
      Re: compiler switch to indicate "end of function"? Jonathan Leffler <jleffler@earthlink.net> - 2011-05-10 21:26 -0700
        Re: compiler switch to indicate "end of function"? Keith Thompson <kst-u@mib.org> - 2011-05-11 08:07 -0700
      Re: compiler switch to indicate "end of function"? luser- -droog <mijoryx@yahoo.com> - 2011-05-11 06:04 -0700
        Re: compiler switch to indicate "end of function"? pete <pfiland@mindspring.com> - 2011-05-11 09:42 -0400
    Re: compiler switch to indicate "end of function"? Keith Thompson <kst-u@mib.org> - 2011-05-10 17:34 -0700
      Re: compiler switch to indicate "end of function"? David Mathog <dmathog@gmail.com> - 2011-05-12 08:44 -0700
        Re: compiler switch to indicate "end of function"? Kenneth Brody <kenbrody@spamcop.net> - 2011-05-12 13:41 -0400
          Re: compiler switch to indicate "end of function"? Michael Press <rubrum@pacbell.net> - 2011-05-12 12:08 -0700
            Re: compiler switch to indicate "end of function"? Chad <cdalten@gmail.com> - 2011-05-12 15:28 -0700
        Re: compiler switch to indicate "end of function"? Keith Thompson <kst-u@mib.org> - 2011-05-12 11:49 -0700
          Re: compiler switch to indicate "end of function"? David Mathog <dmathog@gmail.com> - 2011-05-13 08:56 -0700
            Re: compiler switch to indicate "end of function"? "robertwessel2@yahoo.com" <robertwessel2@yahoo.com> - 2011-05-14 00:03 -0700

csiph-web