Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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.

In reply to Re: Code Maintainability by GrandFather
in thread Code Maintainability by mpeever

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-29 07:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found