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.

Better Perl Practices

1,334 views

Published on

This presentation was at the Japanese Perl Association Seminar #1 in Akihabara on April 21st, 2009.

It covers ideas for how to have establish good habits one by one, and strategies to get them to stick.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Better Perl Practices

  1. 1. Better Programming Practices  When Best isn’t good enough
  2. 2. Better Programming Practices About Me • Jay Shirley, jshirley@gmail.com • On IRC (a lot), irc.perl.org as ‘jshirley’ • IT Director, National Auto Sport • Co-Founder of www.coldhardcode.com • Opinionated
  3. 3. Better Programming Practices Better • Useful techniques for better applications • Best is for Knuth, we build applications
  4. 4. Better Programming Practices Why? • We all complain • Sometimes we whine • JFDI
  5. 5. Better Programming Practices Step 1: You’re Wrong • Hopefully just the first time • Better to be wrong and learn, than to never learn.
  6. 6. Better Programming Practices Step 2: Learn • Accept you will not be right the first time • Still give a lot of thought • Make your second attempt stick
  7. 7. Better Programming Practices Step 3: Be Happy • Don’t be perfect • Perfect your technique • Be Happy (Or at least your boss will be)
  8. 8. Better Programming Practices Design • Goals! • Understand your role • Don’t over-extend • Don’t let others, either.
  9. 9. Better Programming Practices Testing • Is not a goal • Don’t design tests, design API then test • Coverage is not a valuable metric of completeness or progression
  10. 10. Better Programming Practices Refactoring • This is inevitable • Make it count • Learn!
  11. 11. Better Programming Practices It’s about the API • Make it simple on yourself • Expose flaws in tests • Impress your boss
  12. 12. Better Programming Practices These are good • We can all agree: Design, Test, Refactor • But we never do them. • Habits!
  13. 13. Better Programming Practices Establishing Habits • Make mole hills out of mountains • Make it easier to do the right thing • Make it harder to do the wrong thing
  14. 14. Better Programming Practices How to get habits • Make things easier as time goes on • Make things better as time goes on • Make people happier as time goes on • It’s about the future
  15. 15. Better Programming Practices Habits in Practice • The Hows: • How to (test|design|refactor)
  16. 16. Better Programming Practices How to test • Schema classes • SQLite • Unit Tests • Applications • Test::Class, Test::FITesque • Workflow Tests
  17. 17. Better Programming Practices How not to test • ok(1, ‘it works’);
  18. 18. Better Programming Practices Testing • Lots of Good Tools: • Test::Class • Email::Send::Test • Test::WWW::Mechanize
  19. 19. Better Programming Practices How to design • First, get something working • Syntax Sketch • Establish individual goals (methods) •
  20. 20. Better Programming Practices Not Unique • You are probably not the first to solve the problem • Find similar functionality
  21. 21. Better Programming Practices Iterative • Simmer & Reduce • That is your secret sauce • Software is fluid, not stone
  22. 22. Better Programming Practices Clever Japanese •
  23. 23. Better Programming Practices Web != Desktop • You control the environment • You control the deployment • Slow? Scalability! Add another server. • Apple would kill to have this
  24. 24. Better Programming Practices Better Applications • Your software is only as good as its tools • Don’t reinvent wheels
  25. 25. Better Perl Practices Better Perl
  26. 26. Better Programming Practices Simple Syntax my $foo = $request->params->{foo}; my $bar = $request->params->{bar};
  27. 27. Better Programming Practices good code, type my $data = $request->params; my $foo = $data->{foo}; my $bar = $data->{bar};
  28. 28. Better Programming Practices Type Less is not my $s=($api?$api->params:$request- >params)||$request->params; ${“__PACKAGE__::$_”}=($s->{$_}) for keys%$s;
  29. 29. Better Programming Practices warnings & strict use warnings; use strict;
  30. 30. Better Programming Practices good code, type my $data = {map{$_=>$request- >params->{$_}}@keys my $foo = $data->{foo}; my $bar = $data->{bar};
  31. 31. Better Programming Practices Test::Class • This is the best test package • Don’t take my word for it, chromatic and Ovid have written a ton on it at http:// modernperlbooks.com/
  32. 32. Better Perl Practices I failed 3 times
  33. 33. Better Programming Practices Why? • Package based testing • Wrap each package in a Test:: package • More flexible than .t files (code reuse) • Better configuration for testing
  34. 34. Better Programming Practices Base Classes • Common Functionality • Easy to change • Awesome in Perl
  35. 35. Better Programming Practices Find Dierences
  36. 36. Better Programming Practices Do More with Less • Make code better • Write less • Next step: use Moose;
  37. 37. Better Programming Practices Moose 101 • This is not a Moose talk • Perl API to Perl OO • Keep it Simple
  38. 38. Better Programming Practices Use what you need • Too many OO-API modules on CPAN • Moose does most of it • Don’t need it all, just ‘use Mouse;’
  39. 39. Better Programming Practices Use Config • Configure everything you can think of • You’ll figure out how to do it easily • The more you do it, the easier it is • (MooseX::SimpleConfig)
  40. 40. Better Programming Practices Favorite Recipe Ever with 'MooseX::SimpleConfig'; with 'MooseX::Getopt'; has +configfile = ( default = ( grep { defined $_ and -f $_ } @places_to_look )[0]||”” );
  41. 41. Better Programming Practices To Recap • Write less code (Base classes) • Test where it matters • Use Moose (Write less code) • Config!
  42. 42. Better Programming Practices Moose • Yay, we have time for this!
  43. 43. Better Programming Practices Class::MOP • CLOS for Perl 5. • 20 year old Lisp technology. • Moose is syntactic sugar.
  44. 44. Better Programming Practices Moose = Better API • Better Accessors • Lazy Builders • Less Code • More Tests
  45. 45. Better Programming Practices Not Invented Here • Moose is better than what you would write • It is written by many people • It has a huge test suite • You can do more with Moose
  46. 46. Better Programming Practices Slow? • Moose is not slow • Moose is slower • Mostly Startup • Linear Scalability (constant time) • Use more hardware
  47. 47. Better Programming Practices Incremental • Add where needed • Refactor when the time is right • “Progressive Enhancement”
  48. 48. Better Programming Practices Immediate • Type Checking • (Yes, it still is Perl) • ‘ArrayRef[MyObject]’ works in Moose
  49. 49. Better Programming Practices A Word of Caution • Everything will look like a Role • That’s normal • Resist the temptation, then build a role.

×