PDA

View Full Version : indenting code - different styles



ICool
09-08-2007, 02:13 PM
Im new to C and one of things my lecturer stresses out is that indenting is important, and marks will be awarded for presentation. I had a brief look at this wiki article :
http://en.wikipedia.org/wiki/Indent_style
And it shows a lot of different styles. My question is which one should I , the beginner adopt and which one is used most frequently ? Is white space good ? Shoud I use tabs only or tabs and spaces ?

ICool
09-08-2007, 02:16 PM
Just an example from the wiki article , is this bad indentation ?


for(i = 0; i < 10; i++) {
if(i &#37; 2 == 0) {
doSomething(i); }
else {
doSomethingElse(i); } }

MacGyver
09-08-2007, 02:16 PM
You're asking a question there is no real definitive answer to. No one on this board should care what indentation style you use when you post code here provided that you stay consistent. If you mix and match styles with no reason, then people will assume you're just sloppy. If you care about my personal opinion, use only the first or second one provided in the wikipedia article.

We had a poll here on tabs vs spaces. More people chose spaces, but they couldn't agree on the number of spaces. End result: Use whatever you want.

Salem
09-08-2007, 02:23 PM
> is this bad indentation ?
Yes, closing braces should line up at the indentation level they're meant to be closing off.
But then again, that particular style isn't listed on wiki.

But MacGyver is correct, nobody will care so long as you''re consistent in whatever style you choose.

brewbuck
09-08-2007, 03:23 PM
Im new to C and one of things my lecturer stresses out is that indenting is important, and marks will be awarded for presentation. I had a brief look at this wiki article :
http://en.wikipedia.org/wiki/Indent_style
And it shows a lot of different styles. My question is which one should I , the beginner adopt and which one is used most frequently ? Is white space good ? Shoud I use tabs only or tabs and spaces ?

The only real rule I adhere to is: "Indent like the rest of the program is indented." If you are writing your own code, pick a reasonable style and stick to it. If you are working on somebody else's code, indent how they do.

If you have no idea what "reasonable" means, spend some time looking at other people's GOOD code.

brewbuck
09-08-2007, 03:25 PM
But MacGyver is correct, nobody will care so long as you''re consistent in whatever style you choose.

I'm sure there are ways to be irritating and consistent at the same time. Imagine:



int main()
{
int i;

for(i = 0; i < 10; i++)
printf("%d\n", i);
}


Consistent, but the indentation goes leftward instead of rightward.

@nthony
09-08-2007, 05:10 PM
I prefer what they call the "Allman" style (though I've always known it as the Java or OO style as nearly all OO code uses it):
start_of_block
{
....
} //end of blockI switched to this style from using K&R because it is more easy to find during scanning where your blocks start and end, minimizing the "missing brace" headaches; however your whitespace count goes up considerably (which is why I had been using K&R up until then, but then realized I rather have 200+ lines of whitespace than 200+ lines of potential headache).
Also, as you can see I've used 4 spaces to indent there, usually anywhere between 2 and 8 are good to use as indents.

Yarin
09-08-2007, 06:59 PM
I suggest you find a style that you like and can easily read; regardless if it has a name or is in wiki.

Here's an example of my coding style. I prefere 3 spaces, no tabs, so that I don't have to touch the horz scroll bar.
I like to have the brackets on their own line (with a few exceptions) making it more open. (I don't like clutered code.)


// By ME in August of 2007.

char* TestFunction(DWORD dwAmount)
{
char *pszRet = new char[dwAmount];
int a;
for(a = 0; a < dwAmount; a++)
{
if(a &#37; 2 == 0)
pszRet[a] = '#';
else
pszRet[a] = '@';
}
return pszRet;
}

int main(void)
{
MessageBox(0, TestFunction(5), "Test", MB_ICONINFORMATION);
return 1;
}

// End of file.

brewbuck
09-08-2007, 07:02 PM
Example of my coding style... (I prefere 3 spaces, no tabs)


Wow. Somebody else does 3 spaces!

I salute that.

laserlight
09-08-2007, 07:32 PM
Consistent, but the indentation goes leftward instead of rightward.
Actually, after reading that a couple of times I become fine with it. The only catch is that it is probably a pain to write without special editor support (you need to re-indent everything every time you introduce a new indent level).

DavidP
09-09-2007, 12:34 PM
I code in the Allman style, although to be quite honest I never knew it had a name until today, when I just read that Wikipedia article. The styles I have seen most often are K&R and Allman.

I like Allman because I like whitespace. I believe that whitespace should be used liberally (as well as comments in code), so that someone can very easily look at your code and understand what is going on. I think the Allman style is best suited for that.

matsp
09-10-2007, 06:07 AM
I agree with DavidP, whitespace is good.

Once you start programming in larger organization, you'll probably find that the company or group mandates the style to some degree. Some companie/groups will have a very basic set of rules, others will state that your naming standard, as well as indentation rules, and perhaps also how and when certain constructs should be used.

Obviously, if you have to add/modify code to an existing project, the idea is to "fit in", rather than write your own style, as has already been said.

If you are writing your own code, it should, as has also been said, be consistent, because that makes it easier to read and follow, which is the entire goal of indentation and other text/code-formatting. This applies to native languages as well - there is a section of about 200 pages in James Joyce's Ulysses that has no punctuation or capitalization [at least not used to mark beginning of new sentence] at all. It is MUCH harder to read than text with punctuation and capitalization.

Part of your style will be naming variables, the use of upper/lower case characters, as well as how you use whitespaces to indicate what part of code belongs with which. For example, a variable can be called "n", "name", "Name", "NameOfPersonInstallingSoftware", etc. "n" is obviously less informative than "NameOfPersonInstallingSoftware", but the latter may actually become hard to use too, sicne it's so long. The alternative of "nameofpersononstallingsoftware" is obviously much less readable. Using a good average between "excessively long" and "too short" names is what you want to aim for. Depending on the circumstances, the right answer will vary quite a bit. If you have many variables in a function, you may need longer names, more descriptive names, compared to a function that is very short, and the concept is easy to grasp without further naming hints.

There is hungarian notation, which is used in Windows, where the first part of the name is indicating the type of variable, e.g. pszName would tell us (without looking at the declaration) that this is a "Pointer to String that is Zero terminated", and it's the name of something, pRectRegion would be a "pointer to Rectangle type", and that it's a region.
http://en.wikipedia.org/wiki/Hungarian_notation

You may also have a look at "Programming Style" to get a bit more info.
http://en.wikipedia.org/wiki/Programming_style

A good style is helpful for others to read your code easier.

--
Mats

psychopath
09-10-2007, 07:10 AM
I usually code in K&R, but I never knew that's what it was called untill a little while ago. I wrote in allman style when I first started to learn C++ though.


There is hungarian notation
*hiss*

sept
09-10-2007, 08:08 PM
Well, this is an interesting thread.

This 1)
ptr = static_cast <int *> ( realloc(ptr, sizeof(*ptr)) ); is far better readable for me then this 2)
ptr=static_cast<int*>(realloc(ptr, sizeof(*ptr))); This might have a physical reason, my eyes seam like it more easy with more spaces.

Most of the time with 1) I need to add spaces to be able to understand the source better. But I also try to force me to understand that style too.

For nuances for if, else and such I use tabs. Imho tabs are much more easy to write for me (using visual studio). It`s also more easy to change the nuances if you use tabs (in case of removing one of multiple ifs for example)

Question:
Is there already some program which can change the style of the source? Like 1) to 2) would be awesome.

Blackroot
09-10-2007, 08:44 PM
I prefet single-space indenting and I prefer to use as little space as possible


int main() {
return 0;
}


It's really just a matter of what you like, legibility is important though, if you have big blocks of code it's sometimes hard to read without good indentation.

Yarin
09-10-2007, 10:18 PM
This 1)
ptr = static_cast <int *> ( realloc(ptr, sizeof(*ptr)) ); is far better readable for me then this 2)
ptr=static_cast<int*>(realloc(ptr, sizeof(*ptr)));
I dunno. I would say a balance is important for any coding style. I would write that like this

ptr = static_cast<int*> (realloc(ptr, sizeof(*ptr)));

Cynic
09-10-2007, 11:44 PM
Im new to C and one of things my lecturer stresses out is that indenting is important, and marks will be awarded for presentation. I had a brief look at this wiki article :
http://en.wikipedia.org/wiki/Indent_style
And it shows a lot of different styles. My question is which one should I , the beginner adopt and which one is used most frequently ? Is white space good ? Shoud I use tabs only or tabs and spaces ?

Which one does he want to see? Cater to him, because he's giving the grade.

Salem
09-11-2007, 12:14 AM
> For nuances for if, else and such I use tabs. Imho tabs are much more easy to write for me (using visual studio).
> It`s also more easy to change the nuances if you use tabs (in case of removing one of multiple ifs for example)
1. If you post code to a forum / mailing list, the result is almost always "what we get is NOT what you see". This is even more true if you've set the IDE to be something other than 8 spaces.
2. The IDE is smart enough to figure out indent. You can for example highlight a whole block of code and press tab to indent one level and shift-tab to undent one level.
Coupled with auto indent as you type, most of the time it isn't a problem at all.

> Is there already some program which can change the style of the source? Like 1) to 2) would be awesome.
Loads of them
http://clusty.com/search?query=code+beautifier&sourceid=Mozilla-search
http://clusty.com/search?query=code+formatter&sourceid=Mozilla-search
If you have cygwin / Linux, then
man indent
or
info indent

> Which one does he want to see? Cater to him, because he's giving the grade.
Absolutely.
The first style is that of whoever is giving the marks / writing the pay cheque.

novacain
09-11-2007, 03:25 AM
I have two styles.....

The style I use at work. I write assuming someone will have to fix the bugs in the code a few months/years down the track (train pun...).
I want to make my intent as clear as possible, the code as readable as I can and so (hopefully) make the fix easier. I even put braces around single lines of code. I use 'allman'.

The style I use when I write code that _only_ I will read again. I write it as fast as possible with few comments and no spaces.