Monday, September 12, 2011

Ugly business makes for ugly code

A number of associates bemoan the state of their code. More specifically, they complain of the complexity of the code, and of the time and effort required to implement changes and new features.

The complexity of code is the sum of two elements: the complexity of the business and the complexity of the source code. Complexity of the business is decided by the management of the business; complexity of the code is a bit more subtle.

A program can be written in multiple ways; some simple and some complex. In my experience, non-trivial programs can be complicated or simple (some might use the word "elegant"), but the time for solutions is different. Complicated solutions can be written quickly; elegant solutions require more time. A "quick and dirty" solution is just what is sounds like: hastily assembled code that gets the job done but is a jumble of logic.

Simple code, code that is easy to maintain, is harder to write than jumbled code. Simple code is the result of jumbled code that is refined and improved through multiple iterations of design. I know of no programmers that write simple, elegant code from the get-go. They all start with messy code, confirm that they have the correct behavior, and then refactor the code. Those refinements take time and skill.

The one truth about simple code is this:

If you want simple code, you have to pay for it.

But there is a limit to the simplicity of your code. That limit is defined by your business. Your business has operational requirements; your programs must meet those requirements.

The one truth about complexity is this:

No amount of programming will simplify complex business rules. If you have a complex business, you will have complex programs.

How you choose to run your business is, well, your business. But consider this: complex programs cost more to maintain than simple programs. The business benefit of complex rules must yield enough additional revenue to cover the cost of the additional maintenance; if not, you are spending money for a net negative return on investment.

No comments: