Reduced Structural Complexity With Examples

Code quality is -should be- one of the main concerns of a software developer. While designing the internal structure, it is important to avoid increasing the structural complexity -cyclomatic complexity, cognitive complexity, halstead volume, maintainability index and such…- which exposes to reliability, maintainability and testability problems.

Factory pattern design seemed like a good place to start explaining how to work on complexity problems. The reason, Gürel and I picked factory pattern, is because its most common usage -no offense, this is just one way of doing it :)- simply turns the code into an if/else farm in no time. As you would imagine, this kind of approach aggravates the complexity.

With my dear friend and colleague Gürel Kaynak, we prepared a factory pattern example in both Java and Python just to indicate an alternative way which comes as a complexity-free version with lazy initialization (delegates). This approach uses Maps to replace the conditional statements and manage the passed dependencies as strict as possible via Enum types.

To spice it up a little bit, it is always welcome to implement a caching system or even prototype pattern when creating an object comes at a high cost.

Please check out the link below for the examples;

Complexity-free Factory Pattern Examples

Related posts

Leave a Comment