Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Writing code that looks nice

by theantler (Sexton)
on Apr 12, 2010 at 12:47 UTC ( [id://834260]=perlquestion: print w/replies, xml ) Need Help??

theantler has asked for the wisdom of the Perl Monks concerning the following question:

What constitutes nice looking code, and why is important? Here is my musing on this .. Nice looking should be a) minimal and compact but 2) should not sell-out on readibility. A very simple example:

 print if -d;

I would "correct" this (and maybe I am the only one?) to:

 if (-d) { print $_ } Maybe we should skip the $_ .. ? Folks, what do you think, what is the most correct style of perl code?

Replies are listed 'Best First'.
Re: Writing code that looks nice
by moritz (Cardinal) on Apr 12, 2010 at 12:49 UTC
    That's just a matter of personal preference, not of "correct"ness.

    It is also a matter of the intended audience: The less Perl they know, the more verbose might have to write.

    perlstyle collects some stilistic wisdom that's widely adopted in the Perl community, but YMMV.

    Update: In this specific case I'd either leave out the $_ altogether, or use an explicitly named variable, like

    print $file if -d $file;
Re: Writing code that looks nice
by JavaFan (Canon) on Apr 12, 2010 at 13:00 UTC
    I'd keep it as is. No need to introduce an extra scope. And why add the default argument for print, but not for -d?

    Folks, what do you think, what is the most correct style of perl code?
    Are you sure you're posting in the correct forum? Perhaps you're looking for the Python one. Perl embraces the "there's more than one way of doing things". It's the Python philosophy of there being only one right way of doing things.
Re: Writing code that looks nice
by nagalenoj (Friar) on Apr 12, 2010 at 13:12 UTC

    It's also a matter of familiarity.

    When a person a new to use some language, he will definitely use the basic constructs to solve his needs and when he get used to the language, he'll start playing with language.

    But confusions can be avoided by following some standards(as like perlstyle)

    Some of the earlier posts,
    I need perl coding standards
    Perl Naming Conventions
Re: Writing code that looks nice
by BrowserUk (Patriarch) on Apr 12, 2010 at 14:40 UTC
Re: Writing code that looks nice
by sblanton (Sexton) on Apr 12, 2010 at 14:48 UTC
    My style preferences are for natural language - I'll lean toward whatever sounds like English. "Print if -d"/"Print, if the directory exists" is a pretty simple English sentence.

    Others I know prefer a more mathematical approach, such as I would classify the latter.

    In order to save time, and with little fear of others having trouble reading my code in my current environment, I may choose a syntax based on the fewest # of characters that do not give me pause.

    There are a lot of considerations that can go into style decsions...not all may be obvious.
Re: Writing code that looks nice
by PeterPeiGuo (Hermit) on Apr 12, 2010 at 14:38 UTC

    It also depends on the flavor of the language, and the tradition and culture of the community.

    For example, Java community is one of those conservative ones. Perl for sure gives people more freedom. Interestingly, and indeed I am very happy to see, C# is becoming more and more artistic.

    Peter (Guo) Pei

Re: Writing code that looks nice
by amir_e_a (Hermit) on Apr 12, 2010 at 15:30 UTC

    I avoid using $_ and default arguments even in the shortest programs, except when i have to (in map, for example) or in very particular idioms, such as

    for ($string) { s/^\s+//; s/\s+$//; }

    (from perlfaq4.)

    And i usually avoid postfix conditions.

    But as everyone here says, it's a matter of taste.

      I think that's more idiomatic as

      s/\A\s+|\s+\z//g for @strings; # or $string =~ /\A\s+|\s+\z//g;

      For the OP: I prefer short, natural language code over formal blocks and such any day. But the point you get to at the bottom of it all is, there will be disagreement so consistency is best / don't edit other hackers' code for style; fit in with whatever codebase you have. (update, put in missing "/", thanks webfiend!)

Re: Writing code that looks nice
by StarNamer (Novice) on Apr 14, 2010 at 22:47 UTC
    The ultimate aim is for code that works. As long as anyone who needs to maintain the code understands it, then any style is fine by me.

    My personal preference tends towards minimalist code (print if -d;) simply as there's less likelihood of a typo! ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://834260]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-29 14:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found