CHANNELS
 
 
 
 
 
 
 
 
ON THE WEB
 
 
 
 
PRINT EDITION
 
 
 
 
BZ MEDIA
 
 
 
 
ADVERTISER LINKS
 
 
 
 
 
 
 
AS OF 11/19/2008 7:09AM EST
Getting the knack of OO coding
Stories Columns Opinions Resources

By Andrew Binstock

May 15, 2008 — 

One of the areas in which I most see poor coding style is in OO code written by developers, such as I, who were weaned on procedural code. Only by dint of many hundreds of rounds of refactoring has my Java code begun to look like native OO code and not an OO veneer over procedural code. I still have further to go to write those small, crisp methods as a natural expression of my thinking, but I am finally getting there.

This progress was realized by hard effort—an effort, I would add, that took far longer than I expected. The time and struggle would have been greatly reduced had I earlier read an essay in a new book from the Pragmatic Programmers called “The ThoughtWorks Anthology.” The essay I am referring to is “Object Calisthenics” by Jeff Bay. It’s a detailed exercise for perfecting the creation of small routines that demonstrate true OO design. If you have developers who need to improve their ability to write OO routines, I suggest you get this essay. I will summarize Bay’s approach here.

He suggests writing a 1,000-line program with the following constraints listed below. These constraints are intended to be excessively restrictive so as to force developers out of the procedural groove. I guarantee if you apply this technique, your code will move markedly towards object orientation. The restrictions (which should be mercilessly enforced in this exercise) are:

1.    Use only one level of indentation per method. If you need more than one level, you need to create a second method and call it from the first. This is one of the most important constraints in the exercise.

2.    Don’t use the “else” keyword. Test for a condition with an if-statement and exit the routine if it’s not met. This prevents if-else chaining and every routine does just one thing. You’re getting the idea.

3.    Wrap all primitives and strings. This directly addresses “primitive obsession.” If you want to use an integer, you first have to create a class (even an inner class) to identify its true role. So zip codes are an object, not an integer, for example. This makes for far clearer and more testable code.

4.    Use only one dot per line. This step prevents you from reaching deeply into other objects to get at fields or methods and thereby conceptually breaking encapsulation.

5.    Don’t abbreviate names. This constraint avoids the procedural verbosity that is created by certain forms of redundancy—if you have to type the full name of a method or variable, you’re likely to spend more time thinking about its name. And you’ll avoid having objects called Order with methods entitled shipOrder(). Instead, your code will have more calls such as Order.ship().

6.    Keep entities small. This means no more than 50 lines per class and no more than 10 classes per package. The 50 lines per class constraint is crucial. Not only does it force concision and keep classes focused, but it also means most classes can fit on a single screen in any editor/IDE.

7.    Don’t use any classes with more than two instance variables. This is perhaps the hardest constraint. Bay’s point is that with more than two instance variables, there is almost certainly a reason to subgroup some variables into a separate class.  

8.    Use first-class collections. In other words, any class that contains a collection should contain no other member variables. The idea is an extension of primitive obsession. If you need a class that subsumes the collection, then write it that way.

9.    Don’t use setters, getters or properties. This is a radical approach to enforcing encapsulation. It also requires implementation of dependency injection approaches and adherence to the maxim “tell, don’t ask.”

Taken together, these rules impose a restrictive encapsulation on developers and force thinking along OO lines. I assert than anyone writing a 1,000-line project without violating these rules will rapidly become much better at OO. They can then, if they want, relax the restrictions somewhat. But as Bay points out, there’s no reason to do so. His team has just finished a 100,000-line project within these strictures. And I’ll bet it’s amazingly readable code.

What’s particularly interesting is that these rules are what’s missing. For example, there is no long-winded discussion about favoring interfaces over inheritance. Or how to refactor. Or any of the endless lists of standard topics. Just hard-nosed encapsulation. Teach that to the developers who need it, and many of your concerns about their coding style will disappear.

Andrew Binstock is the principal analyst at Pacific Data Works. Read his blog at binstock.blogspot.com.


Related Search Term(s): Programming


Share this link: http://sdtimes.com/link/32074
 


 
 
 
 
 
 
 
 
 
 
SUBSCRIBE TODAY!
 E-Newsletters:
  News on Mon/Thurs.  More info
  Test & QA Report  More info
  EclipseNews  
  SPTech Report  More info
 
 
 
PDF & PRINT EDITION
* Requires Resource Account!  LOGIN or SIGN UP

Download Current Issue!
ISSUE 11/15/2008 PDF

Need Back Issues?
DOWNLOAD HERE

Receive The Print Edition?
SUBSCRIBE HERE
 
REGISTER
 
GET NOTIFIED!
About all of the latest Resources
 
 
SD TIMES 100
It's time once again to
recognize the organizations
or individuals that have
demonstrated leadership in
their markets.