indenting code - different styles

This is a discussion on indenting code - different styles within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Im new to C and one of things my lecturer stresses out is that indenting is important, and marks will ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    104

    indenting code - different styles

    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 ?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    104
    Just an example from the wiki article , is this bad indentation ?
    Code:
    for(i = 0; i < 10; i++) {
         if(i &#37; 2 == 0) {
             doSomething(i); }
         else {
             doSomethingElse(i); } }

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    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.
    Last edited by MacGyver; 09-08-2007 at 02:18 PM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by ICool View Post
    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.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by Salem View Post
    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:

    Code:
                    int main()
                {
            int i;
    
            for(i = 0; i < 10; i++)
        printf("%d\n", i);
                }
    Consistent, but the indentation goes leftward instead of rightward.

  7. #7
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    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):
    Code:
    start_of_block
    {
        ....
    } //end of block
    I 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.

  8. #8
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,533
    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.)
    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.
    Last edited by Yarin; 09-08-2007 at 07:07 PM.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by Yarin View Post
    Example of my coding style... (I prefere 3 spaces, no tabs)
    Wow. Somebody else does 3 spaces!

    I salute that.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    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.
    My Website

    "Circular logic is good because it is."

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    The Right Honourable psychopath's Avatar
    Join Date
    Mar 2004
    Location
    Where circles begin.
    Posts
    1,070
    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.

    Quote Originally Posted by matsp
    There is hungarian notation
    *hiss*
    Memorial University of Newfoundland
    Computer Science

    Mac and OpenGL evangelist.

  14. #14
    Registered User
    Join Date
    Jul 2007
    Posts
    88
    Well, this is an interesting thread.

    This 1)
    Code:
    ptr = static_cast <int *> ( realloc(ptr, sizeof(*ptr)) );
    is far better readable for me then this 2)
    Code:
    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.

  15. #15
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    I prefet single-space indenting and I prefer to use as little space as possible
    Code:
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Fixing the Indentation draft
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-23-2008, 10:17 AM
  3. Obfuscated Code Contest: The Results
    By Stack Overflow in forum Contests Board
    Replies: 29
    Last Post: 02-18-2005, 04:39 PM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21