Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What makes Perl code maintainable?

I've been writing Perl for several years now and it is my preferred language for text processing (many of the genetics/genomics problems I work on are easily reduced to text processing problems). Perl as a language can be very forgiving, and it's possible to write very poor, but functional, code in Perl. Just the other day, my friend said he calls Perl a write-only language: write it once, understand it once, and never ever try to go back and fix it after it's finished.

While I have definitely been guilty of writing bad scripts at times, I feel like I have also written some very clear and maintainable code in Perl. However, if someone asked me what makes the code clear and maintainable, I wouldn't be able to give a confident answer.

What makes Perl code maintainable? Or maybe a better question is what makes Perl code hard to maintain? Let's assume I'm not the only one that will be maintaining the code, and that the other contributors, like me, are not professional Perl programmers but scientists with programming experience.

like image 843
Daniel Standage Avatar asked Dec 03 '10 19:12

Daniel Standage


People also ask

Is Perl readable?

It's an easy read — a beach read. Reading good Perl code is like reading Shakespeare. Every line is deeply expressive, both in its content and its rhythm. You probably need a dictionary to look up some words that, while not perhaps in everyday usage, express exactly what the author intended.

Is Perl programming still relevant?

While Perl might seem like an outdated scripting language, it still has plenty of relevant uses today. If you love UNIX/Linux/BSD like me, then you have definitely learnt Perl and programmed in it.


2 Answers

What makes Perl code unmaintainable? Pretty much anything that makes any other program unmaintainable. Assuming anything other than a short script intended to carry out a well defined task, these are:

  • Global variables
  • Lack of separation of concerns: Monolithic scripts
  • NOT using self-documenting identifiers (variable names and method names). E.g. you should know what a variable's purpose is from its name. $c bad. $count better. $token_count good.
    • Spell identifiers out. Program size is no longer of paramount concern.
    • A subroutine or method called doWork doesn't say anything
    • Make it easy to find the source of symbols from another package. Either use explicit package prefix, or explicitly import every symbol used via use MyModule qw(list of imports).
  • Perl-specific:
    • Over-reliance on short-cuts and obscure builtin variables
    • Abuse of subroutine prototypes
    • not using strict and not using warnings
  • Reinventing the wheel rather than using established libraries
  • Not using a consistent indentation style
  • Not using horizontal and vertical white space to guide the reader

etc etc etc.

Basically, if you think Perl is -f>@+?*<.-&'_:$#/%!, and you aspire to write stuff like that in production code, then, yeah, you'll have problems.

People tend to confuse stuff Perl programmers do for fun (e.g., JAPHs, golf etc) with what good Perl programs are supposed to look like.

I am still unclear on how they are able to separate in their minds code written for IOCCC from maintainable C.

like image 86
Sinan Ünür Avatar answered Oct 13 '22 20:10

Sinan Ünür


I suggest:

  1. Don't get too clever with the Perl. If you start playing golf with the code, it's going to result in harder-to-read code. The code you write needs to be readable and clear more than it needs to be clever.
  2. Document the code. If it's a module, add POD describing typical usage and methods. If it's a program, add POD to describe command line options and typical usage. If there's a hairy algorithm, document it and provide references (URLs) if possible.
  3. Use the /.../x form of regular expressions, and document them. Not everyone understands regexes well.
  4. Know what coupling is, and the pros/cons of high/low coupling.
  5. Know what cohesion is, and the pros/cons of high/low cohesion.
  6. Use modules appropriately. A nice well-defined, well-contained concept makes a great module. Reuse of such modules is the goal. Don't use modules simply to reduce the size of a monolithic program.
  7. Write unit tests for you code. A good test suite will not only allow you to prove your code is working today, but tomorrow as well. It will also let you make bolder changes in the future, with confidence that you are not breaking older applications. If you do break things, then, well, your tests suite wasn't broad enough.

But overall, the fact that you care enough about maintainability to ask a question about it, tells me that you're already in a good place and thinking the right way.

like image 15
Paul Beckingham Avatar answered Oct 13 '22 20:10

Paul Beckingham