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];
- Why is this considered bad style?
- How could I restructure my program to avoid the ‘bad style’ but also avoid the extra line of code that is created as per the above suggestion?
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:
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.