I was wondering...what is the proper method for indenting your code?
Printable View
I was wondering...what is the proper method for indenting your code?
I always indent 8 spaces, and generally follow K&R as far as bracket placement goes.
Code:void somefunction()
{
// hmm
}
if (a > b) {
// hmmmm
} else {
// hmmmmmmmmmm
}
What's K&R? When I learned c++ from c++ for dummies it didn't have any system to help me indent.
> what is the proper method for indenting your code?
Personally, I don't really care so long as it is neat and consistent.
What I can't stand is people posting code with either no indentation at all, or code indented by falling asleep on the keyboard.
Unless you work for someone with explicit coding standard, the choice is your's, but the neater it is, the easier it will be for you to work on (now and after you've left it for weeks, months, etc), and the more willing people will be to help you. Some guidelines (and only that):
a) Some common choices for how much each indent should be are 3, 4, and 8. 1 and 2 spaces, however, still leaves your code looking like a rat's nest.
b) A matching set of braces on the same indentation level, and everything inside indented from them:
c) Try to leave blank lines between separate "units" within a function. This helps improve clarity.Code:void foo(int bar)
{
int q = 9;
for(int i = 0; i != bar; ++i)
{
q += bar;
}
}
Cheers
That's pretty much how i do it.Code:while ( i != 0 ){ //open brace after the parenthesis
//code under the opening parenthesis
}//closed brace under the first letter of the keyword it "belongs" to
I agree with Salem though, how you indent doesn't matter as long as it is consistent and easy to read.
Quote:
Originally Posted by dra
no offense but i find in alot of code thsi way is tacky and hard
to read, i think you should drop the brackets to a new line like so
Code:while ( i != 0 )
{ //open brace after the parenthesis
//code under the opening parenthesis
}//closed brace under the first letter of the keyword it "belongs"
The system ILV uses looks silly with one line of code (I drop the brackets when I have a one-line if or for), but it's what I use too for control statements.
I don't indent my functions, though. Mostly seems like an unnecessary level to me, although I guess it'd be nice for another cue when functions start & end - as of now I just use lots of vertical whitespace.
Edit: Whitespace between different functions, that is...although I use a good amount inside my functions, too.
When you write more and more complex functions, you'll probably be inclined to indent.Quote:
I don't indent my functions, though.
I often do the same, although braces can be helpful. At any rate, here is a small piece of code that points to where braces for "one-liners" may be useful, and if nothing else, shows very clearly why good indentation is necessary:Quote:
I drop the brackets when I have a one-line if or for
Code:#include <iostream>
int main()
{
for(int i = 0; i != 10; ++i)
for(int j = i; j != 10; ++j)
if(i + j == 7)
std::cout << "i + j == 7" << std::endl;
else if(i + j > 17)
std::cout << "i + j > 17" << std::endl;
else
std::cout << "something else" << std::endl;
}
Quote:
Originally Posted by ILoveVectors
awww. :( lol.
There is no standard convention for C++, but Java's recommended convention uses dra's style.
http://java.sun.com/docs/codeconv/ht....doc6.html#430
The most important thing to realize is that this is a holy war topic, so everyone has a different opinion, and everyone thinks theirs is the only "true" way. As long as it's formatted consistently, anyone can figure out your code with minimal effort, so just adopt a style and make sure that if you use it in a program, don't stray from it. Anyone who claims that your code is hard to read simply doesn't try very hard to read anything but their own code.
One thing I just hate is:
And:Code:int someFunc(char a)
{
switch(a)
{
case 'a':
stuff();
}
}
I just feel it very necessary to indent the switched case's and the, um, "class member type specifiers" for lack of a real term ;)Code:class SomeClass
{
public:
SomeClass()
private:
int a;
}
Access specifiers ;)