Tuesday, July 29, 2008

Stepanov Programming Principle

(from Note on Programming, 2006 book)

I found Stepanov 's book (he is the father of Standard Template Library) while searching for material to enhance my C++ coding skill. You can find a lot of his love for programming in his writing. Here is an extraction: "Programming is a wonderful activity that goes well beyond the range of what a single programmer can experience in a life time".

He said that "The book does not present scholarly consensus. It present my personal opinions... The book does not attempt to solve complicated problems. It will attempt to solve very simple problems which most people find trivial: minimum, maximum, linear search and swap. These problem are not however simple as they seem. I have been forced to go back and revisit my view of them many times."

He learn from his experience that we should solve simple problems carefully, and master them in order to solve complicated ones. He said "I do understand that most people have to design system somewhat more complex than maximum and minimum. But I urge them to consider the following: unless they can design a three line program well, why would they be able to design a hundred thousand line program".

So, before you coding, you should read Stepanov Programming Principle:

  • The code should be partitioned into functions
  • Every function should be at most 20 lines of code
  • Function shouldn't depend on the global state but only on the arguments
  • Every function is either general or app specific. Where general function is useful for other apps
  • Every function that could be made general should be made general
  • The interface to every function should be documented
  • The global state should be documented by describing both semantics of individual variable and the global invariant

By appling that principle. He got a nice result:

  • The code didn't contain serious bugs
  • 95% of the code was in general functions !

His conclusion:

  • Decomposing an app into a collection of general purpose algorithm and data structure make it robust
  • General code grow with the grow of app size

His believe:

  • In most desktop apps, the non-general code should be well under 1%

You can read his whole book Note on Programming, 2006 (free) he used to teach programming at Adobe. It's not about C++ and STL. It's about what his learn from his programming career.

No comments: