Re: strcpy warning

From: Arthur J. O'Dwyer (ajo_at_nospam.andrew.cmu.edu)
Date: 01/03/04


Date: Fri, 2 Jan 2004 18:24:55 -0500 (EST)


On Fri, 2 Jan 2004, Keith Thompson wrote:
>
> "Arthur J. O'Dwyer" <ajo@nospam.andrew.cmu.edu> writes:
> [...]
> > if (foo) {
> > int bar;
> > baz();
> > }
> > else
> > quux();

> > [I also make a rule of 2-space indentation for the bodies of
> > control statements without enclosing braces, like 'quux' above
> > -- but that's not a widespread usage, AFAIK.]
>
> I *always* use braces on control statements, except in rare cases
> where the whole thing is short enough to fit on one line. It's a
> habit I picked up from Perl, which requires the braces, but I find it
> useful [...]

> else {
> quux();
> }
>
> I'm not arguing that everyone must do this, it's just the style I like
> to use.

  I completely agree with you as far as the example I chose -- in a
real program, I would almost always put braces around the body of
an 'else' clause, especially in cases where the matching 'if' clause
had them. But I often write things like

    int foo(void)
    {
        FILE *fp = fopen("bar", "r");

        if (fp == NULL)
          return -1;

        ...

  In that case I find it more convenient *not* to add braces; and
if I'm not going to add braces, I find it more consistent *not* to
indent the body a full level. That way, whenever I'm maintaining
my own style of code, I can tell instantly if a closing brace is
missing: e.g., if I see

            }
    }

or
        }
      }

then I immediately *know* that there's a problem. Of course,
being able to rely on this "gimmick" in my own code means that I
have a harder time with other people's code than I might if I just
wrote awfully-formatted code to begin with. ;-)

> In rare cases I might compress the layout to something like this:
>
> if (foo) do_foo_stuff();
> elsif (bar) do_bar_stuff();
      ^^^^^
  Assuming, of course, that you were programming in CPerl. :)

> but I often regret it later when I'm maintaining the code (which I
> spend a lot more time on than writing it in the first place).

  Yes; I used to do this, but then realized that it was going to
take up a lot of space on the page no matter how I indented it,
so I might as well be consistent.

-Arthur



Relevant Pages

  • Re: oscillator
    ... Read other peoples code and learn to indent and use your ... braces clearly and consistently. ... consistent. ... As to level of understanding, I don't profess to be a C expert or I ...
    (comp.lang.c)
  • Re: Coding standards
    ... variables and the C idiom that was established with the C standard library. ... There's no need for any braces ... with spacing used to make a nested parameter ... >> I know almost nothing about the `indent' utility. ...
    (comp.lang.c)
  • Re: Braces or not [Re: cat]
    ... BUT - it is appreciably harder to remember to add them later on ... I used to actually always put the braces in. ... placing the cursor in the new linedirectly under the 'l' from while, ... so new indent is done, ...
    (comp.lang.c)
  • Re: Format C# Braces in IDE?
    ... > {// inline as indicated ... several suboptions under Formatting that affect how C# code is formatted. ... Check all options under New Lines->New line options for braces. ... Check "Indent block contents" under Indentation and uncheck "Indent open ...
    (microsoft.public.dotnet.languages.csharp)