Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Code Maintainability

by GrandFather (Saint)
on Dec 04, 2008 at 04:21 UTC ( [id://727867]=note: print w/replies, xml ) Need Help??


in reply to Code Maintainability

Very nice, especially as you seem to have copied it straight out of my (mental) guide book (how did you manage that!).

Two related area you've not covered are the question of line length and nesting. One might think that long lines are a good idea now that we have wide screen monitors which can easily display several hundred characters on a line - more code per line, yay! Well, think again. Part of the art of structuring code so that it is maintainable is breaking it up into comprehensible units. Like long sentences, long lines are hard to comprehend. Deeply nested code is hard to comprehend.

Historically 80 character lines were a hard limit. In practice 80 character lines is a very satisfactory length. It's long enough to express as much complexity as is generally required and short enough to limit the complexity of reasonable code to a chunk that can be teased apart and understood without resort to taking off shoes and socks to count brackets. 80 character lines also means that with most editors it's easy to have two edit windows side by side, and that is often very useful.

Nesting is related to line length in as much as the deeper you nest control structures (if statements and loops) the less room you have on the line to put interesting stuff. It's also related in the sense of being able to comprehend the code. With deeply nested code, especially with nested conditional code, it can be very difficult to figure out when a particular line or block is executed.

A technique that can help avoid nesting is to take advantage of early exits from sub and loops. Very often the changing the sense of a test allows a short block containing a little code that handles a special case and ends with a return, next or last. The if is then followed by the general case code without needing extra nesting. That gives a double benefit - you avoid nesting and it's much easier to identify the different case code and how you get there.


Perl's payment curve coincides with its learning curve.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://727867]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-04-20 08:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found