Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: 78/80 chars perl line still a meaningful rule

by SuicideJunkie (Vicar)
on Oct 16, 2012 at 16:27 UTC ( [id://999368]=note: print w/replies, xml ) Need Help??


in reply to 78/80 chars perl line still a meaningful rule

I just skimmed through some code I'd written recently on my netbook and checked the line lengths. (not counting indentation, currently set to tab width of 2)

  • Most of the "short" lines are 40-50, as there are lots of two level deep hash keys with decent names involved.
  • Longer lines go 80-120, taking up half the screen with math and hash keys, or comments.
  • The longest lines hit 200, and tend to be die/warn/confess strings with interpolated hash lookups and a post condition.
    • eg: confess "Insufficent data in X from {hash lookup} through {hash lookup} source data file {hash lookup}" unless {condition};

I'm happy with the 200 char confess, because the important bits of it are all in the first 40-60 characters. Then there's a bunch of hash lookups which only matter after they've been interpolated into an error message. Then comes the conditions, which should have been clear from the English in the first 40-60 characters. It's a netbook, so why waste vertical space on unimportant stuff like that?

Note: I am not using a fixed width font, so the lines appear narrower than they otherwise would be.

Replies are listed 'Best First'.
Re^2: 78/80 chars perl line still a meaningful rule
by tobyink (Canon) on Oct 18, 2012 at 16:26 UTC
    $condition or confess sprintf( "Insufficent data in X from %s through %s source data file %s", $hash{lookup1}, $hash{lookup2}, $hash{lookup3}, );
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^2: 78/80 chars perl line still a meaningful rule
by McA (Priest) on Oct 17, 2012 at 07:35 UTC

    Good morning,

    thank you for looking at your code.

    One thing I'm really intersted in is how would the others break up and concatenate these long "confess-strings" if they don't want to violate the 80cpl rule?

    Best regards
    McA

      perltidy does

      confess "Insufficent data in X from {hash lookup} through {hash lookup} so +urce data file {hash lookup}" unless $condition;
      which seems reasonable, although these days I prefer "condition or" to "unless"
      $condition or confess "Insufficent data in X from {hash lookup} through {hash lookup} source + data file {hash lookup}";

      I've tried my hand at concatenation

      $condition or confess "Insufficent data in X from {hash lookup} " . "through {hash lookup} source data file {hash lookup}";
      And I it is pleasing to the eye, but I wouldn't do that by hand typing (editor IDE should handle it automagically). This also makes searching source code slightly more keyboard clicks but its not a deal breaker for me.

      I also like

      $condition or confessn "Insufficent data in X from {hash lookup} through {hash lookup} source data file {hash lookup}";
      where confessn would do some newline squashing

      But mostly I just let it go past 80 chars cause is the least hassle and works with all editors :)

        Is it only me, but I think personally that the perltidy solution looks really ugly. The manual splitting looks best in my eyes, but exactly this is cumbersome.

        Better solutions desired... ;-)

        Best regards
        McA

      For 'confess', it is trivial:

      % perl -MCarp=confess -e"confess 'this ', 'is split'" this is split at -e line 1

      If it isn't something that can deal with a list of strings, then I use join the vast majority of the time (because '.' has lousy precedence for such, 'sprintf' quickly leads to action-at-a-distance, '<<' can't be sanely indented, etc.).

      - tye        

      # Given. confess "Insufficent data in X from {hash lookup} through {hash lookup +} source data file {hash lookup}" unless {condition};

      I prefer sprintf when there are more than 1 or 2 simple variables are involved. Using sprintf sans () with post condition check is awkward, so used or. I would use map or hash slice if the 3 instances refer to the same hash (reference). First argument to sprintf would be on single line.

      {condition} or confess sprintf "Insufficient data in X from %s through %s source da +ta file %s" , {hash lookup} , {hash lookup} , {hash lookup} ;
        I would have split the long text-string into two more convenient strings, concatenated together, and put the "unless" on a separate line, with each subsequent line neatly-indented from the first. I would particularly want to be sure that the presence of the "unless" line was not missed. I might for that reason say "unless (condition) { ... confess ... }" to make this important point all the more obvious to the reader. Being "obvious" is essential.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-23 21:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found