Our C++ professor mentioned that using the result of operator-> as input into another operator-> is considered bad style.
So instead of writing:
return edge->terminal->outgoing_edges[0];
He would prefer:
Node* terminal = edge->terminal;
return terminal->outgoing_edges[0];
There's a number of reasons.
The Law of Demeter gives a structural reason (note that your C++ professors code still violates this though!). In your example, edge
has to know about terminal
and outgoing_edges
. That makes it tightly coupled.
As an alternative
class Edge {
private:
Node* terminal;
public:
Edges* outgoing_edges() {
return terminal->outgoing_edges;
}
}
Now you can change the implementation of outgoing_edges
in one place without changing everywhere. To be honest, I don't really buy this in the case of a data structure like a graph (it is tightly coupled, edges and nodes can't escape each other). This'd be over-abstraction in my book.
There's the null dereference problem too, in the expression a->b->c
, what if b
is null?
You should ask your professor as to why he considers it bad style. I don't. I would however consider his omission of the const in the declaration of terminal to be bad style.
For a single snippet like that, it's probably not bad style. However consider this:
void somefunc(Edge *edge)
{
if (edge->terminal->outgoing_edges.size() > 5)
{
edge->terminal->outgoing_edges.rezize(10);
edge->terminal->counter = 5;
}
++edge->terminal->another_value;
}
This is starting to get unwieldy - it is difficult to read, it is difficult to write (I made approximately 10 typos when typing that). And it requires a lot of evaluation of the operator-> on those 2 classes. OK if the operator is trivial, but if the operator does anything exciting, it's going to end up doing a lot of work.
So there's 2 possible answers:
And a single snippet like that, you can't avoid the extra line. In something like the above, it'd have resulted in less typing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With