Structure over Function

From WikiContent

Revision as of 12:37, 27 March 2009 by PeterSommerlad (Talk | contribs)
Jump to: navigation, search

When learning to program, the function of a piece of code is the most important thing to get right once you have mastered the syntax of the programming language. Many programmers are easily satisfied with code that (somehow) works. Please, don't stop there. This early satisfaction with function can be counterproductive.

Code needs to be clean to allow smooth evolution. Cleanliness means good structure. Significant structure exists at many levels: the number and order of statements in loop and conditional blocks; the nesting within control structures; the statements within a function; the functions within a module or class; the relationships between one piece of code and others; the partitioning 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 up. Consider your code as not "working" unless it is actually working in a way that you and other programmers can easily understand.

There are limits to our cognitive capacity depending on our state of mind. If you concentrate very hard, you might be able to grasp more individual elements at once. However, usually our brain starts to group elements implicitly to keep some coherent picture of the whole. Or, just fails to see the wood from the trees.

Try to keep your code organized and limit the number of elements or group them at a given level of abstraction as a guideline to judge when better structure is needed. Other principles apply as well. After the size, the number of relationships should be minimized. Notice that relationships can be implicit as well as explicit. 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