Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: 20 most important Perl Best Practices

by moritz (Cardinal)
on Aug 22, 2012 at 20:30 UTC ( #989137=note: print w/ replies, xml ) Need Help??


in reply to 20 most important Perl Best Practices

3) Always start your elsif or else block on a new line.

That seems to be a pure stylistic choice, cuddled else don't decrease robustness, efficiency or maintainability -- they just make some diffs a tiny bit larger.

9) Never use $_ or the invisible equivalent.
Always define your own variable in a foreach loop. Never use print or /regex/ with the invisible $_.

That's just stupid. If the scope for $_ is small, there's no reason to avoid it. Even more, if you don't want to use $_, you are doomed not to use map, grep and friends from List::Util. Which can make code much more maintainable.

Compare

my @filtered = grep /someregex/, @original;

with

my @filtered; for $str (@original) { if ($str ~~ /someregex/) { push @filtered, $str; } }

I for one find the first one much more readable and maintainable, because it has less boilerplate code.

Unless and until could be very confusing.

They could be, if you used them in a stupid way. Like with a negated condition. But so could be the if and while equivalents. I agree about unless and else blocks though.

What I don't like about your list is that it forces people to write very unperlish code, and (implicitly) forbidding map and grep seems like you are afraid of their power. Maybe that's because you haven't understood them yet?

Anyway, here are my top 3:

1) Don't do stupid things

It's usually pretty obvious if code will be hard to maintain later on. Convoluted call graphs, 50-char regexes without whitespaces and excessive use of magic literals all indicate that you don't want to end up maintaining the code. So don't do that. But you don't need a rule for everything if you apply your common sense.

Which brings me to my second point:

2) Think about what you write

That one is pretty obvious, no?

3) Be consistent

In many ways it doesn't matter much how exactly your programming style looks like, as long as it's consistent. If you don't code alone, make sure to agree on some style with your peers.


Comment on Re: 20 most important Perl Best Practices
Select or Download Code
Re^2: 20 most important Perl Best Practices
by greengaroo (Hermit) on Aug 22, 2012 at 20:54 UTC

    Interesting, let me sleep on this and I will get back to you! Thanks!

    Take my advice. I don't use it anyway.
Re^2: 20 most important Perl Best Practices
by Anonymous Monk on Aug 23, 2012 at 02:23 UTC
    3) Be consistent In many ways it doesn't matter much how exactly your programming style looks like, as long as it's consistent. If you don't code alone, make sure to agree on some style with your peers.

    I choose to be consistently inconsistent :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (15)
As of 2014-08-27 16:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (244 votes), past polls