C writing style

Started by
37 comments, last by vicviper 20 years, 3 months ago
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...
Advertisement
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
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

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

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 ifend 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.
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!
And look how much time is wasted on formatting and structure... Long live indentation-is-scope!
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
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

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

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 ifend 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
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

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

Forget what anyone else''s style is like, or whether they think yours is cool. Just use a damn prettyprinter!
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)
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk
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.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

This topic is closed to new replies.

Advertisement