Tim\'s picture      Blogging Ottinger (tim)

2005-July-27

Behold! The Function!

Filed under: Programming

When it comes to software design, in procedural languages, the primary unit of design is the function or method. Even in OO, don’t start to think it’s not so. If your functions and methods are well-designed, the whole program will be more easily understood and more easily maintained.

So how do you design a function? The first and biggest thing to remember is that a function performs A FUNCTION. One meaningful operation, done completely and well. That means that the function should be relatively easy to name, or at least describe in a very short sentence. Now this is true of user-visible functions. There may be private functions (defined within other functions or in classes), and there is a little more slack given for the invisible.

The second feature of good function decomposition is that a function has limited effect: no side effects. The name of the function and the parameter list should be good documentation, and one should not have to go poking around in variables and the file system and memory locations to see if something else happened.

A third feature is that the function is dumb. It lives by need-to-know. If it can operate on less information than it is given, then it is rewritten to use less. It also doesn’t require more intelligent parameters than it has to. It doesn’t seek out information from its environment if that information is not necessary for the task at hand.

Finally, the function is complete, so it doesn’t need to pass back a whole lot of complex data for further processing. Instead, it reports the information it was created to collect in an obvious and minimal useful form. The data set returned is the obvious data one would expect from any function so-named, and in harmony with a single-sentence explanation of the function.

Rephrase: the number of useful operations in a function should approach 1 (one), the number of side-effects should approach 0 (zero), the number of parameters is as close to 0 (zero) as you can get it, and the amount of unnecessary data carried by the parameters is as close to 0 (zero) as practical. And the result set is as simple and small as possible.

1 Comment »

The URI to TrackBack this entry is: http://tottinge.blogsome.com/2005/07/27/behold-the-function/trackback/

  1. Amen brother Tim!!!

    Comment by Walter Moore — 2005-July-28 @ 01:29

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome | Theme designs available here