Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Nice! I've recently started documenting my style used in my personal projects, the document is not complete. Over the years, my style changed a lot. The main difference is I hated spaces when I was younger which makes most of my old code unreadable :-)

Here's the document (converted from pod):

Choroba's Perl Style

Parentheses

Omit parentheses in builtin functions where possible.

print int rand $x;

Sometimes, I add parentheses to keep parallel structures similar.

return if substr($x, 0, 2) eq substr($y, 0, 2); # Second pair not ne +eded.

For subroutines, almost always use parentheses. You can omit them when it creates a nice DSL-like effect:

my $first = first { $x > 0 } @array; # List::Util

I also don't write them after test assertions.

is_deeply $response->{content}, bag(1 .. 10); # Test::Deep

I don't write empty parentheses after method calls.

$object->frobnicate;

For m, s, and qr, I use / or m{} (resp. s{}{}, qr{}). I use qx{} for code and usually q() and qq() for lists. I use different types of brackets for qw depending on the semantics of the enclosed expression:

use Exporter qw{ import }; # sub name my $from_roman = {qw{ I 1 II 2 III 3 IV 4 V 5 }}; my $primes = [qw[ 2 3 5 7 11 13 17 19 ]];

Whitespace, indentation, aligning

I use 4 spaces for indentation and spaces to align. No tabs at all.

I use a space after control flow statements and before the opening curly bracket:

while (my $item = $iter->next) {

I use spaces inside parentheses when the parenthesised expression does something on its own, e.g. changes a variable value. I only do it after functions, though, not control flow statements.

chomp( my @lines = <> );

I don't put spaces after [ or { when indexing an array or hash with a simple index. I usually use spaces for complex indices (both around the operators and around the whole expression). I write spaces after commas.

my $first = $array[0]; my $middle = $array[ $#array / 2 ];

I don't add space after a bracket starting a list, unless it's a qw() list or dereference.

my $arr = [1, 2, 3]; my @directions = qw( left right ); my @items = @{ $ref{items} };

I cuddle elses and use the K&R style:

if ($x > 0) { say 'Positive'; } elsif ($x < 0) { say 'Negative'; } else { say 'Zero'; }

For lists, though, I use the List style for the closing bracket:

my @planets = qw( Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune );

After a control flow command, I usually insert an empty line (unless the following line is a closing bracket).

while ($x = $iter->next) { next if $x > $threshold; $x -= $correction; }

If there is a longer sequence of the same commands, the inner empty lines might be omitted.

return if $x > $threshold; return if $err || ! $success; return [] if $not_found; my $result = count($x);

I use empty lines to separate code chunks semantically. In larger projects, I use two empty lines to separate subroutine definitons.

Boolean operators

I use ||, &&, and ! in expressions and and and or for control flow (which means I don't use not much).

Implicit variable

I usually don't type the $_ if not needed (i.e. map and grep blocks).

my @normalised = map lc, @words;

Expression forms of map and grep

Expression forms of map and grep are OK. I switch to the block form when the expression can't be written without +().

my %pairs = map { $_ => value($_) } @labels;

I don't use map and grep in void context. That's why we have for (and I never spell it foreach).

Semicolon

I don't put a semicolon after the command that leaves the block.

sub new { my ($class, $name) = @_; return bless { name => $name }, $class }

It protects me from adding code below and wondering why it's never executed.

Similarly, I don't put a semicolon after the 1 at the end of a module. Moreover, I usually spell it

__PACKAGE__

I also omit the semicolons in short one-line subs:

sub second { $_[0][1] }

I never put whitespace (including newline) before a semicolon.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

In reply to Re: How has your coding style changed over the years? by choroba
in thread How has your coding style changed over the years? by stevieb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found