Have I got the hang of indentation?

This is a discussion on Have I got the hang of indentation? within the C++ Programming forums, part of the General Programming Boards category; I like to read other people's code, but the problem is most people don't seem to indent their code so ...

  1. #1
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34

    Have I got the hang of indentation?

    I like to read other people's code, but the problem is most people don't seem to indent their code so it's readable. This encouraged me to start indenting mine in a particular style, and after a bit of experimentation I've came up with this:

    Code:
    #include <iostream>
    using namespace std;
    
    
    void NewLine()
    {
    	cout << endl  << endl << endl;
    }
    
    
    int main()
    {
    	cout << "Enter a number and press enter: ";
    	int n;
    	cin >> n;
    	cin.ignore();
    	
    	for (int i = 1; i <= n; i++) 
    	{
    		cout << i << " ";
    	}
    	
    	cin.get();
    	
    	return 0;
    }
    Here's a tiny program to show you what I mean. I know it's a crap program, and I know I've not commented it (I always do, I just did this for speed) but I'm only a beginner in C++. Is a good way of indenting? Also, is there too much whitespace between the namespace declaration and the start of the function, and between the function and Main?

    NOTE: I know the function does nothing, it's there to show you guys the whitespace I used.

    Thanks for reading, and for any help you can offer.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Looks fine to me. The most important part is to be consistent - every time you see something, you should be able to immediately say what goes on inside and outside any particular block of code (if-statement, for-loop, etc).

    --
    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.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Sure, that looks fine
    There are two articles trying to cover good indentation, as well, if you want a read:
    http://cpwiki.sf.net/Indentation
    http://cpwiki.sf.net/User:Elysia/Indentation
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Is a good way of indenting?
    Sure, as long as you're consistent. But I think you'll find 8 spaces to be a bit wide when you start increasing the nesting or using longer lines. It's generally a good idea to limit line lengths to around 80 characters, and the wider your indentation, the less meat you can fit on a single line.

    >Also, is there too much whitespace between the namespace declaration
    >and the start of the function, and between the function and Main?
    Once again, as long as you're consistent, it's fine.
    My best code is written with the delete key.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's a completely traditional style for indenting (Allman style--I had to look that up). I think the 8-space tabs are going out of favor, with the 4-space being more prevalent, but that's not going to bother anybody.

    And there's nothing wrong with extra whitespace between functions (I don't usually do it personally, but it can help if you've got a lot of short functions).

  6. #6
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34
    Thanks for the very fast replies! Consitency is what I'm aiming for, if I had to read my own code from somebody elses point of view (if that makes sense) the last thing I would want is any nasty surprises.

    Thanks for the links Elysia, I'll start reading them.

    EDIT: Regarding the indentation, I use SCiTE editor, and the tabs it uses is what I keep. When posted here the gap looks so much wider, like I've pressed tab twice instead of once but in the editor the gap is only one tab wide.
    Last edited by Caduceus; 01-25-2008 at 09:25 AM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Quote Originally Posted by Prelude View Post
    >Is a good way of indenting?
    Sure, as long as you're consistent. But I think you'll find 8 spaces to be a bit wide when you start increasing the nesting or using longer lines. It's generally a good idea to limit line lengths to around 80 characters, and the wider your indentation, the less meat you can fit on a single line.
    But then again, the OP uses tabs, and the browser likes to set tab width to 8 spaces. But in IDEs it's very easy to set it to 4 spaces and it's standard in some IDEs like Visual Studio, so I see no problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    But then again, the OP uses tabs, and the browser likes to set tab width to 8 spaces. But in IDEs it's very easy to set it to 4 spaces and it's standard in some IDEs like Visual Studio, so I see no problems.
    That's right - but if you MIX tabs and spaces, you get funny behaviour.

    --
    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.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Indeed, the golden rule...
    Still, pretty much everything about indentation should be covered in those two articles.
    It's nice to see someone actually caring for indentation for a change!
    And it's also very nice to see someone else who uses Allman styles w/ tabs! My favorite!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Beginner in C++
    Join Date
    Dec 2007
    Posts
    34
    I didn't actually know it had a name, I just used my common sense to see what looked best and easiest to read ^_^

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    There are at least 4 coding styles that are named. They are 4 of the most used coding styles.
    Why they have names, I don't really know... I don't know the history.
    But Allman is definitely one of the most used coding styles used.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >They are 4 of the most used coding styles.
    Were. At this point it seems that only two of them are still in common use (variations of K&R and Allman) and the other two are falling from favor. At least, that's assuming you're referring to K&R, Allman, Whitesmith, and GNU. This is basically what they look like:

    K&R:
    Code:
    #include <iostream>
    
    int main() {
      for ( int i = 0; i < 10; i++ ) {
        std::cout<< i <<'\n';
      }
    }
    Allman:
    Code:
    #include <iostream>
    
    int main()
    {
      for ( int i = 0; i < 10; i++ ) 
      {
        std::cout<< i <<'\n';
      }
    }
    Whitesmith:
    Code:
    #include <iostream>
    
    int main()
      {
      for ( int i = 0; i < 10; i++ )
        {
        std::cout<< i <<'\n';
        }
      }
    GNU:
    Code:
    #include <iostream>
    
    int main()
      {
        for ( int i = 0; i < 10; i++ )
          {
            std::cout<< i <<'\n';
          }
      }
    >Why they have names, I don't really know... I don't know the history.
    K&R was the style used in The C Programming Language, so a whole generation of C programmers picked it up from their best (and probably only) bible on the language.

    Allman was named after the guy who used it in a lot of BSD utilities, and because C programmers then liked to learn by reading the source on their system, they picked it up.

    Whitesmith was named after the compiler that used the style in it's example code. I'm not sure how popular it was, but I'm guessing it had enough of a rebellious feel that the hippies picked it up.

    GNU was named because it was used (almost exclusively) in Richard Stallman's code and other FSF source. You can imagine why people might pick it up, but it's awkward to use, so I think most people dropped it just as quickly.

    FYI, I use a variation of K&R:
    Code:
    #include <iostream>
    
    int main()
    {
      for ( int i = 0; i < 10; i++ ) {
        std::cout<< i <<'\n';
      }
    }
    According to my observations, programmers who started with C tend to use K&R and programmers who started with C++ tend to use Allman. I'm not sure why.
    My best code is written with the delete key.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Yeah, those 4 are the ones I know. I've seen K&R most, and then a few Allman, and once or twice perhaps the GNU and the rest just mumbo-jumbo.
    At least this is what I've witnessed on the boards. I can't remember other styles from back before, and by then, I didn't know some were named.
    I use Allman, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-23-2009, 07:20 PM
  2. C# and the WebClient class - application hang?
    By Devils Child in forum Networking/Device Communication
    Replies: 1
    Last Post: 01-09-2009, 11:24 AM
  3. Indentation in Code::Blocks
    By PING in forum Tech Board
    Replies: 8
    Last Post: 03-26-2008, 04:33 PM
  4. Fixing the Indentation draft
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-23-2008, 11:17 AM
  5. setting indentation in dev-c++
    By richdb in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-12-2006, 09:03 PM

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