Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^2: My coding guidelines

by emazep (Priest)
on Sep 08, 2005 at 05:58 UTC ( #490100=note: print w/ replies, xml ) Need Help??


in reply to Re: My coding guidelines
in thread My coding guidelines

But if you use spaces instead of tabs, indentation will also align. And you don't run the risk lines will be pushed over the 80 char limit if someone uses different tab settings.
But using spaces you are forcing the others to use for indentation the same number of spaces you've chosen, while they may prefer to use different settings.

Think for example of a visually impaired person (or simply a tired night coder) who wants 8-spaces indentation, who deliberately sets her tabs that way, to obtain what she wants regardless of the indentation habits of her colleagues (and still preserving their freedom to nest as deep as they prefer).

By using spaces for indentation, as you suggest, your are forcing her to use your own indentation settings (which could be even a single space for that matter), which is not kind at all. Furthermore, any decent editor permit you to (automatically) convert tabs to spaces (the amount of them you prefer), while the opposite is not (always) true.

In a word, tabs are portable, while spaces aren't (so tabs are not evil ;-)

Ciao,
Emanuele.


Comment on Re^2: My coding guidelines
Re^3: My coding guidelines
by demerphq (Chancellor) on Sep 08, 2005 at 11:41 UTC

    In a word, tabs are portable, while spaces aren't (so tabs are not evil ;-)

    I would say you are right, except for one thing. You will virtually always find situations where you can't indent properly with just tabs alone. Which means that if you work with tabs set at different size the code turns becomes very difficult to deal with. Spaces alone are much easier to work with. And they avoid the perenial argument about how long a tab will be. IMO forcing people to use your indentation is minimal issue, as the tendency seems to be that if you work on someone elses code, or on shared code, that you should follow the indentation style for the code itself, not your own preferences.

    ---
    $world=~s/war/peace/g

      You will virtually always find situations where you can't indent properly with just tabs alone. Which means that if you work with tabs set at different size the code turns becomes very difficult to deal with.
      This could be a problem, but it shouldn't be difficult to eliminate it. In order to do so, even if you use a mix of spaces and tabs to indent (or rather to align things vertically) just always use tabs to indent (that is, to align vertically the left margin of the lines), and reserve the use of spaces to align vertically only the things past the left margin (in the middle of a line, so to say) such as the => symbol in a hash. In other words, never use spaces to align the left margin and reserve their use only to align the things inside a line. Here is an example:
      my %a_hash = ( <TAB> very_very_long_key => 1, <TAB> k2 <SPACES> => 2, <TAB> another_k <SPACES> => 3 );
      I've personally used (and spread) this convention for years (mainly with languages different from Perl though) and never had a single problem or complaint.
      The only problem I can see happens if you want to use a fancier alignment like this:
      my %a_hash = ( very_very_long_key => 1, k2 => 2, another_k => 3 );
      but frankly I would avoid it (though some people seem to like it).
      IMO forcing people to use your indentation is minimal issue
      This is true as long as everyone religiously adopt the same, sensible number of spaces (4 could be OK, but not necessarily, see later), but I've seen so many times people who love to indent with just 2 spaces or even with a single space. As for the 4-spaces convention, not everyone agree: for example both the Linux kernel and the Gnome coders recommend to use 8-spaces (tabs) for indentation.

      Thanks to their portability, tabs are IMO the only way to to settle this controversy and make everyone happy.

      Ciao,
      Emanuele.

        The best solution to this controversy is to use perltidy. Just filter that nasty code through perltidy to get your preferred indentation, brace location, else-cuddledness, etc. If you are working in a team project, have the team define a standard set of perltidy options that are to be used before checking the code in. It can be automated as part of the check-in process so that you don't have to worry about it.

        In my experience the problem comes when people use editors that use tabs set at 8 spaces, and indentation rules of 4 spaces, with the editor autobundling 8 spaces into a tab when it can. This is a common editor set up IME, and just positively sucks when you change the size of tabs. And that was my point, using tabs you end up in debates about crap like this, using spaces you never do.

        Theres another aspect of Tabs that I havent seen mentioned, and that is their impact on a diff. One thing I find particularly annoying is that tabs in your code can lead to diff saying a line has changed but when you look at the line it looks identical. Its only when you notice that somone has accidentally stuck a space in front of a tab that you realize why diff is telling you what it is. IMO avoiding such scenarios is a good reason in itself to avoid tabs. Just like IMO its a good reason to avoid trailing whitespace.

        Anyway, this is one of those debates that I believe has proved intractable. There will always be stalwarts out there for one side or another. All i can say is that the pain of dealing with multiple tab sizes, and even tabs in general, has made me adopt a policy of: no tabs in source code, no trailing whitespace in source code.

        ---
        $world=~s/war/peace/g

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://490100]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (17)
As of 2014-09-02 15:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (25 votes), past polls