Readable Perl

Despite what you often hear, one can produce very readable perl code.
It is just not enforced.

I feel that readabilty of my code has improved since I've started my IT bachelor studies. I'm wondering if it is because
- we are forced to learn java which enforces a lot of things
- I am much worse at java than at I am at perl which forces me to write really simple code
- I code more and am humiliated more often when trying to read my own code
- I have more confidence in my solutions and think longer before hacking.

I'm not very afraid of baby perl. I want to code efficiently and want to understand my code in a year or two.

As an example, last evening I sat one hour or so over a calculation I could not solve. I was tired, did it in excel and then tried to implement it in my listing. The line became longer and longer, the veriables more and more nested. A true monster! And - of course - it did not work.
This morning I looked at it afresh, removed the monster, replaced some sub-calculations with variables and now it works.
It is not efficient code, nor is it as short as it could be. It is even a little stupid.
But hey, I understand it without thinking a lot.

So, do you optimize your code for readability?
(Not only when you have to?) ;-)


I almost always optimize for maintainability, which means readability among other things. In general, I sacrifice readability only when I see no way out (because I cannot devise an equally acceptable simpler-to-read solution) or when I am too frustrated and just want to be done with whatever I'm doing.

Both cases end up well commented. In the first case, I can explain just what is going on. In the second case, I can include a note of frustration with some apology to my future self. No matter when, when I come back to the code I will be happy with my younger self: either I produced easy-to-maintain code or I explained why I did not.

Having code that is a pleasure to work on again after a year's hiatus is a huge reward to a small investment.

Yes, Perl requires you to bring your own discipline (though, in any way that actually matters, it requires this no more than any other language.) But "readable" has a different definition if you only read at a first-grade level and it seems that this is too often the definition used by those who criticize Perl's "readability".

Skimability is far more important for maintenance, and you can only get this from using appropriate abstractions, code composition, method/variable names, and density. Not all languages give you the tools you need for creating the right abstractions and code composition. Even fewer allow you to pair those with density the way Perl does.

Leave a comment

About :m)

user-pic I blog about Perl.