Structure over Function

From WikiContent

Revision as of 12:36, 2 December 2008 by PeterSommerlad (Talk | contribs)
Jump to: navigation, search

When learning how to program, the function of a piece of code is the most important thing to get right after the syntax of the programming language is mastered. Many non-professionals are easily satisfied with code that (somehow) works. Do not stop there.

This early satisfaction with function is bad. Code must be clean to enable its evolution. Cleanliness means good structure. Good or bad structure comes at many levels, from the amount and order of statements in a loop or conditional, through the nesting of such control structures, through the statements within a function, the functions within a module or class, the relationships of a piece of code to other pieces up to the architecture: the structure of and dependencies between subsystems.

Bad structure can occur at all levels, but good structure starts from the ground up. Keep your program code clean and understandable from the statement level on. Consider your code is not "working" unless it is actually working in a way that you and other professionals can easily understand.

There is a well-known psychological barrier of understanding that is often told as seven plus/minus two, meaning that it is easy to grasp between five to nine different things at once but not more. If you need to handle more things at once, you either need to concentrate very hard or usually your brain starts to group elements implicitly to keep the picture of the whole.

Try to keep your code organized and use this five to nine elements at a given level of abstraction as a guideline to judge when better structure is needed. Other principles apply as well. Next to the size the number of relationships should be minimized. Notice that there are not only explicit but often also implicit relationships. Understand and manage the number of relationships. Loosen coupling as much as it makes sense. On all levels of abstraction. Refactor your code to keep its structure within understandable boundaries.

by Peter Sommerlad

This work is licensed under a Creative Commons Attribution 3

Back to 97 Things Every Programmer Should Know home page

Personal tools