Encapsulation: free functions or a class

For a long time now, I have used used this very informative article by Scott Meyers when implementing interfaces and functions:

http://drdobbs.com/cpp/184401197

However, I basically need some opinions on what is best for my current task.

I am writing a collection of functions that will perform calculations on data. These functions share a common theme, however they do not share state, so as of now I have them grouped in a namespace:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
namespace StatsAnalysis
{
    DLLDIR double CalcThis(
        DocMap &dm,
        const int searchType,
        const int searchVal_1,
        const int searchVal_2
    )
    {
        RawData data;
        
        switch (searchType)
        {
        case TYPE1: data = SearchByDocumentID(searchVal_1, searchVal_2, dm); break;
        case TYPE2: data = SearchByDocumentType(...); break;
        case TYPE3: data = SearchByDocumentStatus(...); break;

        //perform specific calculation and return
    }

    DLLDIR double CalcThat(
        DocMap &dm,
        const int searchType,
        const int searchVal_1,
        const int searchVal_2
    );
}


Each one calls one of three helper functions to retrieve the data from supplied from the DocMap, and then performs a specific calculation related to the function name. Each calculation function performs the same switch case to retrieve data.

As said earlier, they do not share any common state, so they are not in a class. The only reason I can think of to put them in a class is so that the result from searching the data can be cached, if more than one calculation is required for the same data set.

Is this a good approach? I think its better than having to call methods to change the data, search type and parameters, and then the calculation, but I do not know.

Any help is appreciated. Thank you.
My opinion: Create a helper function (that is not exported from the DLL and not listed in the header file) that encapsulates the switch() and returns the RawData object. I don't see the need of caching anything here, but of course I don't know any specific details of your project or setup.
That's what I was thinking too, but is it ok to nest function calls that deep? Lets say the object where data is pulled from is a map of a 1,000 items or so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

DLLDIR double CalcOpticalInvariance(DocMap &dm, const int searchType, const int searchVal)
{
    RawData = CollectData(dm, searchType, searchVal);
    //calculations
}

//Functions not exported
RawData CollectData(DocMap &dm, const int searchType, const int searchVal)
{
    RawData data;

    switch (searchType)
    {
        case TYPE1: data = SearchByDocumentID(searchType, searchVal, dm); break;
        case TYPE2: data = SearchByDocumentType(...); break;
        case TYPE3: data = SearchByDocumentStatus(...); break;
    }

    return data;
}


Perhaps CollectData() and the search functions should take a RawData reference instead.

When I was thinking of caching I was thinking of storing the RawData someplace so it can be used by another calculation function if necessary, instead of having to search again.

Basically the StatsAnaylsis namespace contains functions to calculate the ways light can pass through a prism, if viewed through another device.
I don't see a problem with this setup. I've seen call stacks with a few dozen function calls, and I bet even that is small.

You can get a RawData object by reference in order to return the result, but I don't see much gain in that: You are exchanging a copy constructor call with a operator= call, and operator= is sometimes lengthier because it must release previous resources first.
Topic archived. No new replies allowed.