Program code, whether it's written in Perl or another language, can range from lucid to occult, depending on how intelligently the programmer documents it. But in Perl programming, according to Damian Conways' Perl Best Practices, lucid code means much more than judicious commenting. From the arcane (not padding values with leading zeroes to make them line up) to the esoteric (factoring out common affixes from alternations to avoid endless re-matching), these practices can help lift your Perl code from spaghetti to elegance.
Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
—Introduction to Perl Best Practices
Conway begins with a discussion of why programming style matters, and why it's important to make your style consistent and accessible. In this, he cites three goals for "best" code: robustness, efficiency and maintainability; and he gives plenty of examples to show the merit of each. He quotes Larry Wall, the originator of Perl, "We do not all have to write like Faulkner, or program like Dijkstra. I will gladly tell people what my programming style is, and I will even tell them where I think their own style is unclear or makes me jump through mental hoops. But I do this as a fellow programmer, not as the Perl god..."
Having cited the Perl god in support of his effort, Conway launches right into the practices themselves. He begins with "Code Layout" and "Naming Conventions" to show how top-down management of code can make the process easier, and open later stages of a programming project to lucidity. His simple suggestions (Never place two statements on the same line. Abbreviate only when the meaning remains unambiguous) are logical, and need little justification when each is taken alone. Together, these two chapters lay easy-to-understand foundations for Conway's dive into the abstrusities of Perl.
Quoting Arthur Norman ("Data is semi-animate... sort of like programmers."), Conway next discusses "Values and Expressions," where he points out some of the pitfalls of applying any style-law across the board without considering it fully. For example, he notes, blindly following a code-layout rule to align things vertically doesn't make sense if you add leading zeros to a values table to make them align. Perl takes the leading zeros to denote octal numbers, and your carefully-entered values become something quite different than you intended.







Article comments