Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Object Calisthenics - A fresh look on OO Progra...

Object Calisthenics - A fresh look on OO Programming

Object Calisthenics is an exercise on object oriented programming introduced by Jeff Bay where 9 simple rules can help you go back to basics and revisit object oriented programming through a new perspective, more like a code kata rather than absolute principles.

Samuel Gordalina

November 13, 2013
Tweet

More Decks by Samuel Gordalina

Other Decks in Programming

Transcript

  1. What are Object Calisthenics? Jeff Bay listed 9 rules to

    writing better Object Oriented Code @sgordalina
  2. class Board { // ... String board() { StringBuffer buf

    = new StringBuffer(); for(int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) buf.append(data[i][j]); buf.append("\n"); } return buf.toString(); } } @sgordalina
  3. class Board { // ... String board() { StringBuffer buf

    = new StringBuffer(); collectRows(buf); return buf.toString(); } void collectRows(StringBuffer buf) { for(int i = 0; i < 10; i++) collectRow(buf, i); } void collectRow(StringBuffer buf, int row) { for(int i = 0; i < 10; i++) buf.append(data[row][i]); buf.append("\n"); } } @sgordalina
  4. class Board { // ... String board(StringBuffer buf) { if

    (buf.length()) { return buf.toString(); } else { collectRows(buf); return buf.toString(); } } // ... } @sgordalina
  5. class Board { // ... String board(StringBuffer buf) { if

    (!buf.length()) { collectRows(buf); } return buf.toString(); } // ... } @sgordalina
  6. It is hard to let go •ELSE is resident in

    our consciousness •Come up with other alternatives @sgordalina
  7. Small objects make you write semantically correct programs as well

    as feeding the compiler additional info about what it is (static analysis) @sgordalina
  8. Not possible in languages which are not completely object oriented

    (e.g.: PHP) which degrades performance @sgordalina
  9. class UIComponent { void repaint(boolean animate) { // ... }

    } // ... component.repaint(true); @sgordalina
  10. class Animate { Animate(boolean animate) { this.animate = animate; }

    } class UIComponent { // ... void repaint(Animate animate) { // ... } } // ... component.repaint(new Animate(true)); @sgordalina
  11. Any class that contains a collection should contain no other

    member variables Java Collections follow this rule @sgordalina
  12. If you are digging into other object’s properties or methods

    you are probably violating encapsulation @sgordalina
  13. The Law of Demeter “Only talk to your friends” is

    a good place to start refactoring @sgordalina
  14. class Board { // ... class Piece { // ...

    String representation; } class Location { // ... Piece current; } String boardRepresentation() { StringBuffer buf = new StringBuffer(); for(Location l : squares()) buf.append(l.current.representation.substring(0, 1)); return buf.toString(); } } @sgordalina
  15. class Board { class Piece { private String representation; String

    character() { return representation.substring(0, 1); } void addTo(StringBuffer buf) { buf.append(character()); } } class Location { private Piece current; void addTo(StringBuffer buf) { current.addTo(buf); } } String boardRepresentation() { StringBuffer buf = new StringBuffer(); for(Location l : squares()) l.addTo(buf); return buf.toString(); } } @sgordalina
  16. If you have to write the same name repeatedly, you

    are probably reusing it multiple times, signaling a code duplication @sgordalina
  17. Method name is too long? Maybe your class has multiple

    responsibilities you should probably rethink your architecture. @sgordalina
  18. “Most classes should simply be responsible for handling a single

    state variable, but there are a few that will require two.” - Jeff Bay @sgordalina
  19. class Name { Surname family; GivenNames given; } class Surname

    { String family; } class GivenNames { List<String> names; } @sgordalina
  20. Decomposition of object attributes leads to a more effective object

    model, one that represents a hierarchy of collaborating objects @sgordalina
  21. Probably the hardest rule to follow, but the most brain

    picking one. One needs to rethink the way code is written @sgordalina