Being Cellfish

Stuff I wished I've found in some blog (and sometimes did)

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

Change of Address
This blog has moved to blog.cellfish.se.

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

  • Comments 2

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.

  • I believe this is also called the Law of Demeter.

  • I believe you're right.

Page 1 of 1 (2 items)