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.
Sometimes, I add parentheses to keep parallel structures similar.
For subroutines, almost always use parentheses. You can omit them when it creates a nice DSL-like effect:
I also don't write them after test assertions.
I don't write empty parentheses after method calls.
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:
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:
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.
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.
I don't add space after a bracket starting a list, unless it's a qw() list or dereference.
I cuddle elses and use the K&R style:
For lists, though, I use the List style for the closing bracket:
After a control flow command, I usually insert an empty line (unless the following line is a closing bracket).
If there is a longer sequence of the same commands, the inner empty lines might be omitted.
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).
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 +().
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.
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
I also omit the semicolons in short one-line subs:
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
|
|