CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27
  1. #16
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Opinions about programming styles

    Quote Originally Posted by Lindley View Post
    Generally, spaces are a better choice [...]
    what I dislike of space-filled "tabs" is when the cursor gets "trapped" in the indented area and you want to move to a specified indentation level ... in the editors I've used, is a frustrating experience

    Quote Originally Posted by Lindley View Post
    [...] because they allow more customization of indent levels.
    do you mean different sizes for each indentation level ? what's the benefit ?

  2. #17
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Opinions about programming styles

    Quote Originally Posted by superbonzo
    do you mean different sizes for each indentation level ? what's the benefit ?
    I do not presume to know exactly what Lindley meant, but one advantage of the use of spaces for indentation is illustrated by Moschops' example in post #4:
    Code:
    void clicked_cb(ClutterClickAction* action,
                    ClutterActor*       actor, 
                    gpointer            user_data)
    Spaces are likely to be used to align the second and third parameters with the first since if tabs were used, there is the risk of misalignment should the tab width be different from what is expected. If this were a member function, then the declaration would likely be indented. So, if tabs were used for indentation, then we would have one or more tabs followed by the spaces used for alignment, which is a Bad Thing since it a mixture of tabs and spaces that requires more care to get right.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #18
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Opinions about programming styles

    Another advantage of spaces over tabs is portability. Some editors consider 1 tab = 2 spaces, while others consider 1 tab = 4 spaces (and some even consider 1 tab = 8 spaces).

    As long as spaces and tabs are not mixed, then it *should* be fine, but they are always mixed...

    The ONLY thing (I can think of) that tabs have going for them is size in terms of bytes, but in this day and age, I'd say it's largely irrelevant
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #19
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Opinions about programming styles

    Quote Originally Posted by laserlight View Post
    I do not presume to know exactly what Lindley meant, but one advantage of the use of spaces for indentation is illustrated by Moschops' example in post #4:
    Code:
    void clicked_cb(ClutterClickAction* action,
                    ClutterActor*       actor, 
                    gpointer            user_data)
    I find that aligning variable names like this makes the code much more difficult to read.
    It's also a horrible style for writing code if the editor does not do this automatically. When you add a variable to the parameter list, you don't want to waste time having to fix the alignment of the other variable names.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  5. #20
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Opinions about programming styles

    It's not something to be done all the time, but now and then it can help a lot.

  6. #21
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Opinions about programming styles

    Quote Originally Posted by laserlight
    Moschops' example in post #4:
    Hmm... my mistake. I see that this is actually JohnW@Wessex's example in post #3, quoted by Moschops in post #4.

    Quote Originally Posted by D_Drmmr
    I find that aligning variable names like this makes the code much more difficult to read.
    I find it quite readable, though I do not do such alignment myself. What could be awkward is when there is a parameter with a short type name and another with a long type name, so there ends up with a large gap in between the type name and parameter name for the parameter with a short type name. Note that my citing of this example has to do with the alignment of the parameters themselves, in effect meaning the alignment of the type names.

    Quote Originally Posted by D_Drmmr
    It's also a horrible style for writing code if the editor does not do this automatically. When you add a variable to the parameter list, you don't want to waste time having to fix the alignment of the other variable names.
    Even if an editor does not do this automatically, it may provide vertical selection that allows the alignment of all the parameters to be changed simultaneously.
    Last edited by laserlight; January 27th, 2012 at 12:04 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #22
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Opinions about programming styles

    Quote Originally Posted by D_Drmmr
    When you add a variable to the parameter list, you don't want to waste time having to fix the alignment of the other variable names.
    I used to use the format in conjunction with a code formatter that automatically aligned the parameters for me.

    Quote Originally Posted by laserlight
    What could be awkward is when there is a parameter with a short type name and another with a long type name,
    That can look a little odd, though it was not a common occurance.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  8. #23
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Opinions about programming styles

    I am a frequent participant in "tabs vs spaces" discussions
    I am writing code for almost 30 years, in various languages, for multiple companies and as a sole developer.
    Can we limit the arguments to C++ in a modern editors, for simplicity?
    Here are some assumptions that I think are fair:
    1. The code is written (and, particularly, formatted) for the reader, and he is likely NOT you.
    Conclusion: your code should be easily adopted to the reader's taste. Hint: it is seamlessly achieved when you use TABs.
    And a bit less obvious:
    2. All white space at the beginning of the line is INDENTATION, the white space in a middle might be alignment.
    The reasoning here is that by scanning the left edge of the code you can deduce its logical structure. That, of course, is unless you use your code as ASCII art.
    Conclusion: NO alignment, at least - not at the beginning of the code line.

    And here is my suggestion for the above code example (for people who like alignment):
    Code:
    void clicked_cb(
        ClutterClickAction* action,
        ClutterActor*       actor, 
        gpointer            user_data)
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #24
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Opinions about programming styles

    Quote Originally Posted by VladimirF View Post
    1. The code is written (and, particularly, formatted) for the reader, and he is likely NOT you.
    Conclusion: your code should be easily adopted to the reader's taste. Hint: it is seamlessly achieved when you use TABs.
    Counterpoint: If the code is being viewed in multiple environements, for instance both emacs and Visual Studio's editor, and perhaps a version control system's built-in viewer, it is best if it looks the same everywhere. This is most easily achieved when you use SPACES.

  10. #25
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Opinions about programming styles

    Quote Originally Posted by Lindley View Post
    Counterpoint: If the code is being viewed in multiple environements, for instance both emacs and Visual Studio's editor, and perhaps a version control system's built-in viewer, it is best if it looks the same everywhere.
    Oh, that is a frequent, but not a very strong point at all!
    Sorry, I am not familiar with emacs.
    But *I* am reading the code in the version control system, and I sure want it to be formatted to *MY* liking.
    SVN (Tortoise) can to it. Perforce could too. Which one can't? May be you should reconsider your choice of software?
    Caveat: I do know one exception: the web browsers. They either can't, or I simply didn't find a way to specify the size of the TAB in spaces...
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  11. #26
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Opinions about programming styles

    Quote Originally Posted by VladimirF View Post
    Conclusion: your code should be easily adopted to the reader's taste. Hint: it is seamlessly achieved when you use TABs.
    While I'd agree, that would require an incredible amount of rigor on the part of all developers: 1 single developer who uses spaces where tabs are needed can mess up your indentation.

    I am working on a project: Our team here, and another team overseas. We like 2 spaces per indentation. They like 4 spaces per indentations. tabs are the bane of our existence.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  12. #27
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Opinions about programming styles

    Quote Originally Posted by monarch_dodra View Post
    1 single developer who uses spaces where tabs are needed can mess up your indentation.
    Just shoot him then
    That's what Coding Standards are for!
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured