Isolate Complexity

Complexity is the enemy of scale. Simpler code is better, but harder to achieve as projects grow. Complexity grows, making it harder to work with, and every step forward is matched by unpredictable problems.

Manage complexity by keeping it simple when possible. However, some complexity is inevitable, especially when solving complex problems.

A good strategy is to isolate complexity by keeping internal details simple and the external interface straightforward. This way, users don't have to deal with complexity, even if the code is complicated internally.

Sine and Cosine

The sine and cosine functions are simple to use: call them, get the result. But inside, it's complicated.

I didn't care about the details until I was older. To me, the functions just worked and produced the right answer. That was fine - the complexity inside didn't affect how I used them.

You can draw a circle without knowing the math behind sine and cosine. The complexity in the functions is contained and doesn't leak into your code.

Hiding Internal Details

Isolate complexity and keep it contained in specific sections. This applies to your code too.

Imagine you're working with customer records and need to find recent purchasers. However, not all customers are valid.

The complexity of invalid customers affects every part of your code. If customer validity rules change, you'll need to update multiple places.

Instead of breaking it down, ensure your code starts with a list of valid customers. This makes your code simpler and easier to understand.

By doing this, the complexity is isolated in a single function, providing a list of valid customers.

Distributed State and Complexity

Object-oriented design can help simplify complexity, but it's not a guarantee. When state is distributed across multiple objects, it can still lead to confusing code.

Distributed state isn't inherently bad. Sometimes, multiple objects naturally manage a system's state. The promise of object-oriented design is that these objects will work together seamlessly, each managing its own state and interacting with others.

However, fulfilling this promise requires careful coding. Trying to handle the state of multiple objects can result in rickety code.

Localized Complexity, Simple Interactions

Avoid complex interactions between system parts. Locally complex details are okay, but simple interfaces and interactions are crucial.

A system with simple interactions has linear complexity, making it manageable.

Complicated interactions lead to chaos.

Adding features that require code changes in many places is a red flag. It indicates a lack of complexity localization, ultimately leading to system failure.