Joanna
Joanna

Reputation: 173

using class to represent a task in c++

I'm writing a function to do a specific task. This function need some temporary data to process the task. The problem is if I do everything in this function then the size of this function will be big. Then I think about breaking this function into many small functions. But if I do that, I need to pass some temporary data to those small functions. It make the code look not very well. So I consider to create a class that have private data including input, output and temporary data and those small function become the method of class. Below is pseudo code to represent my idea.

/* big function */
output big_function(input)
{
    temporary_data data1;
    temporary_data data2;
    temporary_data data3;
    temporary_data data4;
    /* do task here*/
    ...
    return output;
}

/* many small function*/
output function1(input)
{
    temporary_data data1;
    temporary_data data2;
    temporary_data data3;
    temporary_data data4;
    /*do some thing */
    function2(input,data1,data2, data3, data4);
    output = function3(input,data1,data2, data3, data4);
    return output;
}

/* use class*/
class Task
{
public:
    Task(input);
    function1();
    output get_output();
private:
    function2();
    function3();
private:
    input;
    output;
    data1;
    data2;
    data3;
    data4;
}

And my question is: using class to represent a task is a right way or not?

Upvotes: 0

Views: 1245

Answers (2)

VolAnd
VolAnd

Reputation: 6407

Public interface, as well as internal structure, of your class depends on many factors:

  • how the class will be used (many instances or single, multithreaded or not, etc)
  • what is the temporary_data (structures, classes, collections/arrays, or just simple types)
  • volume of temporary data and place to store the data (what memory will be used - static, automatic (stack) or dynamic (heap))
  • other related matters (such as errors handling, input and output checking, etc.)

For the simplest implementation (one function call for solution that call other functions) my suggestion is to consider:

  • creation of class with one static method output solve_task(input) instead of three - Task(input) (constructor), function1() (running the solution) and output get_output() (getting the result)
  • private fields and methods have to be also static to be called from solve_task
  • solve_task can be as simple as possible, like
     output solve_task(input)
     {
        if(!check_input(input))
        {
            // reaction on wrong input
            throw(badInput);
        }
        // data preparation
        function1(input); // init private fields
        function2(input); // find/fill the temporary data
        // solution
        function3(); // works with internal temporary data 
        ...
        functionN(); // works with internal temporary data 
        // providing result
        return prepare_output()
     }

Upvotes: 1

dlasalle
dlasalle

Reputation: 1725

Using an object's member variables so as to avoid argument passing generally leads to very fragile code and hard to read code (similar to using global variables).

It seems to me like what you want is:

output function(input);

Rather than the three (in truth four) steps required by your object:

Task t(input);
t.function1();
output o = t.get_output();
// destructor is implicitly called at some point

The other issue with this is that the function on t must be called in a specific order (@VolAnd mentioned this in his comment).

To solve the issue of passing in and out large numbers of parameters, pass structs into and out of the functions (so at their calling its clear what is read and what is modified):

struct params {
  temporary_data data1;
  temporary_data data2;
  temporary_data data3;
  temporary_data data4;
};

If appropriate, use multiple structs to communicate and enforce the state changes in the temporary data (e.g., struct unsortedData and struct sortedData).

Upvotes: 1

Related Questions