When writing scientific software, my most important consideration (behind correctness and speed) is the maintainability of the source code. This is important not only so that others can reproduce my contributions to the community, but also for my own sake and sanity. Trying to edit source code you’ve ignored for 6 months (especially Perl code) is hard enough anyway. If you don’t take the time to write clean code, it just makes it that much more frustrating.
This week I discovered two static code analysis tools for Perl that should be useful to anyone concerned about the quality of their code: perltidy and perlcritic. The latter is designed to critique your code based on published best practices, while the former is designed to tidy up your code for you. perltidy is a script that is run from the command line, while perlcritic can be run from the command line, from your own code via the
Perl::Critic module, or from their web service.
Over the last few years, I’ve developed my own ad hoc standards for clean Perl code. For the important details, my code is pretty similar to the standards recommended and enforced by perltidy and perlcritic. However, as I increase the severity of the perlcritic critique, it finds quite a few issues with my code. I’m not concerned that all of this is necessarily an indication of bad code–after all, coding style and maintainability is as much about style and preference as it is about structure and correctness. However, I am going to review each recommendation seriously and improve my code accordingly (unless I have a darn good reason to ignore the warning).
For a biologist just starting out with Perl programming, these two tools are a wonderful way of helping you to write good, clean, and maintainable Perl code!