• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Better Perl Practices
 

Better Perl Practices

on

  • 1,726 views

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

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.

Statistics

Views

Total Views
1,726
Views on SlideShare
1,646
Embed Views
80

Actions

Likes
0
Downloads
19
Comments
0

4 Embeds 80

http://wiki.perlassociation.org 52
http://our.coldhardcode.com 21
http://www.slideshare.net 4
http://www.coldhardcode.com 3

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • <br />
  • <br />
  • Best is theoretical, it is something that cannot truly be reached because you can’t ever really know. It is an impossible goal, so lets just be better. <br /> We can build applications better, we know this because we can look at how other people do things. <br />
  • Even people who do things better than others can still be better. I know this because I’ve known people with very good practices still complain about shortcomings. Whether it is lacking test coverage, QA, staging, there is usually something missing and sometimes the complaints turn to whine. <br /> <br /> Well, we’re developers. We fix problems, right? So just fix it. <br />
  • When you look backwards, there are a lot of moments where you can point out flaws. Either your own or others, but this is very important. When you do this, you can learn and try to think of ways to do things better. Better yet, look at what other people have done. This is where the Perl Community (p5 porters, CPAN) really shines, they have all these tools built for you. Let them make the mistakes, and you learn. <br />
  • After acknowledging your failures, the next step is to analyze your next attempt at a problem. This is where studying other people works, but you have to get rid of the Not Invented Here attitude. <br />
  • Dropping all that, accepting your own limitations and doing better will make you happy. It will make you happy because you’re not trying to pick up a mountain. You’re simply enhancing something you’ve always done. If you start jogging, you don’t run a marathon. Start running a half kilometer at a time and work your way up. Perfect your technique, tempo, strategies. This all falls into 3 concepts of what constitutes “Software Development”. <br />
  • The first, very easy and very vague. <br /> The best way to enhance your strategy is to do an initial look at design. Design is 100% about goals. Defining your goals in your design step means your implementation will be complete, and hopefully robust. <br /> Another aspect of design is figuring out your role in the project. If you’re not a frontend guy, don’t venture into that territory. It’s hard to let go of some control, but you really can’t do it all. Design what falls in your realm, set those goals, and let other do the same. <br />
  • Testing is equally important, but testing happens iteratively and as time goes on your code progresses in maturity; your tests will increase in quality and coverage. A test without a design isn’t going to do much more than tell you what you already know. <br /> Another point is to not get fixated on code coverage., At least during iterative development phases, it is better to work on testing your API and not focus on code coverage. The reason is simple, in the beginning of a project a percentage of the code initially written will not be there at the end. This is refactoring. <br />
  • Yes, you will have to refactor code. <br /> This is why you have to accept your flaws, because your code is flawed. The first invention of any solution isn’t the right one, but that’s ok because software development doesn’t have manufacturing costs associated with it. Enjoy deleting lots of code, do it often, just make sure you use something you can commit to frequently, preferably in branches (like git or svk). <br />
  • By building a solid and comprehensive API that is both simple to use, and makes sense, you’ll increase productivity and reduce bugs (less code rewriting) -- both things will make your boss much happier. <br />
  • In the charged world of Ruby vs Python vs Perl, or vi vs emacs, it is nice to have something to agree on. I hope we all can agree on these principles here. <br /> It’s very hard to say “No, you never refactor code.” or “Testing isn’t good!” <br /> It’s also very hard to do any of this in your own projects, and even harder to make them habits. That’s really the big problem here, since all of these points have been talked to death. <br /> So, habits. <br />
  • A lot of the burden falls on whoever maintains the development environment. For inspiration, look at the CPAN world. When a module goes to CPAN, you have Smokers (CPAN Testers) and a variety of other tools (such as Kwalitee) that help you write better code. <br /> These are -voluntary- tools built by others that you can use for your stuff. Email yourself statistics. Nag yourself, and you’ll want to do better. Setup a smoker, setup email reports and even kwalitee. Use custom perl critic specs. The CPAN world has better supportive tools than most software companies. At your company: change this! You can. Use CPAN as an inspiration, setup a smoker and a custom CPAN repository for your companies packages. <br />
  • You are the master of your own destiny, and when you establish a habit it becomes easy to drop if there isn’t an eventual reward if it is harder to maintain the habits otherwise. Having tools that help you keep them, such as Smokers and custom CPAN paths, aren’t enough. <br /> <br /> The real eventual reward for good development practices is doing your job in less time. That means you can write more fun code, or just read RSS feeds. At the very least, you can justify a raise easier. <br />
  • So, in practice, setup tools that nag you (Smokers, CPAN overlays, etc). Use iterative testing strategies to get better APIs, then ultimately write less code and do more. <br /> This is very general, though, and each environment is different. The hardest step is really the testing, and that’s because people either fixate on coverage metrics or some other detail. <br />
  • To make it easier, understand what your goal is. Testing is very different based on what you are testing. Schema classes are based on unit testing, figuring out what works and what doesn’t. What relationships need work, what is missing from your API (that is what your schema class really is) <br /> When you test an application, like a web application, you are testing workflow. The difference here is that your unit tests prove the underlying things work. Workflow tests make sure things like email verification works. <br /> [Catalyst Example] <br />
  • Tests are not to prove it works. Tests are to prove it doesn’t break, and that your API does what it should. <br /> The difference is in coverage, for the first step cover your API. After that work on complete code coverage <br /> Tests do not require more time to finish your project, unless you release really buggy software. I’ll buy a beer to anybody who can prove otherwise, unless you work for the Internet Explorer team. <br />
  • The next phase of testing after API testing and then basic coverage tests is going to be your acceptance tests. <br /> I like to think of these mostly as workflow tests make sure things like email verification works, so people can sign up. <br /> At this step, it is about aligning to specifications, the design goals and end-user acceptance (also good to count number of “clicks” and test for that) <br /> <br /> If the test is hard to write, it is hard for the user to use. Except you only have to write the test once. <br />
  • So, testing is really about incrementing the scope of what you look at. And first focusing on the API. The API is what you design, so testing and design go hand in hand. <br /> <br /> The first step of design is a rough idea of what you are after. Think of clay modeling, you don’t start with a finalized product, you shape it from rough forms. Software is, fortunately, nothing like that -- we can throw away and replace without looking at the full form. <br /> <br /> So, think about what you want. Come up with a nice API for it, and then start filling in the methods will practical code. If a method sucks, get rid of it. Your tests will help you while you flesh this out to figure out what you want. Something I usually do is write out an “Ideal syntax sketch” that just pretends all my API functions work. Often times this turns into my first API test. <br /> <br />
  • Now, after you get a rough idea of how your API should look you can think about backend implementations. This is far too similar to computer science courses where you code the methods giving the example, for this, I’m sorry. But it is a good way to do things. <br /> <br /> Fortunately, you can cheat. This is where you find CPAN modules that do the work, or some other algorithm on Google. CPAN is better, because you can steal the tests, too. <br />
  • So when you have your methods built in, your tests cover the API and you can handle (via unit tests) all the individual points of a project it is time to put everything together and build a real application. <br /> <br /> In terms of cooking, lower the heat and start simmering. This is the boring work, everything is done and you’re assembling it. Hopefully, every project gets more boring since you can rely on code reuse from previous projects. By having everything in tested methods, you’ll know what things can and cannot do. <br /> <br /> Also, if a method needs to be changed you can do that. With the API tests, nothing will break, unless your test is broken. It’s fluid, easily changed and very, very robust. <br />
  • <br />
  • The other thing, is that a lot of computer science ideas come from an age where desktops were the real applications, and web applications were simply scripts. <br /> <br /> I’m guessing that all but a few here are working on web applications that rival very large desktop applications. <br /> <br /> Good thing, because often times we don’t know how good we have it. We control everything. We know what hardware, and however much we want to complain about how certain browsers make our lives difficult, imagine trying to support a desktop application on unknown hardware, with a lot of other variables. I’ve done it. It’s terrible. <br /> <br /> Web applications are easier, even considering how hard Microsoft makes it. So, stop thinking about software in terms of a desktop application. We control everything, and even Microsoft provides free virtual PC images of all versions of IE. <br /> <br /> Release early, release often, don’t fear change because you have the tests to make it safe. <br />
  • So, the key to a solid application is to remember your environment, don’t overstep your goals, test as you go and finally, use the tools available. Definitely smoke servers and sandbox development environments. These tools will help you so much. <br /> <br /> And, please, don’t reinvent wheels. CPAN has most things out there, and the hardest part is finding out the right module, since there are usually 5. Getting involved in the community helps, but looking at the unit tests of the modules is a really good litmus test for how good a module is, and how attentive an author is to the packages. <br />
  • To move into Perl specific areas, lets go over what makes better Perl. Applying my previous concepts, a bit part of that is looking at what is out on CPAN now and making the best usage of it. <br /> <br /> For now, lets break this into two categories. The first is testing. <br />
  • A lot of times you see code that is highly repetitive, like fetching parameters out of a request or something. Even if this code is simple, if you find yourself typing the same chain of commands it isn’t very good. <br /> <br /> <br />
  • So, simplify it. Store a reference. You can spend the couple of bytes for the scalar reference to the hash. It saves you typing, and cleans up code. <br />
  • Just because your creating shorthand syntax, doesn’t mean it should turn into Golf. It’s the exact opposite, really. You’re typing less by creating more variables and then saving yourself a lot of trouble later. <br /> <br /> Don’t use stupid obscure code like this. Even if you can write it, you shouldn’t. <br />
  • That code example would die with strict turned on, as it should. Always code with warnings and strict turned on. If you don’t, you don’t know what your code is doing. People seem to have forgotten that back when C was the highest level language, you had to initialize all your variables or you got random data in them. <br /> <br /> Don’t introduce entropy into your programs, use strict to keep things sane. <br />
  • Then, if you get another source of input, you only have to change one type. This is a contrived example, but I’ve had similar things come up in practice. Being able to normalize your data into a very easy to query structure will save you in the long run. The key there is easy to query, you don’t have to obfuscate things. <br />
  • And for testing, use Test::Class if you don’t already have a testing system you know and love. Ovid has written a lot on using Test::Class at modernperlbooks.com, it is easy to follow and very very useful. <br /> <br /> <br /> <br /> <br />
  • I completed 3 different projects, with the intention of using Test::Class and better testing policies. I failed miserably up until the end of the final project. <br /> This involved me talking to people at length, and finally figuring out the “right way”. The big thing is that you do have to change the way you write your code slightly, but more importantly really change the way you think about your tests. They’re not encapsulated as tiny bite sized morsels, because then you end up with no good way to share your code between tests. Test::Class helps this, by applying some structure and a development pattern. <br /> It takes a few times to learn how to do it right. Ovid and Chromatic probably forgot that, but it’s ok to fail -- just take a moment after you fail to figure out why. <br /> <br /> <br /> <br />
  • With the learning curve on doing these tests right, it may not be obvious why you should use it. There are a lot of benefits, and it really is worth it. <br /> <br /> You won’t have the problem of copy and pasting code from one .t file to another. <br /> Test packages make more sense, you can mix and match them. You can extend them, and override them. Your tests will come together quicker, better and generally be friendlier. <br />
  • Base classes are also somewhat related to Test::Class. You save your test data in an upstream class. Base classes are a very simple concept, and Perl supports multiple inheritance (in that a package can have two or more parent classes). <br /> <br /> So, you can easily get away with writing less code and doing more by using base classes. It does take different ways of thinking about the code you write though. <br />
  • Building base classes is, in some ways, a puzzle game. If you like this sort of thing, you’ll have fun. If not, it is yak shaving. Either way, it is better to do it. One thing is really thinking about the real differences. <br />
  • Base classes are great, but it will take a bit of practice to get used to building good classes. In my RESTful Catalyst applications, I have many packages that consist of nothing more than configuration blocks and an inheritance line. <br /> The base class is very complex, works with configuration, but it covers all the use cases I have and to use it I just inherit from it, and muck with config. It is very, very fast to develop with. I also can get away with rigorous testing of just the base class and feel confident it works. <br /> When I introduce new functionality, I start first with my existing tests and work from there, since the new stuff tends to be very complicated it is better to have a scratch pad. <br /> <br />
  • I’m a big fan of Moose, but this isn’t a Moose talk. This is about writing better code, and I believe that Moose does that (and more). <br /> Just as a simple introduction, Moose is syntactic sugar over Class::MOP. Class::MOP is an API into Perl’s OO capabilities. <br /> <br /> Effectively, it is just a layer on top of what Perl gives you that does fantastic things to your code. I’m not going to go into all the fantastic things, because then it would be a Moose talk. <br />
  • Instead, I’m going to talk about what you need to write better Perl code. A big problem is that if you think of Moose (or any OO-API package that is on CPAN) as nothing more than accessor generators and mutators, you’re doing it wrong. <br /> <br /> Moose handles much more. Introspection, reflection, method modifiers, mutable classes, immutable classes. It has an API into the meta definitions. <br /> <br /> But that’s a lot, and you may not need it. If you don’t, there is a minimal set of features that is called ‘Mouse’. You can use that, and speed things up and still write better code. <br />
  • One thing that Moose makes really easy is configuration. This is done through the idea of ‘Roles’. Roles are a very complex subject that requires a Moose talk, and this isn’t a Moose talk, so I’m just going to say that Roles are a way of mixing in functionality. The main difference can be thought of as “A class IS A” and “A role DOES”. It’s an action. <br /> <br /> Configuration is an action, and there is a fantastic role on CPAN that makes it all happen. <br />
  • I configure most things in my application, and one thing is that I tend to have config files in different locations. Sometimes I like to override things by having a “appname_local” configuration file that gets loaded if it exists, but that I instruct git or svn to ignore. <br /> <br /> This lets me do that, by just populating a list of locations to look for a file. While it doesn’t merge multiple files (which would be awesome), it is the next best thing right now. <br /> <br /> So here you can have multiple paths for configuration, and then they set the accessors you have generated. <br /> <br /> So go read up on the pod for MooseX::SimpleConfig and start using it, it’s on CPAN. <br /> <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />
  • <br />

Better Perl Practices Better Perl Practices Presentation Transcript

  • Better Programming Practices  When Best isn’t good enough
  • 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
  • Better Programming Practices Better • Useful techniques for better applications • Best is for Knuth, we build applications
  • Better Programming Practices Why? • We all complain • Sometimes we whine • JFDI
  • Better Programming Practices Step 1: You’re Wrong • Hopefully just the first time • Better to be wrong and learn, than to never learn.
  • 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
  • Better Programming Practices Step 3: Be Happy • Don’t be perfect • Perfect your technique • Be Happy (Or at least your boss will be)
  • Better Programming Practices Design • Goals! • Understand your role • Don’t over-extend • Don’t let others, either.
  • 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
  • Better Programming Practices Refactoring • This is inevitable • Make it count • Learn!
  • Better Programming Practices It’s about the API • Make it simple on yourself • Expose flaws in tests • Impress your boss
  • Better Programming Practices These are good • We can all agree: Design, Test, Refactor • But we never do them. • Habits!
  • 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
  • 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
  • Better Programming Practices Habits in Practice • The Hows: • How to (test|design|refactor)
  • Better Programming Practices How to test • Schema classes • SQLite • Unit Tests • Applications • Test::Class, Test::FITesque • Workflow Tests
  • Better Programming Practices How not to test • ok(1, ‘it works’);
  • Better Programming Practices Testing • Lots of Good Tools: • Test::Class • Email::Send::Test • Test::WWW::Mechanize
  • Better Programming Practices How to design • First, get something working • Syntax Sketch • Establish individual goals (methods) •
  • Better Programming Practices Not Unique • You are probably not the first to solve the problem • Find similar functionality
  • Better Programming Practices Iterative • Simmer & Reduce • That is your secret sauce • Software is fluid, not stone
  • Better Programming Practices Clever Japanese •
  • Better Programming Practices Web != Desktop • You control the environment • You control the deployment • Slow? Scalability! Add another server. • Apple would kill to have this
  • Better Programming Practices Better Applications • Your software is only as good as its tools • Don’t reinvent wheels
  • Better Perl Practices Better Perl
  • Better Programming Practices Simple Syntax my $foo = $request->params->{foo}; my $bar = $request->params->{bar};
  • Better Programming Practices good code, type my $data = $request->params; my $foo = $data->{foo}; my $bar = $data->{bar};
  • Better Programming Practices Type Less is not my $s=($api?$api->params:$request- >params)||$request->params; ${“__PACKAGE__::$_”}=($s->{$_}) for keys%$s;
  • Better Programming Practices warnings & strict use warnings; use strict;
  • Better Programming Practices good code, type my $data = {map{$_=>$request- >params->{$_}}@keys my $foo = $data->{foo}; my $bar = $data->{bar};
  • 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/
  • Better Perl Practices I failed 3 times
  • 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
  • Better Programming Practices Base Classes • Common Functionality • Easy to change • Awesome in Perl
  • Better Programming Practices Find Dierences
  • Better Programming Practices Do More with Less • Make code better • Write less • Next step: use Moose;
  • Better Programming Practices Moose 101 • This is not a Moose talk • Perl API to Perl OO • Keep it Simple
  • 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;’
  • 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)
  • Better Programming Practices Favorite Recipe Ever with 'MooseX::SimpleConfig'; with 'MooseX::Getopt'; has +configfile => ( default => ( grep { defined $_ and -f $_ } @places_to_look )[0]||”” );
  • Better Programming Practices To Recap • Write less code (Base classes) • Test where it matters • Use Moose (Write less code) • Config!
  • Better Programming Practices Moose • Yay, we have time for this!
  • Better Programming Practices Class::MOP • CLOS for Perl 5. • 20 year old Lisp technology. • Moose is syntactic sugar.
  • Better Programming Practices Moose = Better API • Better Accessors • Lazy Builders • Less Code • More Tests
  • 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
  • Better Programming Practices Slow? • Moose is not slow • Moose is slower • Mostly Startup • Linear Scalability (constant time) • Use more hardware
  • Better Programming Practices Incremental • Add where needed • Refactor when the time is right • “Progressive Enhancement”
  • Better Programming Practices Immediate • Type Checking • (Yes, it still is Perl) • ‘ArrayRef[MyObject]’ works in Moose
  • Better Programming Practices A Word of Caution • Everything will look like a Role • That’s normal • Resist the temptation, then build a role.