website/content/blog/complexity.md

7.8 KiB

img title author pdate desc
/img/complexity.jpg Complexity Jeff Russo April 19,2020 What is complexity as it relates to software?

The most relevant definition of complexity for this blog post is: (adj) "Not easy to analyze or understand; complicated or intricate." Intuition of complexity is important if you are designing, writing, or reading software. If you are purchasing software, it is important to prioritize simpler options; it will be cheaper in the long run. In this blog post, I will discuss identifying complexity, why it's bad, why simple is good, and how software engineers manage complexity.

Identifying Complexity

Before being able to manage complexity, it must be identified. Along with the definition, it is helpful to know indicators and examples. Seasoned engineers sometimes call these smells; complexity is not black and white, but more of an intuition. Things that currently appear simple may become complex in the future.

Indicators

  • Incomprehensible: Something complex is difficult to comprehend or articulate. It may have too many parts, or the interaction between the parts is complicated.

  • Fragile to change: Complex things are not easily modified. An internal combustion engine is complex. If one was to modify the bore of the cylinders, special machining and new parts are required.

  • Multi-purpose: A tool that can, "do it all," usually does nothing well due to its complexity.

Examples

  • Run on sentences: Sentences that have too many clauses become complex because they are trying to express several independent, but possibly related, although sometimes unrelated thoughts, and they take more mental energy to comprehend, causing the now frustrated reader to reread it until they can ascertain the author's meaning.

  • Chess: At a glance, chess is straightforward. There are only six types of pieces, and the board is 8x8. The complexity comes from the permutation of games. It generally takes a dedicated decade to become a grand master.

  • Humans: No two are the same. DNA on its own is complex; factoring in the expression of that DNA is beyond the understanding of any single person.

Complex Software is Undesirable

All software is complex; it is one of the most complex man-made things. Physical creations such as spaceships, submarines, and skyscrapers may be complex, but they are limited by the laws of physics. Software has no such limit. For example, FreeBSD is an operating system that powers parts of Netflix, WhatsApp, and PlayStation 4. Using scc, a tool to evaluate the size of codebases, it estimates 16 years with 500 people would be required to recreate FreeBSD 12.1.

While some complexity is unavoidable, it is best minimized. Here are a few reasons to minimize the complexity of software:

  • Expensive changes: As software gets more complex, the cost to change increases exponentially. A module bound to several business concerns is more expensive than a module that does one thing. Engineers, business representatives, and managers need to work together on multi-purpose modules instead of handing a simple task to an engineer to work on a single-purpose module.

  • Time: Time is a scarce resource; more importantly, it cannot be produced, accumulated, or recovered. When complexity creeps into software, a large time cost in incurred. Money can be generated or borrowed to offset project costs, but time cannot. This is the most undesirable attribute of complex software.

  • Engineer retention: Engineers who work on simple software run into less frustrating walls. This is more fulfilling work, and will keep engineers on the team. Engineers are less likely to take ownership of problems and code if it is part of a complex mess.

Simple is Difficult, but Effective

An antonym of complexity is simplicity. Although it is easy to explain a simple system, it is not easy to create one. This is the classic investment mindset applied to time. It is better to think twice and code once. Here are a few examples of simplifying principles:

  • Encapsulation: When the complexity of software cannot be mitigated, it can still be encapsulated. This powerful idea is what enables the internet to work smoothly; each packet of information sent across it is an envelope.

    The inner workings of a vehicle's automatic transmission are quite complex, but it is encapsulated so well that the driver simply selects "D". As Rob Pike points out in this video, the simplest interface is no interface at all; garbage collection is completely encapsulated.

  • UNIX philosophy: "Make each program do one thing well." This can save software from becoming an indomitable mess. Constantly adding features to the same system will eventually make it collapse in on itself. Maintaining this discipline is difficult, but is more effective than simply saying yes to everything.

  • Composition: Software should be composed of interchangeable modules. The boundaries between modules must be well-defined. Chess software should have two players, but those players do not need to be humans on a computer. By keeping the player module separate, it can be swapped for an AI player module. This enables better testing and more interesting games.

Removing complexity requires both science and art. These principles which bring about simplicity are not straightforward to incorporate into an engineering process. Once a system becomes simple, the benefits become evident quickly. One important benefit for teams to think about is the number of engineers who can work on a project. If a system is a convoluted mess, usually only a couple people can work on it. A simple system can be worked on concurrently by more engineers, making it a much more effective piece of software.

Software Engineers Manage Complexity

A programmer understands software languages and can use them to have a computer accomplish tasks. A software engineer will do the same, with the addition of minimizing the program's complexity. In fact the act of minimizing complexity takes up most of an engineer's time. Many other tasks such as designing, testing, documenting, refactoring, and reviewing code are much more time intensive -- and important -- than the actual programming.

Software engineers must create programs that are simple enough to be understood by other engineers. This enables others to work on the project and create something greater than the sum of the engineers' abilities. Creating obvious software is a skill acquired through working with others. One of the more useful abilities of that skill is to use design patterns. When the writer and reader of a program know a common design pattern, they can quickly communicate its intent.

Documentation is important no matter how simple the code. Programs always appear simpler to their author than the reader. This skill is necessary to work on teams that are spread across time zones. Delays due to misunderstanding undocumented code can be costly. Documentation manages the complexity of software by writing in clear terms what the code will do.

Conclusion

Writing software is easy, but writing good software is not. Seek simplicity to save the business and other engineers time and money. Engineers should hone their complexity-reducing skills to write better software.