Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Style geekcode

by Juerd (Abbot)
on Mar 25, 2002 at 11:44 UTC ( [id://154071]=perlmeditation: print w/replies, xml ) Need Help??

OI!

We speak about style a lot. Do we put subs near the top, or do we insist on having them at the bottom? Do we cuddle our elses? How are our variables named, do we have space after commas, where's the opening curly anyway? Do we like arrows or double sigils? And what about indenting?

We all have different opinions about style. Style is very personal, so that may or may not be a good thing. However, we argue about style a lot, and never really sum our favourite style.

The Perl Style Geekcode was inspired by the original Geekcode and the Perl Geekcode, but because I didn't want to imply good or bad, I chose to use digits instead of + and -.
I leave a lot of subjects untouched, like programming logic, infinite loops, module usage, and a lot more. Sorry 'bout that.

Here it is:

Updates are denoted by a coloured character in front of what has been added or changed.
% 2002-03-25 16:32 CET
% 2002-03-25 18:13 CET

I - Indenting

Add the number of spaces you use, or t if you use only tabs. % If you use tabs, you can add "=4" to indicate you have set your editor to show tabs as 4 spaces.
Those who consider a tab to be multiple normal indents, may also suffix t, but with a number.
% If you have your editor change existing code automatically to follow your style, add an exclamation mark.

Example: 4-space indents and tab is two of those: I4t2
% Example: tab indents, but shown as 4 characters: It=4
% Example: I make my editor convert tabs to 8 spaces: I8t!
% Example: I use tabs, but I consider 8 spaces equal to one tab: It=8

O - opening curly

  • s => same line
  • n => new line
    • i => indented
    • % <i => indented, but less than the following block
  • 0 => no space before
  • 1 => space before
Example: on the same line and space before: Os1

; - semicolon

  • 0 => no space before
  • 1 => space before
  • 2 => line up semicolons

S - whitespace around operators

Add signs to the following items. The possible signs are (with X as the item):
  • <X => space before
  • X> => space after
  • <X> => space on both sides
  • % X => no space around this operator
Add a question mark on the _inside_ if you change style a lot: x?>

The operators: qw(, . + ** = && and ++ gt ==) Plus implies all simple math operators, what you say for && goes for ||, gt is all comparison operators, and so forward. With ++, it's only about between ++ and its variable.
%With ++ it's only about the difference between $foo++ and $foo ++ (choose any direction).

Example: S,><.><+>**<=><&&><and>++<gt><==>

B - spaces in brackets and parens if (XXXXX foo XXXXX)

% Floating point possible
  • 0 => never
  • 1 => only around complex things
  • 2 => always
  • 3 => more spaces indicates lower precedence

L - blank lines

  • 0 => never
  • 1 => between chunks
  • 2 => more blank lines means more different chunks

C - cuddling (else)

  • -1 => fully uncuddled and un-indented more than the curlies
  • 0 => uncuddled
  • 1 => half cuddle
  • 2 => full cuddle
Examples:
    C-1       C0        C1       C2
    }         }         }        } else {
  else        else      else {
    {         {

P - parens

% Floating point possible
  • -1 => I always try to avoid them
  • 0 => I don't really care, as long as it works
  • 1 => I always use parens
  • 2 => I'm extremely cautious
  • 3 => I'm paranoid: if ((($foo > $bar) && ($baz < $xyzzy)))

N - newline

  • < => before operator
  • > => after operator
  • e => I make an exception for qw(and or || &&) and such
Example: N>e

R - dereferencing

  • -1 => I prefer ${ $foo }{bar}
  • 0 => I prefer $$foo{bar}
  • 1 => I prefer $foo->{bar}
  • 2 => I wish there were a way to dereference the entire thing with an arrow

V - variable and sub names

  • % l - language
    Put the language(s) in parens, and separate with comma's. Most frequently used language first. Use ISO 639 2-letter codes. (Example for "mostly english, but dutch sometimes): l(en,nl)
  • c - caps
    • 1 => perl-like: long_names_like_this
    • 2 => studdlycaps: LongNamesLikeThis
    • 3 => java-like: longNamesLikeThis
  • a - abbreviation
    • 0 => i never abbreviate
    • 1 => sometimes
    • 2 => i even use "ary" instead of "array"
    • 3 => my variable names are never longer than five characters
  • p - plurality (put its arguments in parens)
      (-1: never, 0: sometimes, 1: always)
    • s - scalar names
    • h - hash names
    • a - array names
  • r - references
    • 0 => I don't include "ref" in the name
    • 1 => I do include "ref" in the name
    • 2 => I even call them "arrayref", "aryref" or "aref"
  • d - dummy names
    • 0 => % I name them a single letter
    • 1 => I call them "dummy" %, "temp" or something like that
    • 2 => I use "foo" and friends
Example of Slash style (incomplete): Vc1a0p(s-1h1a1)

H - habits

What is part of your normal every-script routine? (simply include those that are)
  • s - use strict
  • w - use warnings(1) or -w(2)
  • T - tainting on
  • d - use diagnostics
  • % a - $|++
Example: Hsw2

End with one of these

  • main-sub => main code before subs
  • sub-main => subs first, main code at the end
  • main => ALL subs are in external files
Add curlies to main if you put your main logic in a sub: "{main}-sub".


My code:
I4t2 Os1 ;0 S,><.><+>**<=><&&><and>++<gt><==> B1 L1 C2 P-1 N>e R2 Vl(en,nl)c1a1p(s-1h0a1)r0d2 Hsw2 sub-main
I'd like to know yours :)

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk

Replies are listed 'Best First'.
Re: Style geekcode
by rob_au (Abbot) on Mar 25, 2002 at 12:09 UTC
    A couple of things that stood out with this geek stylecode ... Hrmmm, geek and style, is that a contradiction of terms in itself? ... Anyhow ...

    • The ++ denotation within the whitespace around operators seems without merit - This is because while the prefix and postfix increment operands perform the same task, their order differs and as such, depending on the code, are not stylistically interchangeable.

    • The == denotation within the whitespace around operators is duplicated by the gt denotation because in fact, after all, == is just another equality test.

    • The variable and sub names denotation is quite neat, although I think the parentheses for plurality is redundant if px would suffice (where x is the descriptor code to apply to scalars, hashes and arrays) - For example, Vp(s0h0a0) should be able to be reduced to Vp0.
     

    Very nifty though ... Juerd++

     

    I4 Os1 ;0 S,><.><+>**<=><&&><and><gt> B1
    L1 C2 P2 N< R1 Vc1a2p(s0h0a0)r0d2 Hsw2T sub-main

      Hrmmm, geek and style, is that a contradiction of terms in itself?

      I like to think so ;)

      * The ++ denotation within the whitespace around operators seems without merit

      I knew I had to document it better (will do). It's just the difference between $foo++ and $foo ++ - choose any direction ;)

      # The == denotation within the whitespace around operators is duplicated by the gt denotation because in fact, after all, == is just another equality test.

      I separated them because I have seen someone consequently use "$foo > $bar" and "$foo==$bar"

      * For example, Vp(s0h0a0) should be able to be reduced to Vp0.

      Perhaps, but that would make the rules very complex, and I think that if someone's going to parse, a simple regex will have to be able to do the job ;)

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk
      

Re: Style geekcode
by demerphq (Chancellor) on Mar 25, 2002 at 14:21 UTC

    Nice!

    Before I give mine I had a couple of minor issues.. (heh you knew that was coming didnt you?)

    • For the indenting im using It(=4c<!>) to mean that I use tabs, but have them set to be displayed as four spaces and that my editor automatically converts leading spaces to such tabs and it automatically truncates trailing spaces. Sorry if I didnt follow the code, hopefully you havent finished your parser for this yet ;-)
    • For parens im using -0.5 because I use the minimum number of parens that keeps my codes meaning clear, but I dont try to explicitly avoid them.
    • For references I used R1.5 because I often call vars "array_ref" or the like, but I dont put "ref" in anything like all variables references.
    • Im using d0 for the dummy name question, assuming that "a" and "b" also includes safer var names like "i","j","x","y"
    • I added the keyword "tidy" to your end block, because I rotuinely run my code through perltidy.
    It(=4c<!>)Os1;0S,>.+**<=><&&><and><gt>++<==>B1L1 C2P-0.5N>eR2Vc1a1p(s0h0a0)r1.5d0Hsw1sub-main-tidy
    Update:
    more compliant with the updated spec. I stand by the tidy bit though.
    It=4! Os1 ;0 S,>.+**<=><&&><and><gt>++<==> B1 L1 C2 P-.5 N>e R2 Vl(en)c1a1p(s0h0a0)r1d.5 Hsw1 sub-main-tidy

    BTW, two thoughts regarding perltidy, the first is that you might have a look there for even more options for a later version of the perl geek style code, and on the other hand wouldnt it be cool if you could feed this style blcok into perltidy and have it behave accordingly? Now theres a nifty little hack that you could do that would put your style code a touch above some of the other codes (at least in terms of practicality.)

    Cheers Juerd, good post!

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

      (heh you knew that was coming didnt you?)

      I was afraid of that, yes :)

      * For the indenting im using It(=4c<!>) to mean that I use tabs, but have them set to be displayed as four spaces and that my editor automatically converts leading spaces to such tabs and it automatically truncates trailing spaces.

      I'll use just It=4!, meaning: "Indenting by tabs, but they are displayed as 4 columns of nothingness. I have my editor force (exclamation mark was stolen from vim) existing code to follow my style (convert spaces to tabs, in this case)". :)

      # Sorry if I didnt follow the code, hopefully you havent finished your parser for this yet ;-)

      I don't like parsing/parsed geek codes, so I will leave that up to someone else.

      # For parens im using -0.5 because I use the minimum number of parens that keeps my codes meaning clear, but I dont try to explicitly avoid them. # For references I used R1.5 because I often call vars "array_ref" or the like, but I dont put "ref" in anything like all variables references.

      Unlike with ++++ and ---, you can have floating point numbers. I like that idea. Will add :)

      * I added the keyword "tidy" to your end block, because I rotuinely run my code through perltidy.

      The end stuff was only to indicate order in a file, not programming order.

      It(=4c<!>)Os1;0S,>.+**<=><&&<and><gt>++<==>B1L1 C2P-0.5N>eR2Vc1a1p(s0h0a0)r1.5d0Hsw1sub-main-tidy

      Hmm, I really meant that whitespace to be in between items. But even that is a matter of style ;) Maybe sub-main is going to screw some parsers, but seeing your code, I'm glad I picked uppercased letters just before hitting submit :)

      and on the other hand wouldnt it be cool if you could feed this style blcok into perltidy and have it behave accordingly?

      That's just SICK, but a nice idea :)

      Cheers jured, good post!

      Thanks, but please s/jured/Juerd/ ;)

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk
      

Re: Style geekcode
by nefertari (Chaplain) on Mar 25, 2002 at 12:10 UTC
    My code:
    I4 Os1 ;0 S,>.<?+?>**=<&&><and>++<gt><==> B1 L1 C1 P0 N> R1 Vl(de,en)c1a1p(s-1h0a0)r0d1 Hsw2 sub-main

    It seems to me, that my style is currently changing. And some of it is due to emacs's indenting system. To the arraynaming: I often included liste (german for list) in my arraynames (and my variable names are often german).

    So why not another option: l at variables with the language code after that for the naming. For me it would be Vlde.

    update: Included l(de,en) in my code. (Yes, sometimes i use english names)

      So why not another option: l at variables with the language code after that for the naming. For me it would be Vlde.

      Great idea, but for the ease of reading I'll make parens required for that: Vl(en,nl) meaning "english most, dutch sometimes"

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk
      

Re: Style geekcode (80 columns?)
by Russ (Deacon) on Mar 25, 2002 at 18:00 UTC
    Oh, too much fun!

    Question: how would I show that one rule (see below) trumps all other considerations? For example, my code fits in 80 columns. Always. No exceptions. Period.

    So, could you add an item for column width? Perhaps:

    80 - Width/Wrapping
    How do you handle lines of more than 80 characters?

    • 80! - There are no lines longer than 80 characters!
    • 80? - Who knows? Just resize your window to see more.
    • 80> - My editor can scroll, you luddite!
    • 80+ - They wrap, just like you'd expect, what else?
    Multiple punctuation marks show your vehemence:
    • 80!!!! - Never, Never, Never > 80!
    • 80!+ - Usually within 80 columns, but I don't mind some wrapping.
    • 80!> - Usually within 80 columns, but just scroll to see the rest, sheesh.
    • 80??? - Why would anyone even care about an archaic standard when you have a modern Emacs?

    Then, how would I show that the 80 column rule trumps anything else (spaces may disappear to make it fit)?

    juerd++

    Russ
    Brainbench 'Most Valuable Professional' for Perl

Re: Style geekcode
by buckaduck (Chaplain) on Mar 25, 2002 at 13:58 UTC
    Looking at the codes submitted already, I'm nervously hoping that there are other weirdos like me out there... But here goes:

    I2t! Os1 ;0 S,><.><+><**><=><&&><and>++<gt><==> B1 L1 C1 P0 N< R1 Vc1a1p(s-1h0a1)r1d1 Hsw2 main-sub

    Update: Well as long as we're all being picky, it's a shame that I can't specify NO spaces around an operator. That's why I'm leaving out the ++ operator unless this gets clarified some more. Also, I'm choosing "dummy" variables even though these are more typically "temp".

    buckaduck

      it's a shame that I can't specify NO spaces around an operator.

      You can!! Just have the operator without "<" or ">". Your code would be:

      S,><.><+><**><=><&&><and>++<gt><==>
      I'll add a bit of clarification to the root node

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk
      

        In your example, how do you distinguish between these?
        ++ <gt> ++< gt>

        Update: Apparently he does it by reading the specs more carefully than I did. My bad.

        buckaduck

Re: Style geekcode
by Caillte (Friar) on Mar 25, 2002 at 17:51 UTC

    Fun idea! Here's mine:

    It=8 Os1 ;0 S,><.><+>**<=><&&& gt;<and>++<gt><==>
    B2 L1 C2 P-1 N> Vl(en)c1a1p(s0h0a0)r0d2 Hswl main-sub

    This page is intentionally left justified.

Re: Style geekcode
by buckaduck (Chaplain) on Mar 25, 2002 at 17:35 UTC
    How about quoting strings:
    • in a hash subscript
    • left of the comma-like arrow

    buckaduck

Re: Style geekcode
by BazB (Priest) on Mar 25, 2002 at 20:44 UTC
    Probably a little wierd :)
    It=4 Os1 ;0 S,>.<+><**><=><&&><and>++<gt><==> B2 L1 C2 P2 N>e R1 Vl(en)c1a1p(s-1a1h1)r2d2 Hsw1a sub-{main}
    In english:

    • Tab is 4 spaces
    • Spaces and newlines all over the shop, but not excessively so!
    • Cuddly elses.
    • Perl-y variables.
    • Use strict warnings and diagnostics or die - although Diagnostics only during development
    • Thanks to tye, main is a sub, tho' not for small scripts (less than 200-ish lines)
Re: Style geekcode
by mdillon (Priest) on Mar 25, 2002 at 20:43 UTC
    It On ;0 S,>.<+><**><=><&&><and>++><gt><==> B0 L1 C0 P-1 N< R1 Vc1a1p(s0h0a1)r0d1 Hsw2 main-sub
Re: Style geekcode
by Amoe (Friar) on Mar 25, 2002 at 21:36 UTC

    The only blatant thing I can see is in the P bit. What if you avoid parens where it makes the code look prettier or more logical? I don't avoid them with a vehemence, but I do care about them. For example, I always parenthesize remotely complicated things, and sub calls with more than one argument. Anyway, here's mine. I think it's not too odd.

    It=4 Os1 ;0 S,><.><+><**><=><&&><and>++<gt><==> B1 L1 C2 P0 N>e R2 Vl(en)c1a1p(s0h0a1)r0d2 Hsw1 main-sub

    --
    my one true love
Re: Style geekcode
by premchai21 (Curate) on Mar 25, 2002 at 21:02 UTC
    <person> <uniq><name>Drake Wilson</name><name>premchai21</name></uniq> <ccode type="perlstylecode"> I4t! On ;02S,>.<+><**><=>&&<and>++<gt>== B0 L1 C0 P0 N>e R1 Vl(en)c3a12p0(ha)r0d0 Hsw1 main-sub|{main}-sub|main </ccode> </person>

    Gripe: some items should be more "officially" multi-choosable; for instance, see the last item in my code. Also, floating-point numbers are not the best way to go about doing combinations I think; what about "somewhere between 1 and 3"? And, for instance, 1.5 tends to be read as biased towards 1.

      I dunno, I kind of like the floating-point idea. You could even write a style-comparison metric which would treat a style block as a point in n-dimensional space, and measure the distance between two hackers' styles in the standard Pythagorean way.

      Hell, you could even incorporate that into interviews if you were hiring people, to make sure you never hired someone whose style was radically different to your own ... any takers? ;-)

Re: Style geekcode
by BrentDax (Hermit) on Mar 26, 2002 at 10:15 UTC
    What about the (appalling) style of intermixing subs and main code? msauibn, perhaps?

    =cut
    --Brent Dax
    There is no sig.

Re: Style geekcode
by tmiklas (Hermit) on Mar 26, 2002 at 09:59 UTC
    Here's mine code:
    I4t1 Os1 ;0 S,><.><+>**<=><&&><and>++<gt><==> B1 L1 C2 P1 N>e R-1 Vc1a1p(s-1h1a1)r0d2 Hsw2 sub-main
    Greetz, Tom.
Re: Style geekcode
by theguvnor (Chaplain) on Apr 14, 2002 at 17:15 UTC

    This thing probably has too much momentum to change now, but I think it's regrettable that '>' and '<' were chosen to denote anything, since they have to be encoded in order to show up properly in webspace.

    Of course, perhaps that's part of the fun ; )

    ..Jon

Re: Style geekcode
by snafu (Chaplain) on Apr 15, 2002 at 21:19 UTC
    This is really great. Here's mine.

    It=4 Os1 ;0 S,><.><+><**><=><&&><and>++><gt><==> B2 L2 C2>0 P1 Ne R0 V1enc1>3a1p(s1h1a1)r0d1 Hsw2d?a main-sub

    _ _ _ _ _ _ _ _ _ _
    - Jim
    Insert clever comment here...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://154071]
Approved by root
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: (6)
As of 2024-03-19 14:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found