tags:

views:

103

answers:

7

Hello,

I've read all the books about why to create a class and things like "look for the nouns in your requirements" but it doesn't seem to be enough. My classes seem to me to be messy. I would like to know if there are some sort of metrics or something that I can compare my classes to and see if there well designed. If not, who is the most respected OO guru where I can get the proper class design tips?

A: 

i think Object design is as much art as it is science. It takes time and practice to understand how to design clean & elegant classes. Perhaps if you can give an example of a simple class you've designed that you aren't happy with SO users can critique and give pointers. I'm not sure there are any general answers outside of what you've already read in the texts.

Jason
also, which language are you using? although OOD is language neutral, using language specific features in your class can make it cleaner.
Jason
A: 

The most respected OO guru i personally know is StackOverflow. Put your classnames here and i reckon you'll get a goodly number of reviews.

tehvan
+2  A: 

if you're familiar with database design, specifically the concept of normalization, then the answer is easy: a data-centric class should represent an entity in third normal form

if that is not helpful, try this instead:

  • a class is a collection of data elements and the methods that operate on them
  • a class should have a singular responsibility, i.e. it should represent one thing in your model; if it represents more than one thing then it should be more than one class.
  • all of the data elements in a class should be logically associated/related to each other; if they aren't, split it into two or more classes
  • all of the methods in a class should operate only on their input parameters and the class's data elements - see the Law of Demeter

that's about as far as i can go with general abstract advice (without writing a long essay); you might post one of your classes for critique if you need specific advice

Steven A. Lowe
@[Celebrus]: thank you
Steven A. Lowe
A: 

Classes are typically used to model concepts of the problem domain. Once you have a well-defined problem (aka the set of use cases), you will be able to identify all participants. A subset of the participants will be intrinsic to the system you are designing. Start with one big black box as your system. Keep breaking it down, as and when you have more information. When you have a level where they can no longer be broken down (into concepts in your problem domain), you start getting your classes.

But then, this is a subjective view of a non-guru. I'd suggest a pinch of salt to the menu.

dirkgently
+3  A: 

Creating classes that start clean and then get messy is a core part of OO, that's when you refactor. Many devs try to jump to the perfect class design from the get go, in my experience that's just not possible, instead you stumble around, solving the problem and then refactor. You can harvest, base classes and interfaces as the design emerges.

MrTelly
if you don't know how to design a good class in the first place, chances are that you also don't know how to refactor a class to make it better... ;-)
Steven A. Lowe
A: 

Metrics? Not so's that you'd trust them.

Are your classes doing the job of getting the program working and keeping it maintainable through multiple revisions?

If yes, you're doing ok.

If no, ask yourself why not, and then change what isn't working.

Paul Nathan
+1  A: 

Try to focus on behaviour instead of structure. Objects are 'living' entities with behaviour and responsibilities. You tell them to do things. Have a look at the CRC-card approach to help you model this way.

soemirno