r/cpp • u/According_Leopard_80 • Apr 09 '26
Writing only decoupled code
https://middleraster.github.io/DAG/HeaderOnlyNoForwardDeclarations.html
12
Upvotes
r/cpp • u/According_Leopard_80 • Apr 09 '26
41
u/SuperV1234 https://romeo.training | C++ Mentoring & Consulting Apr 09 '26 edited Apr 09 '26
I've never really had any major pain due to cycles, in fact I cannot really recall the last time either me or any of my coworkers had issues because of architectural cycles.
But I can tell you how much pain I've had due to exruciating compilation times. Header-only libraries and bloated headers are a primary cause of that.
I also feel your article is very anecdotal, such controversial claims should come with strong evidence.
It's a "no" for me.
Source?
7
u/FlyingRhenquest Apr 10 '26
How much of this is due to putting most of your code in headers versus being somewhat heavily invested in TDD? TDD fundamentally forces you to write small, decoupled libraries that are easy to test. TDD also tends to keep you focused on the features you need right now for your minimum viable product, so you don't get the yagni-based overengineering that drives a lot of technical debt.
14
u/garnet420 Apr 09 '26
This is just not right. You can create this sort of problem without having any cycles easily.
If I had to point at any aspect of the dependency graph as being correlated to it, I would maybe say "total number of edges" or "edges that reach through multiple layers" or something like that.
And prohibiting forward declaration is just a dumb thing Google says to do, and people repeat it because Google said it and they're... Big?
13
u/fdwr fdwr@github 🔍 Apr 09 '26
Interestingly this does not apply to class/struct methods, as the one area of C++ where you can define functions in a logically grouped order rather than strict call dependency order. The same does not apply to free functions, but then one cute hack to achieve order independence is to wrap them all inside the class as static methods (not recommending 😅), thus making the class essentially more of a namespace (too bad it doesn't work inside ordinary namespaces too, but there must some reasoning for delayed processing or two pass evaluation 🤷♂️).
5
u/yuri-kilochek Apr 09 '26
If you define a virtual interface class for each concrete class you can technically obey the rules, but still produce tightly coupled mess.
5
u/Big_Target_1405 Apr 10 '26
Putting all your code in headers is the opposite of creating decoupled code.
Every time one header changes loads of your code needs to be recompiled. All of that code is coupled.
2
u/SleepyMyroslav Apr 10 '26
Amalgamation of all sources for a small project is a well known way to build it faster. I would like to see some successful code that supports the hypothesis of header only code. I don't know a game or something. I expect that what works on small sized project is not going to scale well. It might go for a while before scale will bring up issues though.