views:

138

answers:

4

I haven't coded in java for a long time, and after coding in C, I'm having issued organizing my code for OOP. More specifically I'm not sure when to create a new method, and when to create a new class, and when to just lump everything together.

Are there some general rules or guidelines on how it should be done?

+10  A: 

Take a look at the SOLID principles.

EDIT (some more pointers):

You need a SOLID GRASP of some design principles.

To start small, take a look at these first:

When writing code, high maintainability should be your ultimate goal, and it's all about assigning responsibilities and separation of concerns.

Jordão
Damn! Just beat me to it. Nice answer - great minds thinking alike.
duffymo
Maybe a little too advanced for me still. I'm thinking way way more down to the basics!
Adam
No, not that advanced. You couldn't possibly have determined that in such a short period of time. Read them....more than once.
duffymo
@Adam: it's not that advanced, it's just abstract. Use the solid principle names to look for examples, like here: http://msdn.microsoft.com/en-us/magazine/cc546578.aspx
Ozan
This answer deserves to be selected. It's just asinine that the asker dismissed it within 20 minutes of it being posted, in favor of an answer straight out of "OOP for Future Presidents."
Jesse Dhillon
+4  A: 

Well designed objects are SOLID.

This is a difficult question, one that everyone who worries about design has to figure out. I think it's the hardest part of using objects, light years beyond mere syntax.

duffymo
I agree, this question is almost, but not quite entirely _unlike_ asking [the answer to life, the universe and everything](http://www.google.com/#q=the+answer+to+life%2C+the+universe+and+everything). There's no single correct answer. Only experience, principles, and deliberate practice of the [craft](http://manifesto.softwarecraftsmanship.org/) will help.
Jordão
+1  A: 

Is is something that you think of as a thing, entity or actor in your system. Well then it's an object so make a class to represent it. There's no need to make it more difficult than it is.

Khorkrak
+1  A: 

First of all, never just lump everything together. Try to identify the objects first. Build a class for each object your program will work with. If you're building an application for truck drivers, you will need a class for the driver, the truck, the load he's hauling, there's really no limit to how far you can break these bigger objects down. As for the methods, a method handles an action for the object. Truck.Start() would start the truck. Drive() would start it driving, etc... Maybe the Drive method takes a Route object for an argument which contains the roads to drive on. In short, create a method when an object needs to do something and create a class when you want to deal with another type of object.

kirk.burleson