Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

A Question of style.

by GrandFather (Saint)
on Jun 06, 2005 at 21:12 UTC ( [id://464089]=perlmeditation: print w/replies, xml ) Need Help??

A meditation concerning layout style by a novice. Maybe a partial answer to the musings of others concerning the readability of Perl.

Preamble

As a new member of the brotherhood I guess it is expected that there will be a few things I don't understand and that I will bring a little baggage from the world at large. I will try not to disturb the sanctity and serenity within the walls of the Monastery and I appologise if I fail in this. However the matter I wish to discuss often raises the ire of the unenlightened in the outside world. I hope that you who have more experience in the realms of Perl will ponder my humble musings and reply with your wisdom and enlightenment, but without the angst so often encountered in less enlightened society.

Having come from a background in other languages (which will not be mentioned, but may become evident), and having thought deeply from time to time about matters of style in the manner of laying out program code, I have a few observations I wish to make concerning programming style in Perl.

The meat

The first observation is that the lamentable K&R block indentation style is pervasive in this community (as in many *NIX aligned communities). Can we do better? There are a number of points that occur to me when formulating a style for block layout:

  1. is the block structure obvious even to someone who may prefer a different style?
  2. is the relationship between blocks (and statements) obvious?
  3. is it easy to check that brackets match?
  4. are the brackets that delimit the block part of the block?
  5. is editing facilitated by the indentation style?

Points 1 and 2 are related. They are about readability and understanding by some else such as your maintenance programmer.

Point 3 is for you. When the fumble fingered compiler miscounts the brackets, how quickly can you prove it wrong by rubbing its nose in your carefully laid out code?

Point 4 is part of a justification for particular style decisions. If the brackets are part of the block they should be at the same indentation. After all, they are part of that which is being indented.

Point 5 is entirely pragmatic. If you can't edit the thing or it takes too much effort it ain't no use nohow.

Ok, having pondered that lot, how can we improve on K&R? Try this:

if ($SomeTest) {# Brief opening comment to describe this block ... } elsif ($AnotherTest) {# Brief comment for this block ... } else {# Ditto ... } ...

Note that the brackets are indented to the same level as the rest of the block (now you know that I think the brackets are part of the block). If the brackets weren't part of the block then most likely the block would be indented inside the indented brackets.

if ($SomeTest) {# Brief opening comment to describe this block ... } elsif ($AnotherTest) {# Brief comment for this block ... } else {# Ditto ... } ...
That variant meets most of my criteria, but does require more effort editing and consumes horizontal space at a furious rate.

Why is my suggested layout so much better than K&R?

if ($SomeTest) { # Brief opening comment to describe this block ... } elsif ($AnotherTest) { # Brief comment for this block ... } else { # Ditto ... } ...
The block structure isn't obvious to me. All those trailing } obscure the important stuff like the elsif and else. The {'s hidden on the end of lines can't be matched with their closing }'s without some effort, and no editor that I use puts the closing }'s in the right place for me (not that I've tried real hard :-)).

Ok, there's enough to get you going about indentation. How about white space usage? Its a little more subtle, and perhaps a lot more important. Until we started to learn Perl (and for some of us, even after) by far the majority of reading we did was English text (if you're reading this that's likely the case). There are conventions on use of white space in written english( this is unconventional for example ). So, where possible, why not apply those same conventions to our coding style?

Written English is structured as a hierarchy (words, phrases, sentences, paragraphs and chapters ...) to help reading and understanding. Well, what do you know, so is Perl! Maybe we can use something like the same structure to leverage our well trained written English parser (yer brain!) for use in parsing code?

How does that work? Well, for a start white space goes outside brackets (like this), not inside( like this ). There is white space after punctuation, like this, not omitted,like this.Why do we do that? Well, it helps us tokenise the strings that we are dealing with. Compilers don't need that sort of help, but they don't have to do a whole lot of pattern recognition either; we do.

Summary

Is any of this important? Actually, I think it is. Perhaps indentation is a little less important with Perl where often a problem solution is very short and its life time may also be short. But with larger projects (say, more than 40 lines) and projects that may have a long life time, writing for later understanding is much more important than saving a few keystrokes in the short term. In any case judicious use of white space helps make meaning clear for programs of any size.

And figuring out you own reason for doing something is much more important than just following along the rut worn by everyone who went before you.


Food for thought, not fuel for flames.

Replies are listed 'Best First'.
Re: A Question of style.
by talexb (Chancellor) on Jun 06, 2005 at 21:32 UTC

    While no doubt well meant, the subject of coding style has enough opinions that it is usually referred to as a 'religious war', comparable to one's choice of editor or operating system. Thus, in polite society (hopefully The Monastery is included in this definition), one usually soft-pedals any discussion of said 'delicate' subjects -- or even doesn't mention them at all.

    To put it another way, there's no 'nice' way to discuss bracing style -- it's just a matter that we're going to have to agree to disagree on.

    And that's really all I can say.

    Alex / talexb / Toronto

    "Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Re: A Question of style.
by kirbyk (Friar) on Jun 06, 2005 at 21:40 UTC
    Yes, as people say, this is one of those things were TMTOWTDI really is true.

    I will say, the one concrete thing (as opposed to just preference) I like about having the { on the same line of a sub or an if:

    If it's a longer sub or if block, and I'm at the closing }, I can hit % in vi, and it takes me to the matching one. If it's on the same line, I see the sub name or conditional on the screen, which is _exactly_ what I want. If it's on the next line, I have to then scroll up a line to see, and the next thing I'm usually going to want to do after seeing that is % back to the end brace (to go back to where I was.) So, it annoys me to no end when people put them on the next line.

    -- Kirby, WhitePages.com

      If it's a longer sub or if block, and I'm at the closing }, I can hit % in vi, and it takes me to the matching one. If it's on the same line, I see the sub name or conditional on the screen, which is _exactly_ what I want. If it's on the next line, I have to then scroll up a line to see, and the next thing I'm usually going to want to do after seeing that is % back to the end brace (to go back to where I was.) So, it annoys me to no end when people put them on the next line.

      Can't you just rebind the % key so that (in addition to what it usually does) it checks that the line it is on is not the first or last line in the buffer, scrolling one line if necessary to accomplish that? vim fans claim it's pretty customizable, so one would think that would be easy (though, not being a vi user myself, I'm speculating here).

      (Of course, I prefer the behavior wherein the editor shows the line containing the opening brace in the status area when your cursor rests after the closing brace, so you don't have to hit a key in the first place, which brings us back to your argument, since there's not room in the status area for two lines without expanding it, which is a bit jarring.)

      My own reason for preferring nestled braces in most cases is that it makes the code significantly more vertically compact, which allows a lot more context to fit on the screen at once, making the code easier to follow, since you can see more with less scrolling about.


      "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68
      Ahhh, at last a reason for K&R indentation (albeit a bit cart before the horse).

      Food for thought, not fuel for flames.
Re: A Question of style.
by trammell (Priest) on Jun 06, 2005 at 21:44 UTC
    I think that differences in indentation, etc. are largely superficial. Some real style differences that matter at the end of the day are:
    • Is your code documented and commented?
    • Does your code have a reasonable interface? Have you separated concerns?
    • Does your code have a test suite? Do you use it?
    If you do these things these cosmetic issues like your indentation style don't matter.
Re: A Question of style.
by hardburn (Abbot) on Jun 07, 2005 at 13:30 UTC

    While working on the B language, K&R realized that cyrillic character sets have no curly-brace. Further, there is no tab character Therefore, a language that was to catch on in the West should rely heavily on this syntax in order to stop Godless Communists from using it.

    Not only did K&R use this syntax, but they also suggested a bracing style that was difficult to use on commie terminals:

    sub y { $h = shift; if( $b ) { # code } elsif( $t ) { # code } }

    As you can see, the key block, sub, places the brace on the next line. This is because Russian terminals were only 7 lines high, which was forced by Stalin's 1957 decree. With this style in hand, only 5 other lines were visible. The effectiveness can be increased through the use of plentiful vertical whitespace.

    However, modern corruption of the programming field has led to the development of languages that forsake this important, commie-fighting syntax.

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      That is far and away the most compelling justification for K&R indentation I have heard. Wish I could give it more than one vote!

      Food for thought, not fuel for flames.
Re: A Question of style.
by TheStudent (Scribe) on Jun 06, 2005 at 21:45 UTC
Re: A Question of style.
by dbwiz (Curate) on Jun 06, 2005 at 21:29 UTC

    Just a few days ago we had a discussion about the same subject.

    Have a look at it. There are plenty of internal and external links to satisfy your needs.

Re: A Question of style.
by adrianh (Chancellor) on Jun 07, 2005 at 10:18 UTC

    Oh lord. Not the "one true brace style" argument again :-)

    Learning to use perltidy and learning to compromise on something that the whole team can agree on would be my advice.

Re: A Question of style.
by tilly (Archbishop) on Jun 08, 2005 at 01:37 UTC
    Code Complete has (as usual) a lot of good things to say about indentation style. The best IMO is that programming expertise is fragile. Changing any of many apparently insignificant details can ruin an expert programmer's efficiency. It is possible to learn not to be so hampered by details, but they matter.

    If you ever have to use another form of indentation for long enough to become used to it, I predict that you'll discover that your present preference is entirely due to this fragility. You mentally block code based on the indentation style, and facing an unfamiliar indentation style you need to start thinking about that, which gets in the way of everything else that you do.

    However there are other people who would have exactly the same issue with how you indent code.

    BTW about commenting, if you really put comments on every block, you're probably overdoing it. Remember, comments start with as many bugs as code does, but bugs in code get fixed. And code tends to be better maintained than comments. Therefore if your comments reiterate what your code does, then experienced programmers are likely to ignore the comments.

    The mantra is to make the code, as much as possible, its own comment. You do this with well-chosen variable names, function names, and so on.

    Does that mean that you can remove comments? Of course not. But in the long run I've found, as I0 put it once, I find the most useful comments state what remains invariant, while the code states what gets transformed. In that way comments document what you need to know when editing the code, but changes to the logic are less likely to require re-writing comments.

    A relevant thread is Re (tilly) 2 (disagree): Another commenting question,. As I always point out when I bring up this thread, the thread was accompanied by a private chat between me and DeusVult. The tone of the thread was far more confrontational than the chat, so this thread doesn't really capture the actual tone of the full conversation. But still it is worthwhile reading.

      Excellent reply. Thank you for the links. I guess you recommend Code Complete? :-)

      I agree fully with your comments about comments and names, and indentation actually too. Our experience is indeed that programming expertise is fragile (legacy K&R formated code in our software really slows us down).

      No, I don't comment each block as illustrated. On reflection that was a rather illustration. Hey-ho.


      Food for thought, not fuel for flames.
Re: A Question of style.
by jonadab (Parson) on Jun 07, 2005 at 10:22 UTC
    Dude, just use cperl-mode (or the equivalent) and let it do your indentation for you. Then you can get on to thinking about your actual code. As far as your third point, automatic paren matching is forty-year-old technology, and any text editor that doesn't do it is lame in the extreme. Get a better text editor.
      but cperl-mode supports several styles, do you know about M-x cperl-set-style???
Re: A Question of style.
by Nkuvu (Priest) on Jun 06, 2005 at 22:34 UTC
    It doesn't matter what style of indentation you use. As long as you use it consistently.

      By that argument, would lack of indentation qualify as consistent use?

      Personally, I use what makes sense to me. I never focused on a particular style until I learned to write LPC, and I still write my Perl that way. (trust me -- it's better than how I learned to format Perl, as I came from a BASIC, Logo, Pascal and FORTRAN before that).

      As things go, I think that screen size and editor choice play a rather significant role in what someone's preferred style. Someone who learned to program on a 12 line high screen, or through a dumb terminal is going to think differently than someone who started programming with a 21" monitor on their desk. Likewise, someone who started out in ex/ed, or some other line editor is going to work much different from someone who worked in DOS EDIT, or BBEdit, or whatever else the popular editors are these days.

      I'm personally okay with just about anything, because I'll just reflow it. (which works up until I'm trying to submit patches, in which case I have to save my initial reformatted version, so I can diff that and the final, then re-apply the changes with the author's style of formatting back to the original. For the most part, I'm happy with whatever I'm given -- it's normally enough for me that people have gone out of their way to give their code at all, I'm not going to be overly picky about how many spaces are in it.

      It doesn't matter what style of indentation you use. As long as you use it consistently.

      If by "consistently" you mean "always the same way", then I actually disagree with this, at least to some extent. It is my considered opinion that good indentation style takes into consideration the semantics of the code being indented. Sometimes I put up to three "statements" (things between semi-colons) on the same line, if what they are doing is sufficiently related that it enhances, rather than impedes clarity, and indeed, that can make a block rather compact...

      if (condition) { $foo=0; $bar++; }

      But I certainly wouldn't recommend doing *all* conditional blocks that way. Also, sometimes I put something after a closing brace, on the same line, but only if what it's doing is related to the closing of the block, as in...

      open FOO, '<', $filename; while (<FOO>) { chomp; my ($bar, $baz, $quux) = $_ =~ /someregex/; push @quux, [$foo, $baz, $quux] if $quux; } close FOO;

      But again, I certainly wouldn't recommend always putting code on the same line after the closing brace. If it weren't logically related to the closing of the block, that could get very confusing. If I had to distill this to a general principle, it would be, take the code's semantics into consideration when deciding how much whitespace to use and where to use it.


      "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68
        No, I don't mean "always the same way." I'm talking about avoiding inconsistent indentation style like:
        if (condition) { code; other_code; } else { bork; } if (condition_2) { foo; } else { i_think_you_get_what_i_mean(); }
        I don't peronsally mind if a block is on one line, as long as it's short. if (condition) { $foo=0; $bar++; } is perfectly fine. I mean consistency within a file, given the context. If you always put short blocks on one line, great. If you do something like
        if (condition) { $foo=0; $bar++ } # more code... if (other_condition) { $foo++; }
        then I consider it to be inconsistent. And when I find inconsistent indentation it implies to me that your thinking isn't clear. This may or may not be the case -- it's more of an impression I get than an absolute rule.
Re: A Question of style.
by TedPride (Priest) on Jun 07, 2005 at 07:22 UTC
    I generally indent as follows:
    if ($x) { if ($y) { code; } code; } else { code; }
    Though some smaller bits of code can be formatted like this:
    if ($x) { code; }
    Or even this (since this is Perl):
    code if $x; long section of code if long test on $x;
    The style of indenting depends primarily on the size of the blocks, imho, though there are some indenting methods I hate:
    if ($x) { code; } if ($x) { code; }
    Etc. The former produces far too much white space; the latter makes for some real problems lining things up.
Re: A Question of style.
by itub (Priest) on Jun 07, 2005 at 22:35 UTC
    What is lamentable is calling someone else's favorite indentation style lamentable. :-) I personally prefer K&R, and find your style extremely ugly; the first thing I do when I'm confronted with code like that is pass it through perltidy.

    I won't get into the "reasons" for my preference because they are all subjective, and decades of history have proven that this discussion is futile.

    Coming soon: why cherry ice cream sucks and chocolate ice cream rules!

      Oh, I agree, chocolate ice cream rules (esp. with chocolate covered peanuts).

      Food for thought, not fuel for flames.
Re: A Question of style.
by sfink (Deacon) on Jun 09, 2005 at 06:11 UTC
    My answer to your #4 would be "no". To me, curly brackets are part of the conditional or looping construct.

    And I personally don't find it helpful to have open and close curlies line up vertically. I find it very annoying to scroll to some point in a file and see something like

    { ...do something... . . }
    Huh? Is that part of an if, while, for, or ? Oh, right. The important part is just above the top of the screen. I'd much rather have the close curly line up with the keyword.

    But, as many others have pointed out, that's all a matter of religion1. So to actually add something useful to this topic, I'll assert that everyone should try out a couple of different styles. And not just for a file or two. Personally, I have to stick to a style for about a month to get through the full set of phases:

    1. disgust and revulsion
    2. grudging acceptance and understanding of how the lunatics who use that style are able to make sense and extract value out of it
    3. enthusiastic and wholesale adoption of the style for yourself, together with disbelief at how mired you were in your ridiculous old style
    4. (this one usually comes after going back to editing one of your old projects) complete reversal -- wonderment at how you could possibly have found that new style any good. Sure, it has its advantages, but your original style is just so much better.
    5. and finally: integration, when you merge parts of the old and new together to make something that is more like one than the other, but takes parts from both.
    Or perhaps I'm just a slow learner. But the main thing to come out of this is tolerance. I had a big hang-up for a long time where I just couldn't work on a project written in a style I found abhorrent. Well, I could, but I would first reformat everything, and the original maintainer would ignore all of my patches because they looked like crap to him. Or, if we were forced to work together (eg on a school project), we would be constantly annoyed at each other, and prone to needlessly rewriting swathes of each other's code just to make them "readable"2.

    After I (unwillingly) went through a couple of those series of phases I listed above, I actually found it very liberating to be able to shift between styles fluidly to match whatever existing code I encountered. There are too many barriers to working with other people's code already: programming language, NIH syndrome, portability, licensing, .... You do not want style to be yet another one.

    Open source projects are great for this, since there are undoubtably several out there that interest you for personal reasons, and aren't written in your preferred style.

    1 And what's the one thing that all religions have in common? That's right, they all hold that they are right and all the rest are wrong.

    2 On the other hand, this helped my typing speed immeasurably. Whoever could type faster would be able to gradually convert the codebase to the One True Style.

      Good points well made.

      I realise that some people would have the {} belong to the enclosing statement. There is a compelling argument for them being part of the block, but I won't go into that here :-). That is about the point that religion enters.

      The first three people who worked on our code base had pretty compatible styles. Later additions to the team have had divergent styles. In general that's ok so long as they use their style in their new code, but it becomes a real pain when they start editing old code and convert parts of it to a radically different style. K&R and Whitesmiths just don't go together (see Indentation styles).


      Perl is Huffman encoded by design.
Re: A Question of style.
by bageler (Hermit) on Jun 07, 2005 at 16:48 UTC
    what'sthisaboutwhitespaceandindentation?Ifiteverythingononelineandcanreaditfine.
      Howdy!

      youForgotToCamelCaseItSoYouCanBeCompliantWithTheOneTrueJavaSymbolNamingConvention

      yours,
      Michael
Re: A Question of style.
by Juerd (Abbot) on Jun 06, 2005 at 21:18 UTC

    Here we go again...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://464089]
Approved by ktross
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 22:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found