I really have a question about existence, and as I progress it makes more noise and I really don't know if I'm doing things right. I have my own software, which I developed a few months ago... it works quite well for its purpose, but it turns out that they asked me for another one that has the same functionality as the previous one but adds additional features... that is, the first one is based on the second. What I came up with, is about the source set, defining a constant at the project level and adding preprocessor statements... at first it occurred to me that it was the best thing... that way it would only change in key places only when I need new classes of the new functionality...BUT as I progress I see that it gets complicated and the code becomes un-maintainable. I show parts so they can see what is happening to me.
#include "enginegraph.h"
#include "trackfarmgraph.h"
#ifdef __CORTE_SECCION__
#include "multiworkerdpathgraph.h"
#else
#include "workedpathgraph.h"
#endif
#include "pointsfarmgraph.h"
....in other parts of the project...
pointsGraph = NULL;
#ifndef __CORTE_SECCION__
worker = NULL;
#else
multiWorker = NULL;
#endif
engine = NULL;
dlg = NULL;
....in other parts of the project...
#ifndef __CORTE_SECCION__
if (worker)
{
disconnect(worker);
delete worker;
worker = NULL;
}
#else
if (multiWorker)
{
disconnect(multiWorker);
delete multiWorker;
multiWorker = NULL;
}
#endif
if (track)
{
disconnect(track);
delete track;
track = NULL;
}
With this I am noticing that the code becomes unreadable and with time it will be more so. The advantage I find is that if bugs appear in the common part, they are corrected and both software are OK... but I think the cost of unreadability and maintenance grows exponentially. How else can this be done? THANK YOU
The problem in this case is that you are not properly isolating the different functions.
Preprocessor directives can bring a lot of flexibility to your code... but they are far from perfect:
#ifdef
doesn't compile, forcing you to do multiple builds to test all the combinations (too expensive maintenance)_CORTE_SECCION__
instead of__CORTE_SECCION__
... and then you spend hours identifying the problem.It is advisable to isolate the different functionalities in classes or functions. With the new standards using templates is quite an affordable option.
I explain. Let's start with this code snippet:
Improving it and isolating the code here is relatively easy.
The first thing I would do is define a constant based on
__CORTE_SECCION__
:We can then create a collection of templates for the different use cases (two in this case). Notice that you have not indicated the type of
worker
andmultiworker
. I will assume that they areWorker
andMultiworker
.In any case, we are interested in creating a tool that allows us to create one
Worker
or oneMultiworker
depending on the circumstances:And then the disconnection functions. As the logic to be executed is the same, a single template is enough:
We already have all the wickers ready, all that remains is to make changes to the original code snippet:
I'll give you a simple example.
We start a repository
We add the initial files of the program
A client asks us for a very special thing that probably will not work for the rest. The fussy customer!! Well, we created a branch
We add the new files and the modifications of the old ones
Now we jump to our main version to continue improving the program
Added the improvements to the main branch
And now we want to improve the version of the tiquismiquis client, so we go to its branch:
and merge with master
milk!!! It turns out that it cannot automatically merge them, so we open the conflicting file, leave it handy and continue
All ready :-)