02/12/2009 12:44
Originally I wrote this as an answer to this question on stackoverflow:
OOP - How to choose a possible object candidate?
However, It was a bit long, so I stripped it of the talking points and just kept the basics.
So the short answer to the question of “How do I write good OOP code?” is:
Just write something that gets the job done, even if it’s ugly, then refactor continuously:
But:
It’s not a precise recipe, just some general guidelines. Keep practicing.
Now, here’s the problem: lots of people learn OOP from Java books or Java schools, and most of what they learn is BS.
Don’t believe what the Java books/schools teach about objects; they’re lying.
Code objects are not related to tangible real-life objects; they are just constructs that hold related information together. Don’t think in terms of verbs and nouns; that’s a bunch of BS.
OOP is not an end, it’s just the means. If you follow the above mentioned guidelines, and your code doesn’t end up as a massive class hierarchy, don’t be sad, but be glad: for you have produced elegant code.
Classes don’t exist “out there”, you write them, as practical tools to solve your problem.
I think practical is the keyword here. Don’t try to come up with text-book abstractions.