Tuesday, March 25, 2008

Class Creation - To Create or Not To Create, That Is The Question.

It can be difficult to decide when to create a class. Most of the time it's clear when you need to create a class to perform a task but I've put together a list for those edge cases. Hopefully this will help you resolve your dilemma.

Reasons to create a class:


  • Model real-world objects
  • Model abstract objects
  • Reduce complexity
  • Isolate complexity
  • Hide implementation details
  • Limit effects of changes
  • Hide global data


Reasons not to create a class:

  • I want a class of my own
  • I want to name a class after myself
  • Tied of adding code to the same class
  • Because I want to


A large class or method is a telling sign that you need to refactor, but you need to elaborate on what your class or method is doing. Then try to break it out into logical or physical operations.

Also when you create your new class be sure to put it in it's own code file. I've found that maintaining a one to one relationship with your class and code files is critical to the maintainability of your code.

Save to del.icio.us Add to Technorati Add to dzone

No comments: