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]
🔒 Locked
Different coding styles.
Advertisement
Original Post
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(inti=0;i<oof;i++){}while{}}
:b
[edited by - neonoblivion on May 12, 2004 6:22:41 PM]
- 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 ]
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]
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]
[edited by - merlin9x9 on May 12, 2004 6:32:28 PM]
quote:
Original post by Vlion
"for(int i = 0; i < 10; i++)" What is wrong with that?
Yeah... seriously, what''s wrong with that?
quote:
Original post by igni ferroquequote:
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
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!
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.
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;
}
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:
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.
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.
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.
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.
Advertisement