Never pass this as an argument

It is very thempting to pass this as an argument to implement a bidirectional reference or a Visitor.

The problem is that you are giving away to much, it is just like pouring the bath with the baby. First let’s have a look at the alternative (no I wii not use the Visitor without accept):

class MyClass {
class MyRef{…}
… }

Yes, actually it is only an inner class that contains the reference to this. But this is enough in nearly all cases as this inner class (you could have even many of them) exposes just the methods you want to expose.

This sound paranoid, well it is, but it is there to express intend to have loose coupling. A typical use of passing this is a tree- structure

new Node(Node parent) {this.parent = parent };

where

void addNode() { nodes.add(new Node(this));}

adds a new Node. As Node is now a stupid little node, nobody could argue that passing this and having Node.parent as a member would create problems with coupling.

This changes when Node is replaced by some huge class that happens to form a tree. If all Nodes are of the same type, still no big problem (although the call to privates via parent is scary at least). If they are different but still share some interface that goes beyond the structural aspect you have exposed an interface that wasn’t intended to be exposed. If no a collaborator undergoes a change, calls from children to their parents had been introduced that are unwanted by the original designer of the class.

This is a key property of a good architecture: It should preserve the original idea and protect it against unintnetional changes. This does not mean that the architecture is rigid, it just preserves it properties when being used - and if the only important property was flexibilty for changes.

BTW: Nobody will hold you back by declaring MyRef extends MyClass and delegating everything. This might well be the proper desing for the given problem. But it might not be for good. When widening MyClass you now can easily let MyRef stay with the old interface if you think the new methods would introduce too much coupling.