User Tools

Site Tools


Functional Dependency

Functional dependency is deemed a fundamental of today's code bases and at the heart of poor malleability/maintainability.

Functional dependency is present where functions contain both logic and calls to other functions (in the scope of the same code base).

Logic in the above image is red, function calls are green. Logic needs to be tested, function calls hardly need to be tested.

Due to the mixture between logic and function calls the logic is separated in several pieces. The primary purpose of the function represented by logic thus does not exist as one chunk. It cannot be tested by itself. Either the logic of the function is run together with the logic represented by the function calls. Or the function calls have to be replaced with test doubles to focus on the logic in the “function under test”.

Through the use of polymorphy by applying the Dependency Inversion Principle (DIP) and Inversion of Control (IoC) test double can be inserted at test time - but this adds complexity to the code base: injection has to be enabled and an interface has to be introduced as an abstraction. For functions containing more logic and more function calls this becomes progressively more difficult.

And even with DIP+IoC employed the different pieces of logic in the “function under test” cannot be tested separately. If something goes wrong it's hard to isolate the bug and focus just on the incorrect part of the logic.

Finally functionally dependent code is difficult to understand: It violates the Single Level of Abstraction (SLA) principle. Per definition logic is on a lower level of abstraction than a function call. To gain an understanding of how behaviour is created the tree of functions has to be followed into its depth. Without debugging it's hard to see how the transformation process works.

Hierarchies of functionally dependent functions tend to contain few and large functions:

As long as functions are allowed to contain both logic and function calls there exists no incentive to keep them small. They grow as needed: mostly logic is added to add some feature or fix a bug, only sometimes logic is extracted into other functions.

Compare this to a hierarchy following the Integration Operation Segregation Principle (IOSP).

functionaldependency.txt · Last modified: 2018/06/03 15:56 by fdadmin