Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Clean Code Hendrik Van Belleghem Belgian Perl Workshop 2007
It’s all Bruces fault!
Obviously? Pragmas <ul><li>warnings </li></ul><ul><li>strict </li></ul><ul><ul><li>subs  : no barewords! </li></ul></ul><u...
Obviously? Variable names <ul><li>Variable names should be </li></ul><ul><ul><li>Short </li></ul></ul><ul><ul><ul><li>but ...
Obviously? Variable names <ul><li>Variable names should definitely not be generated by Acme::MetaSyntactic: </li></ul><ul>...
Acme::Metasyntactic? It’s all BooK’s fault!
Obviously? Variable names <ul><li>Scalars are single </li></ul><ul><ul><li>$item </li></ul></ul><ul><li>Hashes and arrays ...
Braces.. bad? <ul><li>Array definition </li></ul><ul><ul><li>my @list =  </li></ul></ul><ul><ul><li>( ‘larry’,  </li></ul>...
Braces..good? <ul><li>Array definition </li></ul><ul><ul><li>my @list = ( </li></ul></ul><ul><ul><li>‘ larry’, </li></ul><...
Stay away from $_ <ul><li>$_ as default argument? </li></ul><ul><li>-X, abs, alarm, chomp, chop, chr, chroot, cos, defined...
Postfix if <ul><li>buy(&quot;secret lair&quot;) and </li></ul><ul><li>buy(&quot;sharks with lasers&quot;)  </li></ul><ul><...
C style loops <ul><li>for (my $slide = 1; $slide < 25 ; $slide++) </li></ul><ul><li>{ present($slide); } </li></ul><ul><li...
POD <ul><li>Put POD in the source file.. </li></ul><ul><ul><li>At the end </li></ul></ul><ul><li>Check your POD! </li></ul...
String eval is bad <ul><li>Don’t overuse eval.. Period </li></ul><ul><li>String eval is recompiled on execution </li></ul>...
Filehandles <ul><li>Bareword filehandles </li></ul><ul><ul><li>Cannot be localized </li></ul></ul><ul><ul><li>Passing it t...
Regexp::Common <ul><li>IP addresses? </li></ul><ul><ul><li>$ip =~ /$RE{net}{IPv4}/ </li></ul></ul><ul><li>MAC addresses? <...
Regexp::Common? <ul><li>It’s all Abigails fault! </li></ul><ul><li>Dutch </li></ul><ul><li>Tall </li></ul><ul><li>Strappin...
Regexp Delimiters <ul><li>Question.. </li></ul><ul><li>What does this do? </li></ul><ul><li>harry s truman was the 33rd u....
Regexp <ul><li>Keep it simple! </li></ul><ul><ul><li>$foo =~ s{foo}{bar}g; </li></ul></ul><ul><ul><li>$html =~ s/<(?:[^>'&...
Autoload <ul><li>Inheritence precedence? </li></ul>Catch all method
Refactoring <ul><li>Original code </li></ul><ul><li>Duplicated code </li></ul><ul><ul><li>Move to subroutines </li></ul></...
Tests <ul><li>Write tests first </li></ul><ul><li>Test::Simple & Test::More </li></ul><ul><li>Test for failures </li></ul>...
Version control <ul><li>Multiple developers </li></ul><ul><li>Undo </li></ul><ul><li>Generate patches </li></ul><ul><li>Me...
Format <ul><li>Compile time definition </li></ul><ul><ul><li>No runtime changes </li></ul></ul><ul><ul><ul><li>Except with...
Tie <ul><li>Unknown magic </li></ul><ul><ul><li>Unpredictable behavior </li></ul></ul><ul><ul><li>Eg: Tie::Scalar::Restric...
Perl::Critic <ul><li>As Module </li></ul><ul><li>As command line tool </li></ul><ul><li>As website : http://perlcritic.com...
Perl::Critic <ul><li>As Module </li></ul><ul><li>use Perl::Critic; </li></ul><ul><li>my $file = shift; </li></ul><ul><li>m...
Perl::Critic <ul><li>As command line tool </li></ul>lenore:~$ perlcritic -stern strict.pm  Code before strictures are enab...
Perl::Critic <ul><li>As website : http://perlcritic.com </li></ul>
Perl::Critic <ul><li>As Module </li></ul><ul><li>As command line tool </li></ul><ul><li>As website : http://perlcritic.com...
Thank you! <ul><li>どうもありがとうミスターロボット </li></ul><ul><li>(domo arigato, Mr Roboto) </li></ul>
Upcoming SlideShare
Loading in …5
×

Cleancode

1,942 views

Published on

Slides for talk on Clean Code given at the 2007 Belgian Perl Workshop. Reference: Damian Conways Perl Best Practices

Published in: Technology
  • Be the first to comment

Cleancode

  1. 1. Clean Code Hendrik Van Belleghem Belgian Perl Workshop 2007
  2. 2. It’s all Bruces fault!
  3. 3. Obviously? Pragmas <ul><li>warnings </li></ul><ul><li>strict </li></ul><ul><ul><li>subs : no barewords! </li></ul></ul><ul><ul><li>refs : no symbolic references! </li></ul></ul><ul><ul><li>vars : no undeclared variables! </li></ul></ul>
  4. 4. Obviously? Variable names <ul><li>Variable names should be </li></ul><ul><ul><li>Short </li></ul></ul><ul><ul><ul><li>but not too short </li></ul></ul></ul><ul><ul><ul><ul><li>$i, $a, $o </li></ul></ul></ul></ul><ul><ul><li>Unambiguous </li></ul></ul><ul><ul><ul><ul><li>$i, $data, $line </li></ul></ul></ul></ul>
  5. 5. Obviously? Variable names <ul><li>Variable names should definitely not be generated by Acme::MetaSyntactic: </li></ul><ul><ul><ul><li>$thwacke # batman </li></ul></ul></ul><ul><ul><ul><li>$flrbbbbb # batman </li></ul></ul></ul><ul><ul><ul><li>$bondelschwaartz #pynchon </li></ul></ul></ul><ul><ul><ul><li>$ununquadium # elements </li></ul></ul></ul>
  6. 6. Acme::Metasyntactic? It’s all BooK’s fault!
  7. 7. Obviously? Variable names <ul><li>Scalars are single </li></ul><ul><ul><li>$item </li></ul></ul><ul><li>Hashes and arrays are plural </li></ul><ul><ul><li>@items </li></ul></ul><ul><li>Mark booleans with test </li></ul><ul><ul><li>$found_match </li></ul></ul><ul><li>Add reference prefix/suffix </li></ul><ul><ul><li>$item_ref </li></ul></ul>
  8. 8. Braces.. bad? <ul><li>Array definition </li></ul><ul><ul><li>my @list = </li></ul></ul><ul><ul><li>( ‘larry’, </li></ul></ul><ul><ul><li>‘ damian’ ); </li></ul></ul><ul><li>Any other block? </li></ul><ul><ul><li>for my $coder ( @list ) </li></ul></ul><ul><ul><li>{ print “hi $coder ”; } </li></ul></ul>
  9. 9. Braces..good? <ul><li>Array definition </li></ul><ul><ul><li>my @list = ( </li></ul></ul><ul><ul><li>‘ larry’, </li></ul></ul><ul><ul><li>‘ damian’ </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><li>Any other block? </li></ul><ul><ul><li>for my $coder ( @list ) { </li></ul></ul><ul><ul><li>print “hi $coder ”; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  10. 10. Stay away from $_ <ul><li>$_ as default argument? </li></ul><ul><li>-X, abs, alarm, chomp, chop, chr, chroot, cos, defined, eval, exp, glob, hex, int, lc, lcfirst, length, log, lstat, oct , ord, pos, print, quotemeta, readlink, ref, require, reverse, rmdir, sin, split, sqrt, stat, study, uc, ucfirst, unlink </li></ul><ul><li>Implicit use </li></ul><ul><ul><li>m//, s///, y/// </li></ul></ul><ul><ul><li>for(), map {}, grep {}, while(<>) </li></ul></ul>default input and pattern matching space
  11. 11. Postfix if <ul><li>buy(&quot;secret lair&quot;) and </li></ul><ul><li>buy(&quot;sharks with lasers&quot;) </li></ul><ul><li>and $mini_me = clone() </li></ul><ul><li>if $goal eq &quot;world domination&quot;; </li></ul><ul><li>if ($goal eq “world domination”) </li></ul><ul><li>{ buy(“secret lair”); </li></ul><ul><li>buy(“sharks with lasers”); </li></ul><ul><li>$mini_me = clone(); </li></ul><ul><li>} </li></ul>
  12. 12. C style loops <ul><li>for (my $slide = 1; $slide < 25 ; $slide++) </li></ul><ul><li>{ present($slide); } </li></ul><ul><li>for my $slide (1..25) </li></ul><ul><li>{ present($slide); } </li></ul><ul><li>Cleaner </li></ul><ul><li>Easier to read </li></ul>
  13. 13. POD <ul><li>Put POD in the source file.. </li></ul><ul><ul><li>At the end </li></ul></ul><ul><li>Check your POD! </li></ul><ul><ul><li>Podchecker </li></ul></ul><ul><li>Use POD templates & stubs </li></ul><ul><ul><li>NAME, VERSION, SYNOPSIS,.. </li></ul></ul>
  14. 14. String eval is bad <ul><li>Don’t overuse eval.. Period </li></ul><ul><li>String eval is recompiled on execution </li></ul><ul><ul><li>Use block instead </li></ul></ul><ul><li>Are you taint-checking?? </li></ul>
  15. 15. Filehandles <ul><li>Bareword filehandles </li></ul><ul><ul><li>Cannot be localized </li></ul></ul><ul><ul><li>Passing it to a sub? </li></ul></ul><ul><ul><ul><li>Try typeglobs </li></ul></ul></ul><ul><li>Better: indirect filehandles </li></ul><ul><ul><li>From 5.6 onwards </li></ul></ul><ul><ul><li>open my $filehandle, ”<filename” or die $!; </li></ul></ul>
  16. 16. Regexp::Common <ul><li>IP addresses? </li></ul><ul><ul><li>$ip =~ /$RE{net}{IPv4}/ </li></ul></ul><ul><li>MAC addresses? </li></ul><ul><ul><li>$ip =~ /$RE{net}{MAC}/ </li></ul></ul><ul><li>Credit cards </li></ul><ul><ul><li>$number =~ /$RE{zip}{VISA}/ </li></ul></ul>For all your extraordinary regexp needs
  17. 17. Regexp::Common? <ul><li>It’s all Abigails fault! </li></ul><ul><li>Dutch </li></ul><ul><li>Tall </li></ul><ul><li>Strapping hunk of </li></ul><ul><ul><li>manhood </li></ul></ul>For all your extraordinary regexp needs
  18. 18. Regexp Delimiters <ul><li>Question.. </li></ul><ul><li>What does this do? </li></ul><ul><li>harry s truman was the 33rd u.s. president; </li></ul><ul><li>harry s |ruman was |he 33rd u.s. presiden|; </li></ul><ul><li>harry($string =~ s{ruman was }{he 33rd u.s. presiden}xms); </li></ul>
  19. 19. Regexp <ul><li>Keep it simple! </li></ul><ul><ul><li>$foo =~ s{foo}{bar}g; </li></ul></ul><ul><ul><li>$html =~ s/<(?:[^>'&quot;]*|(['&quot;]).*?1)*>//gsx </li></ul></ul><ul><li>Add comments to big Regexp chunks </li></ul><ul><li>Use A and  instead of $ and ^ </li></ul>s{ < # opening angle bracket (?: # Non-backreffing grouping paren [^>'&quot;] * # 0 or more things that are neither > nor ' nor &quot; | # or else &quot;.*?&quot; # a section between double quotes (stingy match) | # or else '.*?' # a section between single quotes (stingy match) ) + # all occurring one or more times > # closing angle bracket }{}gsx; # replace with nothing, i.e. delete
  20. 20. Autoload <ul><li>Inheritence precedence? </li></ul>Catch all method
  21. 21. Refactoring <ul><li>Original code </li></ul><ul><li>Duplicated code </li></ul><ul><ul><li>Move to subroutines </li></ul></ul><ul><li>Duplicated subroutines </li></ul><ul><ul><li>Move to packages </li></ul></ul>No overkill!
  22. 22. Tests <ul><li>Write tests first </li></ul><ul><li>Test::Simple & Test::More </li></ul><ul><li>Test for failures </li></ul><ul><li>Test the obvious & not so obvious </li></ul><ul><li>Find a bug? Write a test! </li></ul>
  23. 23. Version control <ul><li>Multiple developers </li></ul><ul><li>Undo </li></ul><ul><li>Generate patches </li></ul><ul><li>Merging/branching </li></ul><ul><li>CVS, Subversion, RCS, Perforce, BitKeeper, git </li></ul>
  24. 24. Format <ul><li>Compile time definition </li></ul><ul><ul><li>No runtime changes </li></ul></ul><ul><ul><ul><li>Except with eval </li></ul></ul></ul><ul><li>Global variables only </li></ul><ul><li>Output to named filehandle </li></ul>
  25. 25. Tie <ul><li>Unknown magic </li></ul><ul><ul><li>Unpredictable behavior </li></ul></ul><ul><ul><li>Eg: Tie::Scalar::RestrictUpdates limits changes </li></ul></ul><ul><li>Slow </li></ul><ul><ul><li>Change variable behavior through code </li></ul></ul>
  26. 26. Perl::Critic <ul><li>As Module </li></ul><ul><li>As command line tool </li></ul><ul><li>As website : http://perlcritic.com </li></ul><ul><li>DIY </li></ul><ul><li>Configurable </li></ul>
  27. 27. Perl::Critic <ul><li>As Module </li></ul><ul><li>use Perl::Critic; </li></ul><ul><li>my $file = shift; </li></ul><ul><li>my $critic = Perl::Critic->new(); </li></ul><ul><li>my @violations = $critic->critique($file); </li></ul><ul><li>print @violations; </li></ul>
  28. 28. Perl::Critic <ul><li>As command line tool </li></ul>lenore:~$ perlcritic -stern strict.pm Code before strictures are enabled at line 3, column 1. See page 429 of PBP. (Severity: 5) Code before warnings are enabled at line 3, column 1. See page 431 of PBP. (Severity: 4) Always unpack @_ first at line 11, column 1. See page 178 of PBP. (Severity: 4) Subroutine does not end with &quot;return&quot; at line 11, column 1. See page 197 of PBP. (Severity: 4) Always unpack @_ first at line 27, column 1. See page 178 of PBP. (Severity: 4) Subroutine does not end with &quot;return&quot; at line 27, column 1. See page 197 of PBP. (Severity: 4) Always unpack @_ first at line 32, column 1. See page 178 of PBP. (Severity: 4) Subroutine does not end with &quot;return&quot; at line 32, column 1. See page 197 of PBP. (Severity: 4)
  29. 29. Perl::Critic <ul><li>As website : http://perlcritic.com </li></ul>
  30. 30. Perl::Critic <ul><li>As Module </li></ul><ul><li>As command line tool </li></ul><ul><li>As website : http://perlcritic.com </li></ul><ul><li>DIY </li></ul><ul><ul><li>Lots of policies </li></ul></ul><ul><li>Configurable </li></ul>
  31. 31. Thank you! <ul><li>どうもありがとうミスターロボット </li></ul><ul><li>(domo arigato, Mr Roboto) </li></ul>

×