How to combine multiple functions into a single template based function

Threre are two functions FindMaxDistanceVector and FindMaxDistanceList whose implementation is almost same except some debug information added in FindMaxDistanceList. Note that these two functions are just dummy functions written here to convey the idea. The actual functions are more complex but the underlying problem is the same as discussed here. One can see that the two functions are almost similar. In fact, if we do not consider the debugging code, then the two functions can be replaced with a template function. But if we want to keep the debugging information, then how do we replace these two functions with a single function. Additionally, one can see that inside debugging code, the loop is run once again over the entire size of the vector/list making it look unattractive. This is done to avoid calculating the minVal in release mode. ``` int FindMaxDistanceVector(std::vector&vec1, std::vector&vec2) { int maxVal = INT_MIN; int sz1 = vec1.size(); int sz2 = vec2.size(); if(sz1 != sz2) { return maxVal; } for(int i = 0;i < sz1;i++) { if(vec1[i]+vec2[i] > maxVal) { maxVal = vec1[i] + vec2[i]; } } return maxVal; } int FindMaxDistanceList(std::list&lst1, std::list&lst2) { int maxVal = INT_MIN; int sz1 = lst1.size(); int sz2 = lst2.size(); if(sz1 != sz2) { return maxVal; } for(int i = 0;i < sz1;i++) { if(lst1[i]+lst2[i] > maxVal) { maxVal = lst1[i] + lst2[i]; } } if(_DEBUG_ENABLED_) // This part of the function is only for debugging purpose { int minVal = INT_MAX; for(int i = 0;i < sz1;i++) { if(std::abs(lst1[i] - lst2[i]) < minVal) { minVal = std::abs(lst1[i] - lst2[i]); } } LOGGER->INFO(LOGGINGTYPE::Debug, "minimum distance between the lists = %d", minVal); } return maxVal; } ``` My attempt of combining these two function was to write the following template function. However, I do not think that this code is reusable. If any change in the future takes place; For eg:, what if there is another data type of std::queue and we want to print some other debugging information which is not minVal. In that case, we will have to modify the template function which makes the following implementation non-reusable template int FindMaxDistance(T& obj1, T& obj2, bool addDebugInfo) { int maxVal = INT_MIN; int sz1 = obj1.size(); int sz2 = obj2.size(); if(sz1 != sz2) { return maxVal; } for(int i = 0;i < sz1;i++) { if(obj1[i]+obj2[i] > maxVal) { maxVal = obj1[i] + obj2[i]; } } if(addDebugInfo && _DEBUG_ENABLED_) // This part of the function is only for debugging purpose { int minVal = INT_MAX; for(int i = 0;i < sz1;i++) { if(std::abs(obj1[i] - obj2[i]) < minVal) { minVal = std::abs(obj1[i] - obj2[i]); } } LOGGER->INFO(LOGGINGTYPE::Debug, "minimum distance between the objects = %d", minVal); } return maxVal; } Is there any better way to combine the two functions?

Apr 8, 2025 - 07:32
 0
How to combine multiple functions into a single template based function

Threre are two functions FindMaxDistanceVector and FindMaxDistanceList whose implementation is almost same except some debug information added in FindMaxDistanceList. Note that these two functions are just dummy functions written here to convey the idea. The actual functions are more complex but the underlying problem is the same as discussed here. One can see that the two functions are almost similar. In fact, if we do not consider the debugging code, then the two functions can be replaced with a template function. But if we want to keep the debugging information, then how do we replace these two functions with a single function. Additionally, one can see that inside debugging code, the loop is run once again over the entire size of the vector/list making it look unattractive. This is done to avoid calculating the minVal in release mode.

``` 
    int FindMaxDistanceVector(std::vector&vec1, std::vector&vec2) 
    {
       int maxVal = INT_MIN;
       int sz1 = vec1.size();
       int sz2 = vec2.size();
       if(sz1 != sz2)
       {
          return maxVal;
       }
       for(int i = 0;i < sz1;i++)
       {
          if(vec1[i]+vec2[i] > maxVal)
          {
             maxVal = vec1[i] + vec2[i];
          }
       }
       return maxVal;
    }

    int FindMaxDistanceList(std::list&lst1, std::list&lst2) 
    {
       int maxVal = INT_MIN;
       int sz1 = lst1.size();
       int sz2 = lst2.size();
       if(sz1 != sz2)
       {
          return maxVal;
       }

       for(int i = 0;i < sz1;i++)
       {
          if(lst1[i]+lst2[i] > maxVal)
          {
             maxVal = lst1[i] + lst2[i];
          }
       }
       if(_DEBUG_ENABLED_) // This part of the function is only for debugging purpose
       {
          int minVal = INT_MAX;
          for(int i = 0;i < sz1;i++)
          {
             if(std::abs(lst1[i] - lst2[i]) < minVal)
             {
                minVal = std::abs(lst1[i] - lst2[i]);
             }
          }
          LOGGER->INFO(LOGGINGTYPE::Debug, "minimum distance between the lists = %d", minVal);
       }
       return maxVal;      
    }
```

My attempt of combining these two function was to write the following template function. However, I do not think that this code is reusable. If any change in the future takes place; For eg:, what if there is another data type of std::queue and we want to print some other debugging information which is not minVal. In that case, we will have to modify the template function which makes the following implementation non-reusable

    template 
    int FindMaxDistance(T& obj1, T& obj2, bool addDebugInfo)
    {
       int maxVal = INT_MIN;
       int sz1 = obj1.size();
       int sz2 = obj2.size();
       if(sz1 != sz2)
       {
          return maxVal;
       }

       for(int i = 0;i < sz1;i++)
       {
          if(obj1[i]+obj2[i] > maxVal)
          {
            maxVal = obj1[i] + obj2[i];
          }
       }
       
       if(addDebugInfo && _DEBUG_ENABLED_) // This part of the function is only for debugging purpose
       {
          int minVal = INT_MAX;
          for(int i = 0;i < sz1;i++)
          {
             if(std::abs(obj1[i] - obj2[i]) < minVal)
             {
                minVal = std::abs(obj1[i] - obj2[i]);
             }
          }
          LOGGER->INFO(LOGGINGTYPE::Debug, "minimum distance between the objects = %d", minVal);
           }
           return maxVal;      
        }

Is there any better way to combine the two functions?