code indentation

This is a discussion on code indentation within the C++ Programming forums, part of the General Programming Boards category; so im working on a super mini text based game and i posed becuase i had an issue i was ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    6

    code indentation

    so im working on a super mini text based game and i posed becuase i had an issue i was told i need to properly indent my code. iv been looking around and i cant find anything on how to indent your code. does anyone know somewhere i can learn how to do it properly.
    Last edited by bbeltwilson; 12-28-2008 at 04:13 PM.

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    http://en.wikipedia.org/wiki/Indent_style
    There are several different styles- which you choose is not as important as using it consistently.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    70
    Any good IDE will indent correctly for you.

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    6
    yay i can indent

    thanks guys

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    I kind of get annoyed by certain programmers with large tab indentation.
    I like keeping it at a Tab: 2 setting, for 2 spaces as a tab. But other programmers like excessive spaces and indentation.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  6. #6
    Why am I a programmer? shoutatchickens's Avatar
    Join Date
    Mar 2008
    Posts
    45
    I personally find 4 spaces a bit easier to read. I tend to use 2 to indent code items that are the same "line" of code.

    But usually as long as it's consistent and each line is not longer than 80 or so chars it's should be readable.
    Last edited by shoutatchickens; 12-29-2008 at 03:57 PM.
    One of the disadvantages of being a 22 year old RPG programmer is having to repeatedly explain to your friends that you don't make videogames for a living.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    I am with shoutatchickens on this. 2 is too short, 4 is good, 8 is too much.
    But this is all opinions; there is no correct answer.
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by execute View Post
    I kind of get annoyed by certain programmers with large tab indentation.
    I like keeping it at a Tab: 2 setting, for 2 spaces as a tab. But other programmers like excessive spaces and indentation.
    Smaller indentation levels encourage deeper nesting, since more code can fit on a line without wrapping. In a way, larger indentation encourages code modularity by making it obvious when the nesting gets too deep. Small indents also make it harder to scan with your eye and match up opening and closing braces.

    For ten years I worked at a shop that used 3 spaces for each indent level. Now I'm in a shop which uses tabs, but sets the tab stops to 4 cells in the editor. Open the code in an editor with tabs set to 8 spaces, and it flows off the page.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    A good practice is also to start coding with the last line of code written first and work you way up. That way you will know what the result of the operation will be right away and you don't need to write lengthy blocks of code to simply get the result of a operation.
    home = hat.position;

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Really? That seems counter-intuitive, seeing as you will spend far more time thinking backwards and writing it out and getting it more wrong than right.
    No, a better approach, I think, is to write it the other way and then take a peek at it once it is finished and clean it up a little and so.
    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.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    158
    Quote Originally Posted by Elysia View Post
    Really? That seems counter-intuitive, seeing as you will spend far more time thinking backwards and writing it out and getting it more wrong than right.
    No, a better approach, I think, is to write it the other way and then take a peek at it once it is finished and clean it up a little and so.
    Sometimes with jokes on the internet you can never be quite sure if they are actually jokes or not. Actually I think one should go for negative indention, -8 should be standard.(this was a joke)
    home = hat.position;

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,704
    Well, I'm not being funny, but even as a statement, it isn't so absurd to think about the output of an operation before you start writing it. After all, programming without the slightest idea of what you want is misdirected, if possible at all. In general, your thought process during the design phase of a program is output->input->process; though process usually takes the longest to think about. The rather straight forward input->process->output only occurs during execution.

    As for writing a return statement before the rest of the function or whatev, I don't think it makes a significant difference either way, as long as program flow is ok. By the time you actually code, you've already specified the functions you need. Besides, it's a statement that shouldn't need debugging. How often does that really occur?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing the Indentation draft
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-23-2008, 10:17 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

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