in reply to bloated 'if' formatting

In my opinion, tabs == bad. Personally I use spaces for alignment (unless we're talking word processing, when it's likely that the font being used is not monospace).

As far as aligning the conditions above, I'm not sure if you're asking about indenting with tabs, or aligning the conditions vertically (no matter how you indent). Aligning the conditions vertically really depends on the length and complexity of said conditions. For example:

# Short conditions, I wouldn't align vertically: if ($foo && $bar && $bat && $waffles) { statements(); } # Long or complex conditions, I would align vertically # (also personally prefer binary operators at the end # of the line rather than the beginning): if (some_long_conditions_go_here() && another_one_down_here($foo) && ($foo || $bar) && I_think_you_get_what_I_am_saying()) { statements(); }

Replies are listed 'Best First'.
Re^2: bloated 'if' formatting
by radiantmatrix (Parson) on Oct 06, 2005 at 18:20 UTC

    Not to start a format war, but I've never understood the tabs == bad camp. There are times when using tabs is foolish (mostly, intra-line aligntment), for example:

    $opt_a = 1; # this is option a $obt_hoo = 1; # this is option b

    All the whitespace there should be spaces, not tabs.

    On the other hand, I love it when code I maintain uses tabs for leftmost indentation. This is because I know many people who like an 8-space indent, where as I (for example), much prefer a 4-space indent. I have colleagues that also like a 2-space indent (!). By using tabs for left-side indents, we can all have our way by simply configuring our editor.

    What's the argument against doing this?

    In the case at hand, I would do:

    if ( condition 1 && condition 2 && condition 3 ... ) { code to be executed } #/ short desc of if code, if needed. else { else-block code } #/ short desc of else code, if needed.
    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law
      I have to admit, the primary reason I dislike tabs is because most of the time I find tabs in a file, they're mixed in with spaces. So you get inconsistent indentation, based on tab widths. I've also seen too many instances of tabs in intra-line alignment, as you mentioned. Depending on tab widths, the columns may or may not line up.

      Since I've seen more misuses of tabs than decent uses of tabs, it's pretty easy to pigeonhole them into the "bad style" camp. It's really a very mild dislike, to tell the truth.

      And personally I prefer a 4 space indent for real code (i.e., code in my editor) but often I'll drop that to a 2 space indent when posting code online.

      That's a nice idea, but that would only work if windows aren't bounded horizontally. Think about it.

      Suppose you are using a 4-character indent. You are using tabs for indentation, using a 4 character tabstop. Inside a subroutine you have an for loop, and inside its body you have a 66 character statement. On your screen, the line ends in column 74.

      Now you give the code to someone who also uses 4 character wide indent, but he's using holding to the old mantra "don't change your tabstops" - his tabstops are 8 characters. Now the code he looks at not only has indents much wider than he's used to, the line that ended in column 74 on your screen will end in column 82 on his screen. Which, given a standard width of 80 characters means the code will be wrapped.

      Tabs are evil, and tabstops other than 8 even more so. But if everyone is using tabstops of the same length, you might as well use spaces.

      Perl --((8:>*
        Which, given a standard width of 80 characters means the code will be wrapped.

        Geez. I last used an 80 character wide screen back around 1984. Auspicous year. That was an ADM 3+. Next I used a VT100 which handle 132. Around 1987, I moved over to a PC with a EGA screen that also handle 132. And after that a 3279G which could handle 4 48x132 sessions tiled.

        The last time I used a *nix system, it was remotely via PC-based terminal emulator and I frequently used SCREEN to run four terminal sessions in a window, and from memory each of those four tiled sessions was over 80 characters wide. When I ran one session full screen it was something 200 x 72.

        What is this fixation with the 1970s and 24x80?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.