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;
| [reply] [d/l] |
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. | [reply] [d/l] [select] |
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
| [reply] |
Re: Writing code that looks nice
by BrowserUk (Patriarch) on Apr 12, 2010 at 14:40 UTC
|
| [reply] |
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. | [reply] |
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.
| [reply] |
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. | [reply] [d/l] [select] |
|
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!)
| [reply] [d/l] |
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! ;-) | [reply] [d/l] |