Object Callisthenics

In a nutshell

A way to write really beautiful object-oriented code with draconian limitations. Start with explaining the rules. Be mindful of caveats.

Rules

9 rules are as follows:

  1. Use only one level of indentation per method
  2. Don’t use the else keyword
  3. Wrap all primitives and strings
  4. Use only one dot per line
  5. Don’t abbreviate
  6. Keep all entities small
  7. Don’t use any classes with more than two instance variables
  8. Use first-class collections
  9. Don’t use any getters/setters/properties

Don’t use the else keyword

And if (true) {}; if (false) is disallowed also. 😛 Somebody always asks.

Use only one dot per line

Caveat for some languages, think Ruby and RSpec for instance. Also problem with fluent interfaces. Both cases are allowed as exceptions to the rule.

Don’t use any classes with more than two instance variables

That one can be difficult, but do try. If you have a collection, see next one.

Use first-class collections

My personal favourite. If you have a collection, wrap it in a type class and make instance of that type class one of two allowed instance variables. Then provide API to underlying collection in a type class.

Don’t use any getters/setters/properties

No public or protected fields that you can access without field accessor. No field accessors. That is equivalent to changing the way you do OO – not by direct state manipulation on another object.

More reading

I shall add more links here, which I perused once I was preparing for my OC, including my own blog post (now a draft) about how it went – later.

A very good starting point for me was Mark Needham’s account of Object Callisthenics.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s