Front page | perl.qa |
Postings from February 2012
Re: What is the "best" code on the CPAN?
February 8, 2012 09:06
Re: What is the "best" code on the CPAN?
Message ID: 1328720780.12637.YahooMailNeo@web162101.mail.bf1.yahoo.com
While the Perl::Critic and TB2 examples may be good code, I submit that they're also complicated enough that it may be very hard for a new developers to really understand them due to their sheer size. Even for experienced developers, they may be nerve-wracking as a "learning experience". For smaller problems, though, they may not have enough of the characteristics you list to qualify as "best".
Perhaps you can find smaller examples of code which fit interesting problem spaces and are well-designed? For example, I think you'll find my HTML::TokeParser simple is reasonably well designed, even though it's a disguised factory which inherits from an awkward interface. It was written in the pre-Moose days, so it doesn't fit your criteria.
However, it shows good use of inheritance (a tricky thing to do!) because base classes I wrote are abstract and its polymorphism is effective in making many methods simply a single line of code. On top of that, it also obeys Liskov because you can drop it into a code which uses HTML::TokeParser and it will still magically work, even as you gradually replace stuff like this:
my $text = $token-> =~ /^(?:S|E|PI)$/ )
? $text = $token->[-1]
: $text = $token->;
my $text = $token->as_is;
(get_text() is a better name, but the base class used it for something else and overriding a method to provide semantically different behavior is a Bad Thing)
For bonus education points: grab the older versions from the Backpan and watch how it evolved from a steaming pile of ones and zeros to something tolerable (for its time -- today I would be tempted to rewrite with Moose, but why rewrite working code?)
TL;DR: Matching of your criteria would be hard in a code base small enough to get your head around easily but maybe something smaller helps?
Live and work overseas - http://overseas-exile.blogspot.com/
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog - http://blogs.perl.org/users/ovid/
Twitter - http://twitter.com/OvidPerl/
> From: Jeffrey Thalhammer <firstname.lastname@example.org>
>To: Perl QA <email@example.com>
>Sent: Wednesday, 8 February 2012, 3:29
>Subject: What is the "best" code on the CPAN?
>I'm working with a group of Perl developers with various backgrounds and skill levels. We have instituted a fairly effective code inspection process, but we still get bogged down in debates over what "good code" is. Unfortunately, the developers work in isolated silos and spend the vast majority of the time looking only at their own code. So they rarely have an opportunity to see what good (or just better) code might actually look like.
>I want the team to see how to do things right, rather than debating all the ways to do it wrong. So for our next code inspection, I want them to study some "good" code from the CPAN. So the question is, which distribution provides the best example. These are the things I think I want in such an example (in no particular order):
>Object-orientation using Moose.
>Prudent use of Perl idioms without being overly clever.
>A discernible architecture and clear separation of concerns.
>Strong examples of encapsulation / inheritance / polymorphism.
>Demonstrates inversion-of-control principles.
>Well named variables and subroutines.
>Well factored code with minimal complexity.
>A clear pattern for extension and reuse.
>Useful documentation (e.g. POD, comments).
>High-value tests at the functional and unit level.
>Perl::Critic compliance (any set of Policies will do).
>Effective use of other CPAN modules for routine tasks.
>Effective error handling.
>Effective use of Roles.
>Robust and consistent API.
>So in your opinion, which distribution on the CPAN best demonstrates these qualities? Or do you think there are other more important qualities that I should be looking for? I realize there is more than one way to do it, so I don't really expect to find the "best" code. I just want something I can hold up as strong example that people (including myself) can learn from and aspire to.