Object Calisthenics: Rule 4: Use only one dot per line

Not only is a long line with a lot of dots harder to understand. It usually means that your object is using not only the objects it knows but the objects their "friends" know too. Having only one dot per line does not only make your code more readable. It also helps putting the code in the right place. Consider this method:

1: public void Update()
2: {
3: context.CurrentSession.Update();
4: }

Changing it to this is not the intention of this rule:

1: public void Update()
2: {
3: Session session = context.CurrentSession;
4: session.Update();
5: }

Rather the rule want you to do this:

1: private void Update()
2: {
3: context.Update();
4: }

And here the Update method of the context object has been left out, but it would only call Update on the CurrentSession object. All in all I think this is a rule that is suitable for production code too.

Comments (2)
  1. John says:

    I believe this is also called the Law of Demeter.

  2. I believe you’re right.

Comments are closed.

Skip to main content