mardi 27 janvier 2015

Is this a good pattern: replacing a long function with a series of lambdas?


I recently run into the following situation.



class A{
public:
void calculate(T inputs);
}


Firstly, A represents an object in the physical world, which is a strong argument for not splitting the class up. Now, calcutale() turns out to be quite a long and complicated function. I perceive three possible structures for it:



  • write it as a wall of text - advantages - all the information is in one place

  • write private utility functions in the class and use them in calculate's body - disadvantages - the rest of the class doesn't know/care/understand about those methods


  • write calculate the following way:



    void A::calculate(T inputs){
    auto lambda1 = () [] {};
    auto lambda2 = () [] {};
    auto lambda3 = () [] {};

    lambda1(inputs.first_logical_chunk);
    lambda2(inputs.second_logical_chunk);
    lambda3(inputs.third_logical_chunk);
    }



Can this be considered a good or bad practice? Any problems the need for this approach is revealing? All in all, should I consider this as a good approach when in the future am faced with the same situation?





Aucun commentaire:

Enregistrer un commentaire