0

I have a question regarding the use of lambdas in factories, together with generic/templated classes. It seems like it is possible to get carried away with lambdas and generic classes, to have too much untested logic in lambda functions. Example:

In a Factory:

createGenericClass(ARG1 arg1, ARG2 arg2)
{ 
    auto lambda = [](std::vector<DataSP>& inputVector) {
          DataSP spData(new Data);
          if (inputVector.empty())
          {
             return spData;
          }
    
          spData->clear();
    
          for (const DataSP& spCurrentData : inputVector)
          {
             if (spCurrentData && spCurrentData->getNumberOfSubData() != 0)
             {
                for (auto subDataIdx = 0;
                     subDataIdx < spCurrentData->getSize();
                     ++subDataIdx)
                {
                   ...// Some manipulation here
                }
             }
          }
          return spData;
       };
       
      using GenericClassT = Nmcp::GenericClass<ARG1, ARG2, SomePolicy>;

   return std::make_unique<GenericClassT>(
      arg1, arg2, lambda);
}

The code above is used in a factory. This factory, uses the lambda to create a GenericClass, which will be connected to other classes(for data flow).

Since GenericClass is generic/templated, it makes it hard to test the logic in the lambda. There is a google test for GenericClass, but of course it uses different lambdas everytime it is used. What would you recommend to use/change in such a case? Rewrite a lambda to be a class and test it? Or not use a generic/templated class? Or is this just a bad design altogether?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.