Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection for Procedural Programming

Suppose I've decided to write a large application in C, or any other procedural programming language. It has functions with call-dependencies that look like this:

A
|
+-------------+
|             |
B1            B2
|             |
+------+      +------+
|      |      |      |
C11    C12    C21    C22

Obviously, unit-testing the leaves functions, C11, C12, C21, and C22 is very easy: Setup the inputs, invoke the functions, assert the outputs.

But what is the proper strategy to enable good unit-testing for B1, B2 and A?

Would Dependency Injection suggest that B1 (and B2 also) be declared as followed?

// Declare B1 with dependency injection for invoking C11 and C12.
int B1(int input, int (*c11)(int), int(*c12)(int));

But that strategy does not seem scalable if I have many layers of calls. Just imagine what the declaration for A would look like:

int A(int input, int (*b1)(int, int (*)(int), int(*)(int)), 
                 int(*b2)(int, int (*)(int), int(*)(int)),
                 int (*c11)(int),
                 int (*c12)(int),
                 int (*c21)(int),
                 int (*c22)(int));

Yuck! There has to be a better way.

Sometimes, I feel that DI and other similar patterns that purport to promote modularity and ease of maintenance actually hampers code clarity and complicates what should be straightforward coding into nonsense abstractions and convoluted indirections.

How do large software projects in C, like Perl and Ruby, deals with unit-testing?

like image 680
kirakun Avatar asked Apr 03 '11 05:04

kirakun


3 Answers

If you only require the DI for unit testing you may use the linker to do it.

What I mean is that the functions B1 & B2 are declared in a header and used by function A, so the implementation of the B functions is provided by the linker. You just need to provide a different C-File for unit tests. This should not be a big problem, as you probably have your own makefile for the unit test anyhow.

If you require dynamic dependency resolution at runtime you should use a factory pattern (a function returning the function-pointer) for function pointers and pull them from the factory when required. The factory may decide depending on global context what function to return.

like image 143
sanosdole Avatar answered Nov 07 '22 16:11

sanosdole


A only needs to call B1 and B2. It does not need to know about anything at the C level.

You could inject a different dummy versions of functions B1 and B2 into A for the purpose of testing A.

This isolates A from needing the whole structure and means you can test each function in isolation.

like image 38
WW. Avatar answered Nov 07 '22 15:11

WW.


you can put the dependencies to a c-struct that will become one parameter for the function call. in c this would be similar to the file api where the first parameter is always the file-handle

like image 26
k3b Avatar answered Nov 07 '22 15:11

k3b