---- config # These are YAML settings for Vroom title: Don't height: 34 width: 80 #skip: 22 # Skip slides. Useful when making slides. ---- center Don't by Tina Müller ---- == Don't guess while debugging +* verify! +* print data if you're not sure what's in it +* and even *if* you're sure. ---- perl,i10 # But don't just print... my $line = "one_word "; warn "line=$line\n"; # shows hidden spaces warn "line='$line'\n"; ---- perl,i10 use Data::Dumper; my $line = "4,5\r\n"; warn Dumper $line; local $Data::Dumper::Useqq = 1; # will print a newline as verbatim \n warn Dumper $line; ---- == Don't debug utf8 yourself +* use Devel::Peek; Dump $string; ---- perl,i10 # Don't use global variables use Data::Dumper; bar(); my $result = foo(); warn Dumper $result; sub foo { return { foo => { bar => { boo => { far => 23, }, }, }, }; } sub bar { $Data::Dumper::Maxdepth = 2; } ---- +* If you must use global variables, always local()ize them! local $Data::Dumper::Maxdepth = 2; ---- == Don't use Dumper $var +* You put in a debugging statement: warn Dumper $grid; +* You forget to take it out +* How should anyone else find the place where this statement is called? ---- == Better use Data::Dumper->Dump +* It lets you name the dumped variable +* I have a vim mapping which lets you type +* @dataDUMPER +* and gives you this: warn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\@data], ['data']); +* output: main:4 $data = [ ... ]; +* You immediately know which package, line and variable it is. +* This is the mapping: imap DUMPER ^iwarn __PACKAGE__.':'.__LINE__.$".Data::Dumper->Dump([\llyw$a], ['pa']); It can be found at http://www.perlmonks.org/?node=tinita ---- == Don't use substr() and index() ---- perl,i10 # what does this do? $file = substr($file, length($path)) if (substr($file, 0, length($path)) eq $path); # this needs documentation about *what* it does ---- perl,i10 # ah, better $file =~ s/^ \Q$path\E //x; # this is self-documenting ---- perl,i10 # Don't use C-Style for-loops # hard to read for ( my $i = 0; $i < @array; $i++ ) { # do something with $array[$i] } # better for my $i ( 0 .. $#array ) { # do something with $array[$i] } # perfect for my $elem ( @array ) { # do something with $elem } ---- == Don't use $var || '' or $var || 0 +* Why? my $string = function() || ''; # 0 will be converted to '' my $string = function() || 0; # '' will be converted to 0 +* Usually we are too lazy to test for definedness: my $string = function(); $string = '' unless defined $string; +* Luckily in perl 5.10 you can say: my $string = function() // ''; ---- == Don't test your code +* Break it! +* Testing if the code *works* is good and necessary +* Trying to *break* your code is much more fun +* And you will find even more bugs ---- == Don't test blindly * Use Devel::Cover to check if all your code is actually executed +* You will find out that it isn't +* Add more tests then to increase the coverage ---- == Don't try to be a wizard +* You saw the talk 'Tricks of a Wizard' or read 'Higher Order Perl' # /^\ . # /\ "V" # /__\ I O o # //..\\ I . # \].`[/ I # /l\/j\ (] . O # /. ~~ ,\/I . # \\L__j^\/I o # \/--v} I o . # | | I _________ # | | I c(` ')o # | l I \. ,/ # _/j L l\_! _//^---^\\_ -Row #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +* And now you put code refs all over your code +* Don't get me wrong, callbacks and closures are great, but I had to debug this code... ---- perl,i10 sub list { $loader ||= A::loader(...); $loader->(); } package A; sub loader { B::loader( ..., foo => sub { ... }, ) } package B; sub loader { ... return C::foo_loader( sub { ... }, bar(...), ..., sub { $foo = baz(); a::g::foo() if boo(); }, ); } ---- center == Don't use while (<$filehandle>) +* It doesn't localize $_ automatically +* So you'll overwrite the current $_ ---- left == This is the end * ASCII arts from http://www.ascii-art.de/ * Powered by Vroom::Vroom * Vroom::Vroom is written by Ingy * Write your presentation as a YAML file * Show your presentation with vim