[prog] C and curly braces - question of style?
Diggy Bell
diggy at dbsoftdev.com
Sun Aug 22 15:55:09 EST 2004
Hey Angelina,
I'm probably going to set myself up to be torched here, but braces are
something that I generally see as a matter of personal style. Everybody has
their own personal tastes and I think this is one that shouldn't be such a
religions battle.
There are a few suggestions that you might want to keep in mind. First, if
you're working by yourself it doesn't make much difference, but if you're
working with a team of people it really helps if everybody uses the same
style. Second, whatever style you do choose, be consistent through our your
code. Sometimes this means taking a few minutes to reformat code, but its
well worth it in the long run. Finally, I strongly suggest that people use
braces even in cases where they aren't necessary. You don't need them on a
for() loop or if() statement if there is a single line of code, but when you
go back and add a line 6 months from now it'll be a whole easier to not
accidentally leave the braces out.
In terms of the 'best' way, it doesn't make any difference to the compiler
so there isn't really a wrong way.
Hope this helps...
Diggy
----- Original Message -----
From: "aec" <brat at magma.ca>
To: <programming at linuxchix.org>
Sent: Sunday, August 22, 2004 3:28 PM
Subject: [prog] C and curly braces - question of style?
> Hello,
>
> I have recently begun what is looking like a long
> and difficult journey into learning C.
>
> I am doing this from a book (Programming in C, Stephen Kochan)
> and from a tutorial I found online.
>
> At this point I am working on little examples intended
> for no other use but to teach.
>
> I am told my "style" of using curly braces {} in loops
> is not generally the best way to do it.
>
> Here is what I type for example inside a function:
>
> tri_number = 0;
> for (number = 1; number <= 15; ++number)
> {
> tri_number = tri_number + number;
> printf("%2i %i\n", number, tri_number);
> }
>
>
> I am told however, that if you start a "for" loop, this is clearer:
>
>
> tri_number = 0;
> for (number = 1; number <= 15; ++number) {
> tri_number = tri_number + number;
> printf("%2i %i\n", number, tri_number);
> }
>
> To me, its not clearer, but who am I to argue at this point,
> but for now, id like to continue doing it my way and worry
> about actual syntax rather than style. Style isnt worth much
> if your programs dont work right?
>
> Sure, its early in the game, and I have so much ahead of me to
> learn, so why not change now to the "accepted" way? Because basically its
> another little thing to remember and I have enough to keep track of,
> and like I said, the first example to me is clear as a bell.
>
> any thoughts? or should i just worry about more important things :)
>
> --
> Angelina Carlton
> _______________________________________________
> Programming mailing list
> Programming at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/programming
>
>
More information about the Programming
mailing list