Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. A Question of Style

A Question of Style

Scheduled Pinned Locked Moved The Lounge
question
67 Posts 29 Posters 63 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Peter Mulholland

    I used to be a tab fan, cause of the one backspace instead of 4. But over the last few years i've opened too many code files that have been edited by several people. Even when there's a 'standard' within a team to use 4 spaces or use tabs or whatever, some people insist on being different. If you can get everyone using spaces then there's a better chance that your indentation will stay aligned after several people have edited the same code. IMHO

    Pete If minds had anuses, blogging would be what your mind would do when it had to take a dump Maddox

    P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #46

    Peter Mulholland wrote:

    If you can get everyone using spaces then there's a better chance

    Yes, that's the crux, everyone has to follow the standard, whatever it is. However, it seems to me that the easier standard to follow is to use SPACEs, particularly when various editors or code generators are used.

    1 Reply Last reply
    0
    • H Henry Minute

      Mike Marynowski wrote:

      that's the beauty of shift+tab

      The problem is, at least at first, remembering the shift part, the thing can end up looking like a dogs breakfast if you are a non-touch typist.

      Henry Minute If you open a can of worms, any viable solution *MUST* involve a larger can.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #47

      Henry Minute wrote:

      a non-touch typist

      Like me? I never heard about Shift-Tab until that post, I just use Delete, no big deal.

      1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        What, if any, is the practical reason for preferring spaces over tabs? I use tabs because when I need to increase or decrease an indent, it's only a single character that must be added or deleted. I noticed that the Google Guidelines disallow tabs, and use only spaces. What have they got against tabs?

        J Offline
        J Offline
        Jim SS
        wrote on last edited by
        #48

        Personal preference is tabs (3 spaces), but I am glad that other people have other preferences. I can almost always tell my code from others because of formatting. I got used to compacting my code when we only had 80x25 green screens so I could see more code on the screen at a time. I like:

        if (...) {
        ...;
        } else {
        ...;
        }

        others always do:

        if (...)
        {
        ...;
        }
        else
        {
        ...;
        }

        or:

        if (...)
        {
        ...;
        }
        else
        {
        ...;
        }

        It makes it easier to spot my own code and what may have been modified by me or others. I had a friend that could spot my code anywhere. He always claimed that I could write a whole program on a single line. :rolleyes:

        SS => Qualified in Submarines "We sleep soundly in our beds because rough men stand ready in the night to visit violence on those who would do us harm". Winston Churchill

        P 1 Reply Last reply
        0
        • J jsc42

          I used to use 3 spaces for indenting when I worked with punched cards and paper tape. That way, you could still have reasonable levels of nesting within the 80 chars on a card. Now that I have disc files, I use the TAB key - it is only one key stroke (which saves on storage) and it does not matter so much if I exceed 80 chars per line. I even allow myself to reach the 80th column when writing comment lines - in the punch card days, cols 73 to 80 were saved for sequence numbers so you could resort your deck after dropping them; I even (shock horror) allow in-line comments to slightly break the 80 column rule; if more than slightly, the comment goes on its own line with the code underneath. To preserve visual formatting regardless of how different editors display TAB, I use TAB for indenting but then I use spaces inside the code / comments after the first non-TAB character. This keeps tables / type declarations etc lined up.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #49

          The only place I worked that had a standard, specified eighty. I assumed it was because we used dumb terminals (VT340s?), but I set mine to 132 columns. PRO*C also defaulted to an eighty character maximum (WTF?), but a different maximum could be specified. Now I limit myself to 112 characters because that's how many I can fit on a page 8.5" wide with half-inch margins and an 8-point font. Such lines span only about half the width of my screen when I use a WYSIWYG editor.

          1 Reply Last reply
          0
          • Richard Andrew x64R Richard Andrew x64

            What, if any, is the practical reason for preferring spaces over tabs? I use tabs because when I need to increase or decrease an indent, it's only a single character that must be added or deleted. I noticed that the Google Guidelines disallow tabs, and use only spaces. What have they got against tabs?

            S Offline
            S Offline
            Synaptrik
            wrote on last edited by
            #50

            I can think of one reason. When aligning columns of assignments, if comparisons, etc. When using tabs instead of spaces, say yours is set at 2, and your colleague's is set at 4, you'll find freaky alignments for the mixed code. e.g. int foo = 0; int foobar = 0; So, I prefer spaces. And a code standard to enforce the number, after deliberations of course. :) Some shops use 2, but most I've found prefer 3.

            This statement is false

            P 1 Reply Last reply
            0
            • R Roger Stoltz

              If the decision is up to me I prefer tabs (and tab size 4). The reason is that in any decent editor it's possible to configure the tab size so everyone can view and edit the source code with their own preferred tab size. Most document viewers have the same possibility, e.g. the viewer and compare tool in ClearCase. Naturally, if I encounter source code I have to edit and the indentation is based on spaces, I continue to use spaces in order not to mess up the source code. At my current assignment the coding guidelines states that spaces should be used and the "indentation size" is 2(!) spaces. In my opinion that indentation size is too small and makes the source code hard to read and follow. However, I only use tabs for indentation and possibly preceding comments at the end of a line. In all other cases I use spaces.

              "It's supposed to be hard, otherwise anybody could do it!" - selfquote
              "High speed never compensates for wrong direction!" - unknown

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #51

              I used to use two SPACEs. In fact I also used one! A snippet of Pascal:

              if (paramcount = 0)
              then
              begin
              writeln('Missing required parameters.');
              exit;
              end;

              if (paramcount >= 2)
              then
              assign(outfile , paramstr(2))
              else
              assign(outfile , 'CON');

              then and else are indented only half-way :sigh: When I started writing C, I used the same style. I also used the same style with DCL.

              R 1 Reply Last reply
              0
              • P PIEBALDconsult

                I used to use two SPACEs. In fact I also used one! A snippet of Pascal:

                if (paramcount = 0)
                then
                begin
                writeln('Missing required parameters.');
                exit;
                end;

                if (paramcount >= 2)
                then
                assign(outfile , paramstr(2))
                else
                assign(outfile , 'CON');

                then and else are indented only half-way :sigh: When I started writing C, I used the same style. I also used the same style with DCL.

                R Offline
                R Offline
                Roger Stoltz
                wrote on last edited by
                #52

                PIEBALDconsult wrote:

                A snippet of Pascal:

                Ahh! Those were the days! I've also seen Pascal code without any indentation at all for the begin/end pair even though the rest of the code was indented. The reason was that "they work as the curly braces in C". So, there you have it: indentation size zero! :laugh:

                "It's supposed to be hard, otherwise anybody could do it!" - selfquote
                "High speed never compensates for wrong direction!" - unknown

                1 Reply Last reply
                0
                • P PIEBALDconsult

                  I just installed and experimented with SciTE yesterday, it has settings for each language.

                  aquatarian wrote:

                  and fix the indenting to the project's code formatting rules before commit.

                  If only. :sigh: Eventually I wrote my own untab utility. It would be better if the code library system performed a beautify action to ensure standards compliance and eliminate (most) format-only changes.

                  H Offline
                  H Offline
                  Henry Minute
                  wrote on last edited by
                  #53

                  PIEBALDconsult wrote:

                  I wrote my own untab utility

                  Did you do an article on it?

                  Henry Minute If you open a can of worms, any viable solution *MUST* involve a larger can.

                  P 2 Replies Last reply
                  0
                  • P patbob

                    The strongest argument for spaces is hanging indents -- when you wrap lines of code and indent them. If you use tabs, it only looks correct for the rest of the world that uses the same tabstops as you do. When you use spaces, it looks correct for everybody. However, when working in existing files, the rule should be to match what is already used in the file, even if it isn't your favorite. If I were dictator of the world, I'd have already decreed it so and tortured the unbelievers into compliance.. probably a good thing I'm not dictator :)

                    patbob

                    D Offline
                    D Offline
                    Dan Neely
                    wrote on last edited by
                    #54

                    No, hanging indents just require being smart about mixing them. The non hanging part is tabs, the extra spaces:

                    TAB TAB DoStuff(Foo, bar,
                    TAB TAB Spaces Baz);

                    Works as intended on anyones editor regardless of tab width.

                    Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                    P P 2 Replies Last reply
                    0
                    • D Dan Neely

                      No, hanging indents just require being smart about mixing them. The non hanging part is tabs, the extra spaces:

                      TAB TAB DoStuff(Foo, bar,
                      TAB TAB Spaces Baz);

                      Works as intended on anyones editor regardless of tab width.

                      Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                      P Offline
                      P Offline
                      patbob
                      wrote on last edited by
                      #55

                      That's a great solution, except it won't work if the editor converts spaces to tabs. Most of the editors I use, both Win and Lin, do that.

                      patbob

                      1 Reply Last reply
                      0
                      • H Henry Minute

                        PIEBALDconsult wrote:

                        I wrote my own untab utility

                        Did you do an article on it?

                        Henry Minute If you open a can of worms, any viable solution *MUST* involve a larger can.

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #56

                        Nooooo... should I? :~ I'm pretty sure it also removes trailing SPACEs, which VS won't do :mad: It may also be one that reports lines of excessive length. I guess I can at least take a look at the code. Oh goody. :sigh: P.S. Yeach... better just to write a new one in C#.

                        1 Reply Last reply
                        0
                        • Richard Andrew x64R Richard Andrew x64

                          What, if any, is the practical reason for preferring spaces over tabs? I use tabs because when I need to increase or decrease an indent, it's only a single character that must be added or deleted. I noticed that the Google Guidelines disallow tabs, and use only spaces. What have they got against tabs?

                          M Offline
                          M Offline
                          Member 1229083
                          wrote on last edited by
                          #57

                          I hate tabs, though I'm usually forced to use it in order to stay consistent with the rest of the source files I'm working on. I hate when I copy & paste my code somewhere and it looks all messed up because of the tabs not expanding correcly. What's worse is that when I want to change the indentation or change variable names, suddenly all the layout I've worked on gets distorted. When I hit the space bar, I want to see things moving to the right of it exactly by one space. I don't want it to cause the right part of the sentence to suddenly jump, and especially I don't want to keep pressing the silly space and watch how nothing happens.

                          1 Reply Last reply
                          0
                          • B Brady Kelly

                            TheArtistFormallyKnownAsMaxxx wrote:

                            I can indent 4 spaces, my colleague can indent 2 spaces

                            Different indents in the same source? :~

                            All Sorted

                            L Offline
                            L Offline
                            Lost User
                            wrote on last edited by
                            #58

                            No - if I set my tab spacing to 4 and my colleague to 2, then the display of thesource on my PC shows 4 'spaces' - the same source on his PC shows 2 'space'

                            If I knew then what I know today, then I'd know the same now as I did then - then what would be the point? .\\axxx (That's an 'M')

                            P 1 Reply Last reply
                            0
                            • G Gary Wheeler

                              Given that he's into testicles and anvils, probably not.

                              Software Zen: delete this;

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #59

                              I'm not into them, I was merely proposing a worse scenario.

                              If I knew then what I know today, then I'd know the same now as I did then - then what would be the point? .\\axxx (That's an 'M')

                              1 Reply Last reply
                              0
                              • D Dan Neely

                                No, hanging indents just require being smart about mixing them. The non hanging part is tabs, the extra spaces:

                                TAB TAB DoStuff(Foo, bar,
                                TAB TAB Spaces Baz);

                                Works as intended on anyones editor regardless of tab width.

                                Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall

                                P Offline
                                P Offline
                                PIEBALDconsult
                                wrote on last edited by
                                #60

                                Why use SPACEs there rather than a TAB? Shouldn't it be a full indent? :confused:

                                1 Reply Last reply
                                0
                                • J Jim SS

                                  Personal preference is tabs (3 spaces), but I am glad that other people have other preferences. I can almost always tell my code from others because of formatting. I got used to compacting my code when we only had 80x25 green screens so I could see more code on the screen at a time. I like:

                                  if (...) {
                                  ...;
                                  } else {
                                  ...;
                                  }

                                  others always do:

                                  if (...)
                                  {
                                  ...;
                                  }
                                  else
                                  {
                                  ...;
                                  }

                                  or:

                                  if (...)
                                  {
                                  ...;
                                  }
                                  else
                                  {
                                  ...;
                                  }

                                  It makes it easier to spot my own code and what may have been modified by me or others. I had a friend that could spot my code anywhere. He always claimed that I could write a whole program on a single line. :rolleyes:

                                  SS => Qualified in Submarines "We sleep soundly in our beds because rough men stand ready in the night to visit violence on those who would do us harm". Winston Churchill

                                  P Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #61

                                  Jim (SS) wrote:

                                  write a whole program on a single line

                                  You should be able to; avoid constructs that require newlines.

                                  1 Reply Last reply
                                  0
                                  • S Synaptrik

                                    I can think of one reason. When aligning columns of assignments, if comparisons, etc. When using tabs instead of spaces, say yours is set at 2, and your colleague's is set at 4, you'll find freaky alignments for the mixed code. e.g. int foo = 0; int foobar = 0; So, I prefer spaces. And a code standard to enforce the number, after deliberations of course. :) Some shops use 2, but most I've found prefer 3.

                                    This statement is false

                                    P Offline
                                    P Offline
                                    PIEBALDconsult
                                    wrote on last edited by
                                    #62

                                    But that's after the first non-whitespace character and therefore not part of the indent.

                                    1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      I just installed and experimented with SciTE yesterday, it has settings for each language.

                                      aquatarian wrote:

                                      and fix the indenting to the project's code formatting rules before commit.

                                      If only. :sigh: Eventually I wrote my own untab utility. It would be better if the code library system performed a beautify action to ensure standards compliance and eliminate (most) format-only changes.

                                      B Offline
                                      B Offline
                                      BananaRaffle
                                      wrote on last edited by
                                      #63

                                      Netbeans does this. You can define, very specifically, how to format the source. Then you hit a key/menu command and it reformats the entire file. It comes with built-in configurations that format in the style of ANSI, K&R, Linux, MYSQL, OpenSolaris, Apache, etc. It's not 100% perfect (sometimes a space will be out of place), but it's still the closest I've seen to the ideal.

                                      1 Reply Last reply
                                      0
                                      • L Lost User

                                        No - if I set my tab spacing to 4 and my colleague to 2, then the display of thesource on my PC shows 4 'spaces' - the same source on his PC shows 2 'space'

                                        If I knew then what I know today, then I'd know the same now as I did then - then what would be the point? .\\axxx (That's an 'M')

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #64

                                        But if you type it out in a DOS box, it's 8.

                                        L 1 Reply Last reply
                                        0
                                        • H Henry Minute

                                          PIEBALDconsult wrote:

                                          I wrote my own untab utility

                                          Did you do an article on it?

                                          Henry Minute If you open a can of worms, any viable solution *MUST* involve a larger can.

                                          P Offline
                                          P Offline
                                          PIEBALDconsult
                                          wrote on last edited by
                                          #65

                                          I have now, just emailed it off.

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups