Search code examples
c++organization

C++ organize flow of functions


I'm writing a program for a microcontroller in C++, and I need to write a function to input some numbers trough a computer connected to it.

This function should perform many different and well-defined tasks, e.g.: obtain data (characters) from the computer, check if the characters are valid, transform the characters in actual numbers, and many others. Written as a single function it would be at least 500 lines long. So I'll write a group of shorter functions and one "main" function that calls the others in the only meaningful order. Those functions will never be called in the rest of the code (except of course the main function). One last thing - the functions need to pass each other quite a lot of variables.


What is the best way to organize those functions? My first tough was to create a class with only the "main" function in the public section and the other functions and the variables shared by different functions as private members, but I was wondering if this is good practice: I think it doesn't respect the C++ concept of "class"... for example to use this "group of functions" I would need to do something like that:

class GetNumbers {
    public:
        //using the constructor as what I called "main" function
        GetNumbers(int arg1, char arg2) {
            performFirstAction();
            performSecondAction();
            ...
        }
    private:
        performFirstAction() {...};
        performSecondAction() {...};
        ...

        bool aSharedVariable;
        int anotherVariable;
        ...
};

And where I actually need to input those numbers from the computer:

GetNumbers thisMakesNoSenseInMyOpinion (x,y);

Making the "main" function a normal class method (and not the constructor) seems to be even worse:

GetNumbers howCanICallThis;
howCanICallThis.getNumbers(x,y);
...
//somewhere else in the same scope
howCanICallThis.getNumbers(r,s);

Solution

  • This is really a software design question. To be honest, unless you're sharing the component with a bunch of other people, I would really worry too much about how its encapsulated.

    Many libraries (new and old) might make a family of functions that are to be used in a specific way. Sometimes they have a built in "state machine," but do not have a specific way of enforcing that the functions are used in a specific order. This is okay, as long as its well documented. A group of functions might be prefixed by the same word and packaged as a library if its to be re-usable, that way someone could link to your dll and include the appropriate headers.

    https://en.wikipedia.org/wiki/Finite-state_machine

    A finite-state machine (FSM) or finite-state automaton (FSA, plural: automata), finite automaton, or simply a state machine, is a mathematical model of computation.

    Another way of organizing a set of computations is to package them up as a class, allow the contructor to accept the required input, and...

    1. Kick off the required computation in the constructor
    2. Kick off the computation after calling a public function like ->compute()
    3. Making it into a functor (a class with an overloaded set of () operators)
    4. Along with basically countless other options...

    This has some benefit because if later you have multiple ways to compute results, you can replace it on the fly using something called the Strategy Pattern.

    https://en.wikipedia.org/wiki/Strategy_pattern

    In computer programming, the strategy pattern (also known as the policy pattern) is a behavioural software design pattern that enables selecting an algorithm at runtime.

    In the end, it doesn't matter which you use as long as you are consistent. If you are looking to make something more "idiomatic" in a given language, you really have to go out there and look at some code on the web to get a feel for how things are done. Different communities prefer different styles, which are all in the end subjective.