🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #1499

Re: Inversion-of-control and dependency-injection in C

#1499
From: Janis Papanagnou
Date: Thu, 12 Nov 2026 23:26
111 lines
7105 bytes
On 2026-11-12 22:15, Ross Finlayson wrote:
> On 11/12/2026 12:11 PM, Lew Pitcher wrote:
>> On Thu, 12 Nov 2026 09:41:04 -0800, Ross Finlayson wrote:
>>
>>> Hello, dear C experts, I'm hoping you can tell me a story about
>>> modularity and composability, about the concerns of inversion-of-control
>>> and dependency-injection in C, basically about wiring
>>> test-time and run-time targets, for example about conditional-includes
>>> and various build-targets, probably mostly about function pointers.
>>
>> Pardon my impertinence, but this sounds like a homework problem to me.
>>
>> Could you rephrase your question without the academic wording?
> 
> Not at all, meaning, no offense taken.

Your original post and its intention had been also to me quite
unclear. Here, now, I seem to understand at what you are aiming.

(Disclaimer: my comments are just based on personal experience
in our software developing practice from the 1990's and not some
general accepted opinion or habits.)

> How about "writing testable code", what are common frameworks
> or best practices for "writing testable code" in C.

Our approach was not to "write testable code" by _adapting_ to
some existing framework. While we had standards and practices how
to design software the _tests followed our software_ (not v.v.).
One exception here was that including dedicated trace records in
our code (that could be activated specifically WRT a module and
in granularity) helped for the test evaluation and regressions.

Then we have to differentiate what tests you have in mind here;
there's various types of tests which require their own principles.
For example; Unit Tests (in our [C++] context mainly classes, in
"C" probably functions or a set of related functions) required
functions that own little test programs would call to verify all
the relevant conditions. We haven't used frameworks that created
the tests but wrote our own dedicated test-functions along with the
application functions. For our Component Tests (where components
were in our case software pieces that constitute runtime processes
with a well-defined interface) we wrote stubs that could challenge
the components on the interface levels. Such stubs could operate
some predefined procedures with well-defined input, or arbitrary
data, or be an operator-triggered manual interaction, depending on
the type of the component. Storing all the test input and output
results made regression tests possible. For the Integration Tests I
don't recall the procedures (it was done in a dedicated integration
test group); but they defined and checked the system configuration,
the interoperability (I think!) mostly manually. Malfunctions were
reported back to development accompanied by protocols, traces, and
actual data. The System Tests were checked against the specification
documents, and the Acceptance Tests against the requirements specs,
sometimes accompanied by the customers. One aspect yet not listed
was the test of the GUIs; beside manual tests we used tools to do
all that "mouse shoving and clicking". But given that the functional
tests already happened on the automatable interface level in former
stages that was effectively just to check the "UI ergonomy" ("UX")
factors. And, finally, we had our own hierarchical regression test
framework to run and document the tests on each abstraction stage.
(Eventually there were also external third-party tools existing but
back then we wrote (had to write) our own regression test framework
that fulfilled our demands.)

> 
> I'd suggest abstraction and layers and modularity and composability,

Yes, those are principles that support the mentioned QA processes.

> and having an inversion-of-control/dependency-injection approach,

(See my comment about "tests follow code (not v.v.)". - Opinions may
differ.)

> a convention moreso than a framework, which pretty much means wrapping
> all the standard functions, then about for example having an include
> where those are wrapper functions to stub and mock, and another include
> where those are macros, or, perhaps, using includes, to point the
> include directory for the standard library, into instead what makes
> for stubbable/mockable calls to standard library and system functions,
> or just leaving the headers alone and linking to modules that aren't
> the standard libraries, this being an example of something like
> "can't mock strftime", or, for an example.

Just a comment on one aspect here; yes, you have to organize your
software and tests. Key principles are the modularity, hierarchical
organization, being able to automate processes, and organize all in
some consistent way. In case your company is certified (or wants to
get certified) make sure your processes are documented and that you
can reproduce your quality processes and the results.

> 
> Then, about the modularity, and pointers (to pointers to pointers
> to pointers, ...) to functions, then seems about conventions of
> types for typeof and the like about that like anybody else I rely
> on the compiler to fail with -Wall and failing on warnings to
> catch type errors, ..., has that I want to program to interfaces
> and then have objects composable, ..., about using conditions of
> the build, to, basically make for test-time targets and run-time
> targets, with actually testing the code that goes into the build.

It's not clear to me what you want to say here but it sounds that
you want very specific ("micro-management") advice on a low level?
(If so, I can't and don't want to comment here.)

(All above from memories and off the top of my head. Hope there's
something useful for your purpose.)

Janis
Technical Details
Message-ID: <11d5huo$3flo0$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com> <11d56gl$3ud5q$1@dont-email.me> <16ScnZKb_MQfsWv3nZ2dnZfqn_idnZ2d@giganews.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Janis Papanagnou <janis_papanagnou+ng@hotmail.com>
Newsgroups: comp.lang.c
Subject: Re: Inversion-of-control and dependency-injection in C
Date: Fri, 13 Nov 2026 00:26:16 +0100
Organization: A noiseless patient Spider
Lines: 111
Message-ID: <11d5huo$3flo0$1@dont-email.me>
References: <yQOdnQ-qnKiiZ2j3nZ2dnZfqn_udnZ2d@giganews.com>
 <11d56gl$3ud5q$1@dont-email.me>
 <16ScnZKb_MQfsWv3nZ2dnZfqn_idnZ2d@giganews.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 12 Nov 2026 23:26:17 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3659520"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18zi+qahYIdVOGi4vXZsW+2"; posting-host="ab9297484f17960488c069c38c3e3749"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:S/52wH47RUh0TCz/zKGe15Ilrug= sha256:NTCEgBMnJbiTITzkc5XuirT33O+BwTcSgAPkHPue/Mo=
	sha1:4W9jPTESP8vMZr8e5IM1uofJPsk= sha256:Oeb9Drp2zDVGUhn+G8AYw1xDoXCZrZ6l/n2lyY/QdJQ=
In-Reply-To: <16ScnZKb_MQfsWv3nZ2dnZfqn_idnZ2d@giganews.com>
Content-Language: en-US