Getting the knack of OO coding



Email    print   
May 15, 2008 —  (Page 1 of 3)

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.



Related Search Term(s): Programming

Pages 1 2 3 


Share this link: http://sdt.bz/32074
 
Most Read Latest News Blog Resources

Add comment


Name*
Email*  
Country     


  • Comment
Loading




close
NEXT ARTICLE
Code Watch: Here comes functional programming
C#'s popularity means functional programming is here to stay and poised to overcome its roadblocks Read More...
 
 
 
 
News on Monday
more>>
SharePoint Tech Report
more>>


   

 
 

Download Current Issue
FEBRUARY 2012 PDF ISSUE

Need Back Issues?
DOWNLOAD HERE

Want to subscribe?


 
blogs tab
Are you at risk for burnout?
Burnout is a severe problem and it can strike at any time. Here's how to tell if you are nearing the edge.
02/09/2012 02:16 PM EST

Agility, mom, and apple pie
If we're to evaluate the state-of-the-art in software development, we should start with the values espoused in the Agile Manifesto.
02/07/2012 11:57 AM EST

RIM woos developers with free tablet
How do you get more apps ported to the BlackBerry PlayBook? By giving every developer a free tablet, of course!
02/04/2012 01:57 PM EST

GitHire: Use Headhunters to Find Your Perfect Programmer
Are you a hiring manager tired of scouring the job boards? Check out this new service that will find 5 people interested in your jobs.
02/03/2012 12:17 PM EST

Facebook claims hacker cred
Facebook's SEC S-1 filing form includes a short essay on the Hacker Way by Mark Zuckerberg himself.
02/02/2012 08:26 AM EST

Ryan Dahl steps down
Ryan Dahl, creator of Node.js, steps back from his position as gatekeeper for the project.
02/01/2012 04:58 PM EST

 
Events calendar tab
2/13/2012 to 2/16/2012
Santa Clara
TechWeb

2/26/2012 to 2/29/2012
San Francisco
BZ Media

2/27/2012 to 3/2/2012
San Francisco
RSA

3/4/2012 to 3/7/2012
Las Vegas
IBM Tivoli

3/5/2012 to 3/9/2012
San Francisco
TechWeb