Thread: Indenting...

  1. #1
    Programmer
    Join Date
    Aug 2005
    Location
    Ohio
    Posts
    8

    Indenting...

    I was wondering...what is the proper method for indenting your code?

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    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
    }
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    Programmer
    Join Date
    Aug 2005
    Location
    Ohio
    Posts
    8
    What's K&R? When I learned c++ from c++ for dummies it didn't have any system to help me indent.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > 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.
    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.

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    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:
    Code:
    void foo(int bar)
    {
       int q = 9;
       
       for(int i = 0; i != bar; ++i)
       {
          q += bar;
       }
    }
    c) Try to leave blank lines between separate "units" within a function. This helps improve clarity.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    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
    That's pretty much how i do it.

    I agree with Salem though, how you indent doesn't matter as long as it is consistent and easy to read.

  7. #7
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Quote Originally Posted by dra
    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

    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"

  8. #8
    He's trying.
    Join Date
    Apr 2005
    Location
    Missouri, US
    Posts
    70
    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.

  9. #9
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I don't indent my functions, though.
    When you write more and more complex functions, you'll probably be inclined to indent.

    I drop the brackets when I have a one-line if or for
    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:
    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;
    }
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  10. #10
    Weak. dra's Avatar
    Join Date
    Apr 2005
    Posts
    166
    Quote Originally Posted by ILoveVectors
    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"

    awww. lol.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.
    My best code is written with the delete key.

  13. #13
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    One thing I just hate is:

    Code:
    int someFunc(char a)
    {
          switch(a)
          {
          case 'a':
                stuff();
          }
    }
    And:
    Code:
    class SomeClass
    {
    public:
          SomeClass()
    private:
          int a;
    }
    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

  14. #14
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Access specifiers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error C2106: '=' : left operand must be l-value
    By sobieski19 in forum C Programming
    Replies: 33
    Last Post: 02-08-2008, 04:43 AM
  2. Saving number Output
    By Apocalyptic_end in forum C++ Programming
    Replies: 37
    Last Post: 01-26-2008, 05:16 PM
  3. Gvim stopped auto indenting
    By ICool in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-26-2007, 12:19 PM
  4. indenting code - different styles
    By ICool in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 09-11-2007, 03:25 AM
  5. Multiple problems with my program
    By Seirei in forum C Programming
    Replies: 4
    Last Post: 02-17-2006, 10:15 PM