"Don't talk to strangers". It's the simple explanation of the Demeter's Law.
It's also known as the "Law of train wrecks" A -> B -> C
A should only know about it's closest "friends". It shouldn't access something from C. Instead, it should tell B to DO something, not to ask B about C.
TELL, DON'T ASK
- O itself
- M's parameters
- Any object CREATED within M
- O's direct component objects (O's instance variables)
Example:
public void ShowBallance(BankAccount account)
{
Money amount = account.GetBallance();
this.PrintToScreen(amount.PrintFormat());
}
This violates the low, because it can be written like: this.PrintToScreen(account.GetBallance().PrintFormat()); // it's an obvious violation
Code should be:
public void ShowBallance(BankAccount account)
{
account.PrintBallance(); // TELL, DON'T ASK!
}