skip to Main Content

I work in the Visual Studio IDE. I inhered some C++ code where a lot of macros are defined: it’s like a cascade of macros are referring to some other replacement, again referring to some other replacement, see below.

I come from a Matlab environment, so I’m super frustrated that if I want to watch the value if these variables in Autos/Locals, I have to walk down the cascade and paste the actual preprocessed identifier/string into the watch window. Also, I cannot paste the actual C++ code into the Immediate Window, as it cannot process these identifiers without them being replaced by the preprocessor first.

Is there any add-on that can do all the cascading/preproccesing for you when you want to paste stuff in the immediate window, where these identifiers are parts of more complex statements and you just want to paste it in and get the result?

Or any other solution?
I’m not experienced in C++, but my wild guess is this cannot be the actual good way of programming as it’s so hard to develop the code further or understand it. It’s gonna be near impossible to rewrite this program I got, but if you know of any improvements that can be made, you can let me know.

Some examples:

The code: if ((_f_NUMERO_TT == 97) || (_f_NUMERO_TT == JFP_23) || (_f_NUMERO_TT == YSTAR)) {

_f_NUMERO_TT == 97

F12 leads you to these snippets in some file that starts with #ifndef FILES_HEADER:

#define _f_NUMERO_TT            (_f_PARM_PROD.actu.desc_prod.num_TT) // _f_->input.produit_1[0].actu.desc_prod.num_TT

#define _f_PARM_PROD    (_f_INPUTS.produit_1[PACKAGE_PRODUIT_BASE]) 

    struct Inputs
    {
#define _f_INPUTS  (_f_->input)

        struct ParametersInputs
        {
#define _f_PARAMETERS   (_f_INPUTS.parameter) //_f_->input.parameter

            DATE date_fichiers;
            DATE date_fichiers_pp;

So instead of pasting "_f_NUMERO_TT" I need to replace it everywhere with

_f_->input.produit_1[0].actu.desc_prod.num_TT   

2

Answers


  1. You’re right : This is absolutely horrible code. It’s no surprise that there is no tooling support for code like this. That would only encourage people to continue with code like this, instead of fixing this. And there’s no business case for tool makers.

    I would recommend fixing the code. It appears that this code was written by a C developer, not a C++ developer. These macro’s appear to be a poor substitute for inline methods. You can probably replace them incrementally.

    While you’re fixing it, translate the code as well. Having code in French decreases the pool of people who can maintain it by 95%. And some comments would be helpful, too.

    This is clearly a case of technical debt. It’s hard to judge how expensive this will be to fix, it could be anywhere between EUR 1 and EUR 100 per line of code. But that rapidly becomes a business decision, which is off-topic here.

    Login or Signup to reply.
  2. To give an example on how you can use (member) functions :

    #include <iostream>
    
    struct DATE
    {
        int day {1};
        int month {1};
        int year {1971};
    };
    
    
    struct Inputs
    {
    // #define _f_INPUTS  (_f_->input)
    
            struct ParametersInputs
            {
    // #define _f_PARAMETERS   (_f_INPUTS.parameter) //_f_->input.parameter
    
                DATE date_fichiers;
                DATE date_fichiers_pp;
            } ;
    
        ParametersInputs parameters[2];
    
        // add a member function to Inputs that can do all the internal indirections
        int get_year(std::size_t parameter_index)
        {
            return parameters[parameter_index].date_fichiers.year;
        }
    };
    
    int main()
    {
        Inputs inputs;
        std::cout << inputs.get_year(0); // now use a function 
        // note there is NO checking so using 2 instead of 0 will not be correct 
        // since you will be accessing parameters array in Inputs out of bound
    
        return 0;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search