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


Groups > comp.lang.c > #151022

Re: Coding style for small if statements

Path csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Coding style for small if statements
Date Sat, 04 Apr 2020 14:09:48 -0700
Organization A noiseless patient Spider
Lines 77
Message-ID <86blo7nejn.fsf@linuxsc.com> (permalink)
References <20200131234613.59047e037939ceb549b8e6d6@gmail.com> <r1js0d$n3s$1@gioia.aioe.org> <Rkf%F.196564$5o5.140327@fx41.iad> <86blpyfvv6.fsf@linuxsc.com> <slrn3j3gr4j33j.pa1.ike@sdf.lonestar.org> <86k14db27f.fsf@linuxsc.com> <slrn3ov8r55ncg.idr.ike@iceland.freeshell.org> <86v9mm7ghq.fsf@linuxsc.com> <slrn3m1cr8334v.6ah.ike@sdf.lonestar.org>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Info reader02.eternal-september.org; posting-host="962b2addb8ebbeafdd69920a30d34173"; logging-data="27074"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19n0+zVGiVrCSX6VM1IzqiCvKgrGusMFtc="
User-Agent Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock sha1:D7HOQuJiSHqP1O+WwLlciUksc/0= sha1:BNnQIqATTW5S6G2TUhYe/faUqAY=
Xref csiph.com comp.lang.c:151022

Show key headers only | View raw


Ike Naar <ike@sdf.lonestar.org> writes:

> On 2020-03-30, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>
>> I don't understand the gain of putting open braces on lines by
>> themselves.  I've asked lots of people who choose to write code in
>> that style what they think the benefits are, but the answers I've
>> gotten always seem circular in one way or another.  "I like it
>> because it's more readable" sounds suspiciously like "I like it
>> because I like it."  My asking the question is not meant to be
>> argumentative.  I ask because I want to understand other people's
>> views.  So far though as best I can make out the reason is they
>> like it because that's what they are used to.  Can you shed any
>> more light on this question?
>
> I can give you a reason why I find it more readable:
> the vertical alignment of corresponding opening and closing braces
> makes it easier to see that they form a pair.

I sometimes write controlled statements (of if(), for(), etc), on
the same line as the if(), etc.  When I do this I do not use
braces unless they are necessary for some other reason.

When I write a controlled statement on a subsequent line, even if
it is only a single (non-block) statement, I always use braces.

Do you always use braces for controlled statements not on the
same line as the controlling phrase (if(), etc)?

If you do, doesn't the if() or for(), etc, vertically aligned
with the closing brace, stand out to show the range/pairing of
each group?  I don't see how putting in an additional line with
just an open brace adds any useful information.  (Please note
that I am not saying there isn't any, just that I don't see
how there is.)


> Question to you, as a follower of K&R style:
> You don't see much gain in putting the opening brace on a line by
> itself.  What is the benefit of putting the closing brace on a
> line by itself?

First I assume the alternative you want me to consider for this
question is not this

    if(...){
      something;
    } next statement...

but this

    if(...){
      something; }

(with the understanding that horizontal white space is not part
of the question).  Incidentally this latter form is sometimes
called "Lisp style".  Some C programmers do use Lisp-style
placement of closing braces (even though I myself do not).

Surely everyone knows this, but for completeness, K&R style is
this

    if(...){
      something;
    }

with the closing brace lined up under the 'i' of 'if' or the 'f'
of 'for', etc.

I think K&R-style closing brace placement is a better choice
than, eg, Lisp-style placement, because studies have found that
defect rates are lowest for the placement of closing braces where
K&R style puts them.  (I don't know of any corresponding study
for the placement of open braces.)  This result matches my
personal experience, although of course that experience is
evaluated subjectively rather than trying to do some sort of
objective measurement.

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


Thread

Re: Coding style for small if statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-16 06:30 -0800
  Re: Coding style for small if statements David Brown <david.brown@hesbynett.no> - 2020-02-16 16:14 +0100
    Re: Coding style for small if statements Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-02-16 16:40 +0000
      Re: Coding style for small if statements David Brown <david.brown@hesbynett.no> - 2020-02-16 17:48 +0100
      Re: Coding style for small if statements Robert Wessel <robertwessel2@yahoo.com> - 2020-02-16 17:14 -0600
      Re: Coding style for small if statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-03-30 00:27 -0700
  Re: Coding style for small if statements Ike Naar <ike@sdf.lonestar.org> - 2020-02-16 18:39 +0000
    Re: Coding style for small if statements Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-02-16 20:37 +0000
      Re: Coding style for small if statements David Brown <david.brown@hesbynett.no> - 2020-02-17 09:09 +0100
        Re: Coding style for small if statements Öö Tiib <ootiib@hot.ee> - 2020-02-17 05:32 -0800
        Re: Coding style for small if statements scott@slp53.sl.home (Scott Lurndal) - 2020-02-17 18:07 +0000
        Re: Coding style for small if statements Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-02-17 23:48 +0000
        Re: Coding style for small if statements Richard Damon <Richard@Damon-Family.org> - 2020-02-17 20:40 -0500
    Re: Coding style for small if statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-02-23 10:14 -0800
      Re: Coding style for small if statements Ike Naar <ike@iceland.freeshell.org> - 2020-02-23 20:16 +0000
        Re: Coding style for small if statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-03-29 18:56 -0700
          Re: Coding style for small if statements Ike Naar <ike@sdf.lonestar.org> - 2020-03-30 06:07 +0000
            Re: Coding style for small if statements Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-03-30 06:45 +0000
              Re: Coding style for small if statements David Brown <david.brown@hesbynett.no> - 2020-03-30 10:50 +0200
                Re: Coding style for small if statements gazelle@shell.xmission.com (Kenny McCormack) - 2020-03-30 13:04 +0000
                Re: Coding style for small if statements Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-03-30 06:17 -0700
            Re: Coding style for small if statements Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-04-04 14:09 -0700

csiph-web