Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cyclic dependencies between classes

I am having troubles with dependencies between two classes. The problem is the following:

I got two classes, Timestamp and Exception. Exception is abstract and all possible exceptions derive from this abstract class. Every exception has a timestamp to tell when the exception was thrown. So exceptions need to include (in the language i am using it is called import) the timestamp class. But wen working with timestamps errors can occur, so that exceptions are thrown. Therefor the timestamp class has to import the exception classes.

And there is my cyclic dependency. Now my actual question is (and this is why it is independent from the language): What would be a proper design in such a case to avoid cyclic dependencies? I fail to solve this problem as I cannot figure out a solution to have this classes as independent as they are now but without the cyclic dependency.

like image 535
M0rgenstern Avatar asked Sep 08 '26 13:09

M0rgenstern


2 Answers

I'd drop the whole "Exception-has-timestamp-property" idea. Exceptions should indicate exceptional behavior and nothing else. Perhaps you want to do some logging? It makes more sense for the logger to keep track of the timestamps and pair them with their appropriate exceptions.

Ask yourself what the job of an exception is. Obviously, an exception's job is to get thrown. Does this depend on when it gets thrown? Nope. Does the exception care about this at all? Nope. Does something else care? Yes, the exception logger. But, since the logger is the one who cares about timestamps, the logger should also get those timestamps, and do stuff with them. It's part of its job, after all. And, in a proper design, the logger does not outsource parts of his work to exceptions.

Cyclic dependency solved.

Good Question, especially that you want to avoid the cyclic dependency instead of just getting it to work. But consider that cyclic dependencies aren't bad practice in general. In your case, I is reasonable to make use of both class from each other.

Therefore many languages allow you to define class prototypes. I guess you are using python, though in C++ this looks this way.

class Timestamp;

You can now define the Exception class and use Timestamp object as members. Of course you cannot use it's methods since they aren't defined yet. But say if the Timestamp constructor initialized it's instance to the current time, you may not need to access members from the Exception class.

class Exception
{
    // ...
    Timestamp timestamp;
};

Later on you define the whole Timestamp class.

class Timestamp
{
    Timestamp()
    {
        // initialize to current time
    }
};

But note that coupling two classes this way makes them very dependent. You cannot use one of them without the other.

So you may want to drop the idea to use a Timestamp class for exceptions just to store the time of their occurrence. I guess fetching the actual time stamp just takes you some lines on most operating systems. So if you don't need the whole Timestamp class I suggest to duplicate the code to get the current time. This may be the most practical approach for example if you want to write exceptions along with their time in a log file.

like image 40
danijar Avatar answered Sep 11 '26 21:09

danijar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!