Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am pursuing object oriented design. So we don\'t want to use globals but yet s

ID: 655521 • Letter: I

Question

I am pursuing object oriented design. So we don't want to use globals but yet sometime we have a module that most other modules use. This means we have to just pass it to all other modules and this can be literally all modules!

class Logger
{
public:
    Logger(void);
    ~Logger(void);
};


class Calculator
{
public:
    Calculator(Logger *logger);
    ~Calculator(void);
};


class Taxation
{
public:
    Taxation(Logger *logger);
    ~Taxation(void);
};
So the idea is that the Calculator and Taxation modules use the Logger module to log everything. Similarly most other modules will use it too, and logging is required everywhere. Is this method okay or should Logger be global instead?

Explanation / Answer

Avoiding global variables (and singletons, that are global variables in disguise) is a good thing to strive for. The most important is not to think about it as global and singletons being impure OO, that doesn't get you anywhere. The important concept to keep in mind is 'dependencies'. Any dependency that is explicitly injected into the client is loosely coupled. Any dependency that is invisible from the outside of the client is tightly coupled. Yes, it takes more code to explicitly inject dependencies, but your code ends up being more flexible, more testable and also easier to reason about.

In C++, if you can, inject your dependencies as template parameters. If that isn't an option, consider using std::function. Passing by raw pointer isn't a good idea, but you could consider using a const reference.

B.t.w, a while ago resulting from a linkedin discussion I created a logging library (that later others contributed to) excactly for the reason that in logging libraries the singleton pattern is grocely overused. There is no need to use singletons (or global variables) for things like a logging library.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote