Thread: What's your indentation style?

  1. #1
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413

    What's your indentation style?

    Artistic Style

    Allman for me, and tabs for indentation.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    I've sort of catered and adapted to how I am told to
    format code at work.

    Code:
    struct Sample
    {
        int x;
        float y;
    };
    
    void functionName(int x, int y)
    {
        /* blah */
    }
    Double Helix STL

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Pep 8
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    For a long time by style was Allman-like, but I'm trying to switch to 1TBS lately. I find it must more compact and readable.
    Devoted my life to programming...

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    What it's calling java style. Although I always though that was k&r
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Guest
    Guest
    Quote Originally Posted by swgh View Post
    Code:
    struct Sample
    {
        int x;
        float y;
    };
    
    void functionName(int x, int y)
    {
        /* blah */
    }
    Ditto.

    Allman style here, with 4 spaces. I tried 2 spaces for a while, but personally found that it made indentation noticeably harder to distinguish. Allman seems to waste vertical space, and some blocks like if-else look weird, but I always disliked the following more:
    Code:
    for (int i = 0; i < 10; ++i) {
        int n = 20; // too close, not separated enough
        someFunction(n);
    }
    Depending on what lines 1 and 2 contain, I feel like there isn't enough to distinguish them, and would wish to add spacing anyway. Allman enforces this.

    Not having a space after control keywords like if also makes these easier to distinguish from the block following them, but it's uncommon to do so.

  7. #7
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by laserlight View Post
    Pep 8
    Lol.

  8. #8
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I use K&R.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Allman with tabs (4 spaces wide) because it makes code clear and easy to read to me. It clearly shows where blocks begin and end, which is the biggest reason I like it.
    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
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Allman. I prefer to have matching brackets in the same column.

    Another stylistic question: Does anyone here ever use spaces to line up text? e.g.

    Code:
    struct x
    {
        int  a;
        char b;
    };
    
    // ...
    
    void function1(void);
    int  function2(void);
    
    // ...
    
        x  = 4;
        y1 = 5;
    ... versus:

    Code:
    struct x
    {
        int a;
        char b;
    };
    
    // ...
    
    void function1(void);
    int function2(void);
    
    // ...
    
        x = 4;
        y1 = 5;

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Matticus View Post
    Another stylistic question: Does anyone here ever use spaces to line up text? e.g.
    Generally, no. But sometimes I have been known to do that. It is rare, 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.

  12. #12
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by Elysia View Post
    Generally, no. But sometimes I have been known to do that. It is rare, though.
    Kinda the same. Sometimes, I'm just so incredibly tempted to do so but then my OCD kicks in and it's just too much work to do it that way so it's either all or nothing which for me usually just equates to, well, nothing.

  13. #13
    Guest
    Guest
    What about function definitions with lots of parameters? (not always avoidable) Do you break the line when the imaginary (80/100/120 character) limit is reached, or do you put each parameter on its own line?

    The insistence to line up code inside parentheses can also get ugly...
    Code:
    threadManager.push(std::thread(&myClass::myMethod, this,
                                   std::cref(someMember.child()),
                                   convert(localVariable1 +
                                           localVariable2)));
    ...whereas applying only regular indentation to the next line would allow almost any expression to fit into two lines.


    And what about in-place lambda definitions:
    Code:
    std::whatever(I.cbegin(), I.cend(), O.begin()
                  [](const auto& a, const auto& b){ /* code here */ });
    Or
    Code:
    std::whatever(I.cbegin(), I.cend(), O.begin(), [](const auto& a, const auto& b)
    {
        // code here, but wasteful for simple expressions
    });
    Last edited by Guest; 08-08-2016 at 02:02 PM.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Guest View Post
    What about function definitions with lots of parameters? (not always avoidable) Do you break the line when the imaginary (80/100/120 character) limit is reached, or do you put each parameter on its own line?
    I put breaks at the imaginary character limit. If a function takes so many parameters, though, I strongly reconsider refactoring it. Often it can be made into a class or a struct that's passed around.

    And what about in-place lambda definitions:
    Code:
    std::whatever(I.cbegin(), I.cend(), O.begin()
                  [](const auto& a, const auto& b){ // code here });
    Or
    Code:
    std::whatever(I.cbegin(), I.cend(), O.begin(), [](const auto& a, const auto& b)
    {
        // code here, but wasteful for simple expressions
    });
    If it's a short lambda, then

    Code:
    std::whatever(I.cbegin(), I.cend(), O.begin(), O.end(), [](const auto& a, const auto& b){ // code here });
    If the lambda won't fit on the line...
    Code:
    std::whatever(I.cbegin(), I.cend(), O.begin(), O.end(),
        [](const auto& a, const auto& b){ // code here });
    If the lambda is multiple lines
    Code:
    std::whatever(I.cbegin(), I.cend(), O.begin(), O.end(), [](const auto& a, const auto& b)
    {
        // code here
    });
    If the lambda won't fit on a single line...
    Code:
    std::whatever(I.cbegin(), I.cend(), O.begin(), O.end(),
        [](const auto& a, const auto& b)
        {
            // code here
        });
    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.

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Matticus View Post
    Another stylistic question: Does anyone here ever use spaces to line up text?
    I do it very often.

    Quote Originally Posted by Guest View Post
    What about function definitions with lots of parameters? (not always avoidable) Do you break the line when the imaginary (80/100/120 character) limit is reached, or do you put each parameter on its own line?
    On my C++ days I wasn't shy of overloading these. If a function signature could be partitioned, it would. But often this meant the function was no very well designed to begin with, since if I can overload it with a different set of parameters, very likely the function was trying to do more than one thing. Recognizing this, I would then move to actually break the initial function into more discreet functions. (two refactorings to finally fix something I should have detected from the start is my statistical mode... grr)

    I can't recall the last time I wrote a function with too many parameters to fit in a 120 character line. But I often have to deal with those when working with a library or when functions ask for certain types of strings. I never multiline the arguments list. For some reason I can't stand the look of it. It's part of the reason I would try to get rid of the ones I wrote myself. But if a function demands that many arguments, or requires a long string, I prefer to initialize variables and pass in their names anytime it results in a shorter caller.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 11-23-2015, 02:55 PM
  2. Indentation style and personality
    By heinz55 in forum General Discussions
    Replies: 21
    Last Post: 11-30-2012, 01:01 PM
  3. Code formatter for indentation, style...
    By learn in forum General Discussions
    Replies: 5
    Last Post: 01-13-2011, 09:42 AM
  4. indentation o indentation!
    By rogster001 in forum C++ Programming
    Replies: 18
    Last Post: 09-23-2009, 05:53 AM
  5. code indentation
    By bbeltwilson in forum C++ Programming
    Replies: 11
    Last Post: 12-30-2008, 11:17 AM

Tags for this Thread