Our code is too complex to test
Professional developers routinely deal with highly complex code. That’s just a fact of life. It leads to one valid objection: our code is too difficult to write unit tests for. The code we work on might be highly valuable, trusted legacy code that brings in significant top-line revenue. This code may be complex. But is it too complex to test? Is it true to say that every piece of complex code simply cannot be tested?
Understanding the causes of untestable code
The answer lies in the three ways that code becomes complex and hard to test:
- Accidental complexity: We chose a hard way over a simpler way by accident
- External systems cannot be controlled to set up for our tests
- The code is so entangled that we no longer understand it
Accidental complexity makes code hard to read and hard to test. The best way to think about this is to know that any given problem has many valid solutions. Say we want to add a total of five...