• ### What is your GameDev Story?

#### Archived

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

# C writing style

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

## 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 on other sites
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 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 on other sites
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 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 on other sites
It''s totally up to you, which one do you like? I personally prefer braces in new line.

##### 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 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 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 on other sites

I have never liked this particular stylistic camp:
intmain (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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 9
• 9
• 15
• 14
• ### Forum Statistics

• Total Topics
634071
• Total Posts
3015336
×