• Advertisement

Archived

This topic is now archived and is closed to further replies.

C writing style

This topic is 5156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

just a dumb question here.... I''ve been reading a lot of code lately, and I become bored of having to rewrite the Tabs in order to understand somethin... I''m used to code this way: while(a) { while(b) { ++b; } ++a; } while, what I''ve seen most people do is this: while(a) { while(b) { ++b; } ++a; } this second way seems to be the one seen in books & academies... but, isn''t it a lot more blurry? what ya think?

Share this post


Link to post
Share on other sites
Advertisement
I personally like to go the route of each opening and closing bracket on a new line.

Except in cases where I intent to make code a one liner,
such as small class function definitions in header files:

int getValue( ) { return mValue; }

I find it easier to quickly see the nesting of code in large nested functions. And also, so I can quickly identify those wierd compiler messages where you forget/miss/omit a closing bracket.

Share this post


Link to post
Share on other sites
Indentation style discussions are religious issues.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan (C programming language co-inventor)



[edited by - Fruny on January 12, 2004 2:14:37 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
For most people it''s a question about how your favorite editing tool likes to do it. The default style of Emacs is:

for (; {
++a;
}

As so many other things it can be changed, but who bothers. The only thing that I would like to say is that closing brackets should have their own line except in one liners.

Share this post


Link to post
Share on other sites
quote:
Original post by vicviper

while, what I''ve seen most people do is this:

while(a) {
while(b) {
++b; }
++a; }


this second way seems to be the one seen in books & academies... but, isn''t it a lot more blurry?
I won''t say that this is the conclusive answer, but I do know that sometimes when it comes to printed material, you have to give up a bit of readability to fit it on the page.

There''s nothing I hate more than having to put "code continuation" characters to break up a long line but its gotta be done.



Joel Martinez
http://www.codecube.net/

Share this post


Link to post
Share on other sites
I always preferred your first style, braces on a line by themselves (except for the close brace of a do while).

I noticed a trend in how is worked, back in the early 90s...

I noticed that people who can''t stand this style complained it wasted screen space. And it''s true, you''ll typically lose a few lines of code per screen to nearly empty lines. It''s kinda like complaining that paragraphs waste space, on average a line and a half! But that''s not the shocker... these people all insisted in running in 25 line mode rather than 50 line mode, because it was easier to see the text(remember I said early 90s). So instead of running in 50 lines, and losing a few lines to braces, they''d run in 25 line mode and complain that braces would waste their precious screen space.

Share this post


Link to post
Share on other sites
AP: what do you mean by "The default style of Emacs" ??

I use emacs for coding, and never saw any difference between this:

  while (confition) {
do_stuff();
}

and this:


while (confition)
{
do_stuff();
}

O_o

however, this can be changed for auto identation:


while (confition)
{
do_stuff();
}

or

while (confition)
{
do_stuff();
}

or

while (confition)
{
do_stuff();
}

edit: replaced [pre] with [ code]


[edited by - sBibi on January 12, 2004 3:07:09 PM]

Share this post


Link to post
Share on other sites
i remember reading one book about design patterns in java, wich stated right at the beginning, that it will use a rather compact style, just because it fits bether into the book..

so he used a similar style you presented..

but the writer stated, he normally uses more space.. its just for the book..

and believe me, it was a nice thing. for the book.




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

Share this post


Link to post
Share on other sites

I have never liked this particular stylistic camp:

int
main (argc,argv)
int argc;
char **argv;
{
...
}


Nor the equally terrible:

if (blah){
crap();
if (shat){
crapper();
}
else {
crappy();
}
}


I greatly prefer each brace to be on its own line for readability purposes ( lining them up comes more natural to me in that form ). I also don''t care much for separating each little thing in specifying the return type and arguments of a function. I also prefer more parenthesis over more creative exploitation of the precedence rules -- I try to keep it clear and simple.

Although I do prefer the following:



if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
EXIT.....
}



Over the following:



sockfd = socket(AF_INET, SOCK_STREAM, 0);

if ( sockfd < 0 )
{
EXIT.....
}





.z

Share this post


Link to post
Share on other sites
i''ve always felt out of place sticking with the brace on a new line. i''m used to seeing the brace as the trailing character in so much code... i''m surprised so many people responded that they don''t prefer it.

while we''re at it, i can''t stand when people use spaces instead of tabs. make reformatting things so much more difficult... bleah...

Share this post


Link to post
Share on other sites
the best idea of phyton is, that style is grammar. there is just one way.

there still is full freedom on how you want to name your stuff, though..

i''m coding like this:

class Type {
void sayHello(int someParameter) {
printf("Hello World, %i" \n,someParameter);
}
}


it is just a bit more compact, wich i like.. and the {} on the same place, to determine starting and ending.. this isn''t a big issue.. you see it as well in my example above..

after class, you just search for the next token at the same place.. and you find, the final }


if i could, i would define that there is no way to write a class with a small letter at the start, and there is no way to write a function or a variable with a capital letter at the start..
and if there are two capitals at the start, and the first is an I, its an interface (all pure virtual).

you could drop the ''class'' keyword then:D

and for the rest.. phyton rules..

result of the above:

Type
void sayHello(int someParameter)
printf("Hello World, %i" \n,someParameter);


something like this..

hm.. i could actually write a convertor, wich converts that back to D .. :D hehehe




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

Share this post


Link to post
Share on other sites
When I was a n00b most of my coding experience was with (dirty confession time here!) Hypertalk, so my style looked like (perforce, because the script editor would reindent your code every time you hit return - this could get to be rather slow on the old Macs)


on someMessage
if foo = 1 then
doSomething()
else
doSomethingElse()
end if
end someMessage


And since I was using this to develop stuff for something like 7 or 8 years (yeah, I used a 1st-generation Mac - a Classic - until 1999!), it should come as no surprise that my C/C++/Java code looks like:


void someFunction(int foo) {
if (foo == 1) {
doSomething();
} else {
doSomethingElse();
}
}


Notice: K&R style braces, 2-space indent , cuddled elses, braces for if statements even where not needed.

I will only omit the braces if I am also putting the statement on the same line as the if. That way it just reads like a "conditional statement". But I tend to do that sort of thing more for switches than ifs, anyway.

I do not necessarily recommend this style for *anyone else*, but just about the first thing I do when I am handed someone else''s code to grok, is to reformat it in this style.

To each their own, I guess.

Share this post


Link to post
Share on other sites
Wah! never knew this was such a big issue...

about the difference between:

while(x)
{
___do y
}


and

while(x)
____{
____do y
____}

I preffer the second, M$ Visual Studio comes with the first option as default, but this can be modified in the preferences to automatically indent as the second indentation method.

and, at screens resolutions of 1280x1024 or more, some blank space in screen is very very grateful... even my own old, compacted code of the 90''s is hard to read for myself.

thanks all for the replies!

Share this post


Link to post
Share on other sites
quote:
Original post by vicviper
Wah! never knew this was such a big issue...

about the difference between:

while(x)
{
___do y
}


and

while(x)
____{
____do y
____}

I preffer the second, M$ Visual Studio comes with the first option as default, but this can be modified in the preferences to automatically indent as the second indentation method.

and, at screens resolutions of 1280x1024 or more, some blank space in screen is very very grateful... even my own old, compacted code of the 90''s is hard to read for myself.

thanks all for the replies!


first: how about learning to use the code tags? or source tags, for that mather?

second: urgh, ugly, i HATE that style. it''s just style, though.

i still hate it:D




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

Share this post


Link to post
Share on other sites
quote:
Original post by Zahlman
When I was a n00b most of my coding experience was with (dirty confession time here!) Hypertalk, so my style looked like (perforce, because the script editor would reindent your code every time you hit return - this could get to be rather slow on the old Macs)


on someMessage
if foo = 1 then
doSomething()
else
doSomethingElse()
end if
end someMessage


And since I was using this to develop stuff for something like 7 or 8 years (yeah, I used a 1st-generation Mac - a Classic - until 1999!), it should come as no surprise that my C/C++/Java code looks like:


void someFunction(int foo) {
if (foo == 1) {
doSomething();
} else {
doSomethingElse();
}
}


Notice: K&R style braces, 2-space indent , cuddled elses, braces for if statements even where not needed.

I will only omit the braces if I am also putting the statement on the same line as the if. That way it just reads like a "conditional statement". But I tend to do that sort of thing more for switches than ifs, anyway.

I do not necessarily recommend this style for *anyone else*, but just about the first thing I do when I am handed someone else''s code to grok, is to reformat it in this style.

To each their own, I guess.


fully supporting your style. espencially the braces even for small ifs.




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

Share this post


Link to post
Share on other sites
For for/if/while, I put the braces on a line by themselves aligned with the keyword, but for do/while, I use
do{
...
}while(...);

As far as overall style - I really prefer lisp indentation to pythons. Practically everybody indents lisp the same way (most likely because emacs autoindents and it seems to be the most popular lisp editor), but if you don''t want to you can choose a different style (and even reconfigure emacs to know how to indent your custom forms or indent the ones it already knows about differently)

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
Forget what anyone else''s style is like, or whether they think yours is cool. Just use a damn prettyprinter!


I''ve never found even a tolerably good prettyprinter. Is there one? indent is bad enough for C, diabolical for C++, and it''s the one everyone talks about.

Share this post


Link to post
Share on other sites
I cant stand code that is not set out the way i like it, which is not a good thing, especially when i get into the industry (not gameing in particular). Teams often decide on a style beforehand and then everyone has to stick to it. I like space.

int main (void)
{
printf ("Hello\n");
return 0;
}


[edited by - Mr Lane on January 13, 2004 10:17:38 AM]

Share this post


Link to post
Share on other sites
Same Here Mr Lane, I use the same style:

for (i = 0; i < 100; i++)
{
Do_Stuff();
}

that's my way. It doesn't matter what style you use though. As long as it is readable and works

[edited by - Bad Maniac on January 13, 2004 10:25:33 AM]

Share this post


Link to post
Share on other sites
Most editors have auto indentation features, so as long as you can decide wheter or not to use old unix style (brackets open on the same line), it should be fairly easy to tweak your code to the teams desired style .. however, it would probably still be easier just to adapt

Share this post


Link to post
Share on other sites
I''ve worked on proprietary systems that used a bare-bones UNIX terminal with only an 80x24 display mode. It was then that minimizing whitespace became part of my coding style, and it''s stuck ever since. It also works because in the IDE I use, the code window shares space with object inspectors, tool palettes, etc., so whitespace is at a premium.


if (a) {
do_a();
} else {
do_b();
}
...
try {
code_that_throws();
} catch (exception1 & e1) {
handle_exception1();
} catch (...) {
handle_others();
}


--
Dave Mikesell Software & Consulting

Share this post


Link to post
Share on other sites

  • Advertisement