Thread: indentation o indentation!

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    indentation o indentation!

    after recently being moaned at again for indentation i am a bit mystified by this, its just so subjective, no matter what you do always someone will complain they find it hard to read because they are used to something different, basically i go with the indentation defaults that are produced by code::blocks, if i type a for loop and press enter after the opening curly braces the cursor gets indented and i am happy with that, a nested loop will be indented again and so forth, equally when i add the closing brace it automatically tabs it back in line with the opening braces for that statement block, happy days, i dont mind that at all, suits me fine and find it quite readable.

    yet when i posted code with this layout recently i got moaned at by the first reply over dreadful indentation, why???

    I have read loads of code online and in textbooks and have never seen one conventional style being adhered to, barely the broadstrokes concur, as i read in one C++ guide a nice comment about design was 'i am far more interested in shipping product than producing nice design diagrams and methodology and producing perfect indentation that everyone agrees is perfect. far too much is made of this.

    i think this is a nice point!

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    There is style which has symmetry and consistency, and then there's simply bad indentation.

    I am the one that moaned over your indentation, and if you go back and look at your code, you can see that parenthesis are indented by a different amount in different places.

    On top of that, there's stuff like

    Code:
    if(condition)
    { var1 = something; var2 = somethingelse; var3 = foobar; }
    Which can be considered style, but I really don't understand why you would do that, as it's simply horrible to read. Even if you leave the variable assignments on the same line, the code below is just that much easier to read.

    Code:
    if(condition)
    {
         // ... code here ..
    }

    But that's just my opinion, others might disagree. And yes, you are right. Coding style is the subject of much debate. Not only style of indentation, like:

    Code:
    if(condition) {
         // ...
    }
    versus

    Code:
    if(condition)
    {
         // ...
    }
    But also the naming convention of variables. But there is still a well-defined standard for clean and nicely formatted code. I reiterate, the reason for my moaning was mainly the way your parenthesis were indented by a different amount in different places. I suspect that you might be mixing tabs and spaces.
    You might be able to set your editor to replace tabs with spaces - and if you can, you can probably set the amount of spaces it uses.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I don't know.. I've never had any complaints with this indention:

    Code:
    class A
    {
    public: 
        A() { }
    
        void f()
        {
            for(int i = 0, l = 10; i < l; ++i)
            {
                do_something(); // comment
    
                do_something_else();
            }
    
            boost::function<void()> on_something;
        }
    
        void do_something()
        {
    
        }
    
        /**
        * multi-line description
        */
        void do_something_else()
        {
    
        }
    
    private:
        int something;
    };
    People have commented on my choice to not turn a long line into a multi-line (I use a high resolution, its not hard to read 1 line, and it looks better imo), and overusing the "public/protected/private" keyword so it coincides with other languages (Java/C#). Even though it's obvious, and I say don't comment in the first post, they still comment. So when I post code I just convert it to what the community seems to prefer, or at the very least.. very spaced so you can't complain. Meaning turn a long template, constructor, if statement, into 3+ lines.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You are referring to this thread.

    Code:
    for(mainDwn = 0; mainDwn < dwnMax; mainDwn++)                                                       //draw the map
        {
                 for(mainAcr = 0; mainAcr < acrMax; mainAcr++)
                 {
    Note the inconsistency.

    Code:
                     if(mapnode[mainDwn][mainAcr].Walkstate)
                     {   cR = 128; cG = 128; cB = 255; }
    You are not indenting the block here + putting {} on the same line is rather ugly.

    Code:
                                         for(countX = 0; countX < 14; countX++)
                                         {
    
                                             DrawPixel(screen,boxAcr, boxDwn,cR,cG,cB);
                                                   boxAcr++;
                                         }
    Why are the lines in the same block indented differently?

    All in all your indentation looks completely random (you just can't entirely trust the IDE). Another question is, how come the size of each indent is different (e.g why not used 4 spaces for each indent)?

    I think code::blocks comes with a code beautifier (plug-in), even though it might not match your style. For example, I prefer if's and loops like this:

    Code:
    if (x) {
        //...
    }
    and other things like this:
    Code:
    void foo()
    {
        //...
    }
    Now this is a matter of taste.
    Last edited by anon; 09-22-2009 at 03:06 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Yeah and why do you put a space after a comma sometimes but not always? If I had to change your indentation, to contrast, I'd do..

    Code:
    for(mainDwn = 0; mainDwn < dwnMax; ++mainDwn) //draw the map
    {
    	for(mainAcr = 0; mainAcr < acrMax; ++mainAcr)
    	{
    		if(mapnode[mainDwn][mainAcr].Walkstate)
    		{
    			cR = 128; cG = 128; cB = 255;
    		}
    		else
    		{
    			cR = 0; cG = 0; cB = 255;
    		}
    
    		if(mapnode[mainDwn][mainAcr].STnode)
    		{
    			cR = 255; cG = 128; cB = 0;
    		}
    
    		if(mapnode[mainDwn][mainAcr].TGnode)
    		{
    			cR = 0; cG = 255; cB = 128;
    		}
    
    		/*for(countY = 0; countY < 14; ++countY)
    		{
    			for(countX = 0; countX < 14; ++countX)
    			{
    				DrawPixel(screen, boxAcr, boxDwn, cR, cG, cB);
    				++boxAcr;
    			}
    
    			boxAcr -= 14;
    			++boxDwn;
    		}*/
    
    		DrawBox(screen, mainDwn, mainAcr, cR, cG, cB);
    
    		//boxDwn -= 14;
    		//boxAcr += 15;
    	}
    
    	//boxAcr = 0;
    	//boxDwn += 15;
    }
    Putting those cR/cG/cB on the same line is fine unless they do more than simple assignment.

    I wouldn't be happy with those variable names, and I doubt you will be either in a few years, but yeah, indentation is the topic.
    Last edited by Dae; 09-22-2009 at 03:15 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by rogster001 View Post
    a nice comment about design was 'i am far more interested in shipping product than producing nice design diagrams and methodology and producing perfect indentation that everyone agrees is perfect. far too much is made of this.

    i think this is a nice point!
    Wait so he doesn't plan out any parts of his programs before he writes them? I don't think I'd want everyone to be that kind of smart. You only listened to what he said about indenting, right?

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    the reason for my moaning was mainly the way your parenthesis were indented by a different amount in different places. I suspect that you might be mixing tabs and spaces.
    thats true i am guilty of that , usually becuase i think it looks neater sometimes, though millions would disagree...but i usually go around and do tidy ups as i go along, alos sometimes when you wrap the code tags here it flips the original formatting out i notice, maybe thats because the formatting was bad to start with..

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Wait so he doesn't plan out any parts of his programs before he writes them? I don't think I'd want everyone to be that kind of smart. You only listened to what he said about indenting, right?
    haha I am very loosely paraphrasing, i knew it could come over like that, he was basically saying design is very important, but design methodology gets obsessed about too much at times on projects.
    Last edited by rogster001; 09-22-2009 at 03:24 AM.

  9. #9
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Code:
    DrawPixel(screen,boxAcr, boxDwn,cR,cG,cB);
        boxAcr++;
    was a mistake, i normally would not have that.

    mixing the amount of spaces after commas, yea, lazy. although on the whole i always include a space after a comma.

    Code:
    cR = 128; cG = 128; cB = 255;
    this seems fine to me as noted in another reply, these variable are the R,G,B values, so why not have variables on the same line like this when they are related in this way? as long as i dont list fifty of them.
    equally i like to declare them in this way,sets of X and Y coords are related entities, so if i have startX and startY i want them delcared on same line and initialised.

    Thanks all for comments (no pun)

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well I still disagree. If you're going to get into an argument, the best phase to do it in is the design phase. If you really don't care in a situation, then the best thing to do is to learn how to compromise with others, or end an argument, rather than skirting around the whole issue of methodology and design. You'd look stupid. You'd still want to be able to participate in those discussions, because they're important in group work. It's not all just smoke and mirrors.

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    If I had to change your indentation, to contrast, I'd do....
    nice that you took the time to show that example, cheers,

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    cR = 128; cG = 128; cB = 255;
    this seems fine to me as noted in another reply, these variable are the R,G,B values, so why not have variables on the same line like this when they are related in this way? as long as i dont list fifty of them.
    Code:
    cR = 128, cG = 128, cB = 255;
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    As programming requires meticulous thinking and planning to get any real results, I wouldn't even be afraid of saying that when it comes to code formatting, many programmers either have or will end up with a tiny bit of OCD.

    Albeit badly paraphrased, your point was good. Design is more important, and there are numerous and some even famous examples of IT celebs participating in a hefty debate about details as minuscule as the aforementioned spaces after commas in loops and variable definitions, and similar. No, I can't remember any specific examples, sorry. I just remember seeing them.

    Personally, I hate it when my code looks bad. I still think I need to force myself to be more strict on some details, but it's nothing that keeps me up at night.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  14. #14
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    well there are certainly some freudian classics that could be applied yes..but obbsessing over minutae is what it is all about like you say, otherwise no software would be released to start with, not working versions anyhow

  15. #15
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by rogster001 View Post
    well there are certainly some freudian classics that could be applied yes..but obbsessing over minutae is what it is all about like you say, otherwise no software would be released to start with, not working versions anyhow
    I don't think the styles proposed here require an "obsession" with code formatting. It's really not that hard to keep your code tidy and still release software. Sure, you'll make typos, but generally it's easier to code (read and write) if you follow one strict style.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-23-2009, 06:20 PM
  2. Indentation in Code::Blocks
    By PING in forum Tech Board
    Replies: 8
    Last Post: 03-26-2008, 03:33 PM
  3. Fixing the Indentation draft
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 47
    Last Post: 02-23-2008, 11:17 AM
  4. setting indentation in dev-c++
    By richdb in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 06-12-2006, 08:03 PM
  5. indentation error
    By cc870 in forum C Programming
    Replies: 3
    Last Post: 06-24-2005, 11:06 AM