desc: What is complexity as it relates to software?
---
The most [relevant definition of complexity](https://www.lexico.com/en/definition/complex) 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.
Before being able to manage complexity, it must be identified. In addition to 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 appear simple now may become complex in the future.
* 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.
* 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](https://www.youtube.com/watch?v=Km024eldY1A). It generally takes [a dedicated decade](https://www.quora.com/How-long-does-it-take-to-become-a-chess-grandmaster) to become a grand master.
All software is complex; it is one of the most complex man-made things. Physical creations such as space ships, submarines, and skyscrapers may be complex, but they are limited by the laws of physics. Software has no such limit. For example, [FreeBSD](https://www.freebsd.org/) is an operating system that powers parts of [Netflix](https://www.phoronix.com/scan.php?page=news_item&px=Netflix-NUMA-FreeBSD-Optimized), [WhatsApp](https://www.quora.com/Why-did-WhatsApp-choose-FreeBSD-over-Linux), and [PlayStation 4](https://en.wikipedia.org/wiki/PlayStation_4_system_software). Using [scc](https://github.com/boyter/scc), a tool to evaluate the size of codebases, it estimates it would take about 16 years with 500 people to recreate FreeBSD 12.1.
* 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: While money can be acquired, time is scarce. If a project becomes expensive, some larger companies can handle it. If the software is complex enough, the bottleneck becomes time, at which point it is too late. Cars travel quickly on highways, partly due to their straight path (simple). If the only way to get from A to B is a winding mountain road (complex), more time will be needed than the highway, no matter how fast a car you buy.
* 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.
An antonym of complexity is [simplicity](https://www.thesaurus.com/browse/complexity). Although it is easy to explain a [simple system](https://simplesystems.tech/blog/post/simplicity), 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 an automatic transmission are quite complex, but it is encapsulated so well that the driver simply selects "D". As [Rob Pike](https://twitter.com/rob_pike) points out in this [video](https://www.youtube.com/watch?v=rFejpH_tAHM&t=789), 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.
* 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.
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](https://en.wikipedia.org/wiki/Software_design_pattern). 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.
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.