Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the drawbacks of single source project structures?

Tags:

c++

linker

I'm new in my current company and working on a project written by my direct team lead. The company usually doesn't work with C++ but there is productive code written by my coworker in C/C++. It's just us who know how to code in C++ (me and my lead, so no 3rd opinion that can be involved).

After I got enough insight of the project I realized the whole structure is... special.

It actually consist of a single compilation unit where the makefile lists as only source the main.hpp.

This headerfile then includes all the source files the project consists off, so it looks like a really big list of this:

#include "foo.cpp"
#include "bar.cpp"

While trying to understand the logic behind it and I realized that this is indeed working for this project, as it's just an interface where each unit can operate without accessing any other unit, at some point I asked him what are the reasons why he did it this way.

I got a defensive reaction with the argument

Well, it is working, isn't it? You are free to do it your way if you think that's better for you.

And that's what I'm doing now, simply because I'm really having trouble with thinking into this structure. So for now I'm applying the "usual" structure to the implementation I'm writing right now, while doing only mandatory changes to the whole project, to demonstrate how I would have designed it.

I think there are a lot of drawbacks, starting with mixing linkers and compilers jobs by own project structure can't serve well, up to optimizations that will probably end in redundant or obscure results, not to mention that a clean build of the project takes ~30 minutes, what I think might be caused by the structure as well. But I'm lacking the knowledge to name real and not just hypothetical issues with this.

And as his argument "It works my way, doesn't it?" holds true, I would like to be able to explain to him why it's a bad idea anyway, rather than coming over as the new nit picky guy.

So what problems could actually be caused by such a project structure? Or am I overreacting and such a structure is totally fine?

like image 780
dhein Avatar asked Sep 20 '17 10:09

dhein


People also ask

What are advantages and disadvantages of single sourcing?

Single sourcing, a powerful approach in a stable environment, can amplify a firm's exposure to risk (e.g., supplier's default) in the presence of uncertainty. Multiple sourcing, however, presents higher costs due to the management of more than one supplier.

What are the risks involved in single source procurement?

Dependence on single-sourced components puts companies at significant risk for not meeting their customer demands in the event of a natural disaster, factory damage from fires, or a man-made crisis such as worker strikes and political upheavals.


1 Answers

not to mention that a (clean) build of the project takes ~30 minutes

The main drawback is that a change to any part of the code will require the entire program to be recompiled from the scratch. If the compilation takes a minute, this would probably not be significant, but if it takes 30 min, it's going to be painful; it destroys the make a change->compile->test workflow.

not to mention that a clean build of the project takes ~30 minutes

Having separate translation units is actually typically a quite a bit slower to compile from scratch, but you only need to recompile each unit separately when they're changed, which is the main advantage. Of course, it is easy to mistakenly destroy this advantage by including a massive, often changing header in all translation units. Separate translation units take a bit of care to do it right.

These days, with multi core CPU's, the slower build from scratch is mitigated by parallelism that multiple translation units allow (perhaps the disadvantage may even be overcome if the size of individual translation units happen to hit a sweet spot, and there are enough cores; you'll need some thorough research to find out).

Another potential drawback is that the entire compilation process must fit in memory. This is only a problem when that memory becomes more than the free memory on your developers workstations.

In conclusion: The problem is that one-massive-source-file approach does not scale well to big projects.


Now, a word about advantages, for fairness

up to optimizations will probably end in redundant or obscure results

Actually, the single translation unit is easier to optimize than separate ones. This is because some optimizations, inline expansion in particular, are not possible across translation units because they depend on the definitions that are not in the currently compiled translation unit.

This optimization advantage has been mitigated since link time optimization has been available in stable releases of popular compilers. As long as you're able and willing to use a modern compiler, and to enable link time optimization (which might not be enabled by default)


PS. It's very un-conventional to name the single source file with extension .hpp.

like image 175
eerorika Avatar answered Nov 10 '22 23:11

eerorika