Skip to main content
gamedev.net
🔒 Locked

Different coding styles.

Started by Vlion May 12, 2004 at 5:13 PM 74 replies 6.3k views
Original Post
Vlion
Vlion
So I've been bugged all semester by my friend about not using K&R style with C and C++. I'm quite sick of it, actually. I don't use K&R, I developed my own style. But I'd like to look at other ones out there. What other styles are there, and why do you prefer them? Could you give an example, please? Example of my code:
class C
{
public:
    blah;
    foo();   
}

int foo()
{
    if(oof)
    {
         cout << "no way";
    }
    else if
    {
        cout << "oh?";
    }
    else
    {
         cout << "j00 f00";
    }
    do
    {
    }while(yes);
    for(int i = 0; i < oof; i++)
    {
        //blah

    }
    while
    {
     //more blag

    }

}
 
-ediit: tweaked tags- [edited by - Vlion on May 12, 2004 6:15:33 PM]
~V'lionBugle4d
rypyr
rypyr

- I prefer putting the leading brace for if''s, for''s, while''s, do''s on the same line. However the leading brace for functions and classes go on a separate line. I''m quirky that way.

- I always pre-increment my loop variables.

- classes start with a capital letter, interface-type classes usually start with ''I''

- I use the Hungarian ''m_'' for the start of my member variables

- I usually prefix variables with the type but not consistently (which maybe defeats the whole purpose?). i.e. bools always begin with a ''b''

- I camel-case my member functions

- I always attach the * in a pointer declaration to the type, not the variable name. i.e.:

int* p; // p is of type "pointer-to-int"

However this last one could get me into trouble because of stupid C/C++ syntax:

int* p, v; // p is a pointer, v is an int!!

I hate that...any idea why that syntax was chosen?

Regards,
Jeff


[ CodeDread ]
nonpop
nonpop

class
C
{
public
:
blah
;
foo
(
)
;
}
int foo
(
)
{
if
(
oof
)
{
cout
<<
"no way"
;
}
else
if
{
cout
<<
"oh?"
;
}
else
{
cout
<<
"j00 f00"
;
}
do
{
}
while
(
yes
)
;
for
(
int
i
=
0
;
i
<
oof
;
i++
)
{
//blah

}
while
{
//more blag

}
}

Well... actually my coding style is quite similar to yours with a bit more whitespace maybe.

EDIT: There was an error in my code

[edited by - nonpop on May 12, 2004 6:31:17 PM]

[edited by - nonpop on May 12, 2004 6:31:59 PM]

[edited by - nonpop on May 12, 2004 6:33:50 PM]
merlin9x9
merlin9x9
I wish to God that I knew who started the for(int i = 0; i < 10; i++) thing...so I could kick his ass. It's a stupidity that's been perpetuated for some inexplicable reason. Yes, it's often harmless, but that's clearly not the case when we're talking about a type that's not built-in.

[edited by - merlin9x9 on May 12, 2004 6:32:28 PM]
nonpop
nonpop
quote:
Original post by rypyr
i guess i didn''t realize this is a joke thread...

I guess it isn''t. I just had to response to neonoblivion. Actually, I''m already starting to regret it.
Vlion
Vlion
Its not a joke thread. If it was, I would have indicated so.

"for(int i = 0; i < 10; i++)" What is wrong with that?
~V'lionBugle4d
antareus
antareus
Tell your friend to STFU and actually talk about meaningful things (life, women, dreams), not brace placement in a computer programming language.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
SiCrane
SiCrane
There are a lot of ways of doing code formatting. This site lists most of the major ones.
krez
krez
or you could search these forums for the last 100 iterations of this post, which some people replied to and therefore won''t bother this time.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Oluseyi
Oluseyi
quote:
Original post by merlin9x9
I wish to God that I knew who started the for(int i = 0; i < 10; i++ ) thing...so I could kick his ass.
Dennis Ritchie and Brian Kernighan. Go take a look at the examples from the first edition of The C Programming Language.
igni ferroque
igni ferroque
quote:
Original post by Vlion
"for(int i = 0; i < 10; i++)" What is wrong with that?



Yeah... seriously, what''s wrong with that?
Free Mac Mini (I know, I'm a tool)
wild_pointer
wild_pointer
quote:
Original post by igni ferroque
quote:
Original post by Vlion
"for(int i = 0; i < 10; i++)" What is wrong with that?



Yeah... seriously, what''s wrong with that?



Post-increment creates a temporary copy where as pre-increment does not. On built in types it''s not really a big deal, but there''s no reason to ever use post increment on a loop counter. Why it bothers merlin9x9 on such a personal level, i''ll never know.




[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people
[size=2]
Vlion
Vlion
Thanks, SiCrane!
~V'lionBugle4d
igni ferroque
igni ferroque
quote:
Original post by wild_pointer
Post-increment creates a temporary copy where as pre-increment does not. On built in types it''s not really a big deal, but there''s no reason to ever use post increment on a loop counter. Why it bothers merlin9x9 on such a personal level, i''ll never know.


Thanks for clearing that up!
Free Mac Mini (I know, I'm a tool)
Doc
Doc
quote:
Original post by wild_pointer
Post-increment creates a temporary copy where as pre-increment does not. On built in types it''s not really a big deal, but there''s no reason to ever use post increment on a loop counter. Why it bothers merlin9x9 on such a personal level, i''ll never know.



To elaborate, the copy operation involved in post increment may be non-trivial, particularly for things like C++ iterators, thus it may add a level of inefficiency. It doesn''t matter for C, and that''s where the notation originates from.
Shannon Barber
Shannon Barber
quote:
Original post by Vlion
What other styles are there, and why do you prefer them?
Could you give an example, please?



You style is very common in C++.

I ident my braces, which is somewhat less common. And I hate gratitous spaces. i = 0 ; i < n ; + + i is hard to read. Information is contained in deviations, if the spacing is uniform it doesn''t help.


int sum=0;
for(int i=0; i<n; ++i)
{
sum += i;
}


The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Zahlman
Zahlman
I use basically K&R style braces, except I cuddle my elses. 2-space indent, no Hungarian notation. Class names are capitalized; inter-word caps for multi-word names; constants in all caps with underscores between words. Braces on every if/while/for/etc. even for single-line bodies. I''ll put the whole thing on one line if it will fit, though:


try { Thread.currentThread().sleep(SLEEP_TIME); }
catch (InterruptedException ie) { System.out.println("Oh snap!"); }


Oh, and I mostly work in Java, as illustrated by that code. Some of the above is common in the Java "community", but some of it is apparently very strange.

But I tend to think of "coding style" as referring more to coding *idiom* as opposed to code *formatting standard*. For what it''s worth, I use exceptions primarily as a debugging tool, and gradually get rid of them as much as possible (i.e. arrange things so that I can prove that the RuntimeExceptions won''t happen, and then remove the try/catch). I prefer catching the checked exceptions as quickly as possible, and returning error codes or "null objects" (reworking my design to accommodate this, if necessary.) I also like to use static stuff instead of Singletons; a favourite pattern of mine looks like this:

- Base Foo class includes stuff that most people would move to a "FooManager" singleton, all static of course.
- That functionality typically includes abstract factory-type methods.
- A package is created which includes just class Foo and all its subclasses.
- Only the base class is public; so this is basically a Facade pattern as well.
merlin9x9
merlin9x9
quote:
Why it bothers merlin9x9 on such a personal level, i''ll never know.


This is why:
quote:
To elaborate, the copy operation involved in post increment may be non-trivial, particularly for things like C++ iterators, thus it may add a level of inefficiency.


In C, yes, it makes no difference except with a brain-dead compiler that isn''t smart enough to know that you don''t care about the original value. But in C++, it can make a substantial difference, especially in large or nested loops—and it''s such a needless stupidity.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.