How can I practice better object-oriented programming? [closed]

I have been programming in object-oriented languages for years now but secretly I look at some of the things my colleagues do with envy. A lot of them seem to have some inner OO instinct that I don't have - no matter how hard I try. I've read all the good books on OO but still can't seem to crack it. I feel like the guy who gave 110% to be a professional footballer but just didn't have the natural talent to make it. I'm at a loss and thinking of switching careers - what should do I?


Solution 1:

I would say focus less on the OO programming and focus more on the OO design. Grab a paper and a pencil (or maybe a UML modelling tool), and get away from the screen.

By practicing how to design a system, you'll start to get a natural feel for object relationships. Code is just a by-product of design. Draw diagrams and model your application in a purely non-code form. What are the relationships? How do your models interact? Don't even think about the code.

Once you've spent time designing... then translate it to code. You'll be surprised at just how quickly the code can be written from a good OO design.

After a lot of design practice, you'll start seeing common areas that can be modularized or abstracted out, and you'll see an improvement in both your designs and your code.

Solution 2:

The easiest way is to learn concepts such as SOLID, DRY, FIT, DDD, TDD, MVC, etc. As you look up these acronyms it will lead you down many other rabbit holes and once you are done with your reading you should have a good understanding of what better object-oriented programming is!

SOLID podcasts: http://www.hanselminutes.com/default.aspx?showID=168, http://www.hanselminutes.com/default.aspx?showID=163

SOLID breakdown: http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod

DRY: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself

FIT: http://www.netwellness.org/question.cfm/38221.htm

DDD: http://dddcommunity.org/

DDD required reading: http://www.infoq.com/minibooks/domain-driven-design-quickly

TDD: http://en.wikipedia.org/wiki/Test-driven_development

MVC: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

And yes, rolling up your sleeves and coding is always a good idea. Make a small project to the best of your current abilities. Then read an article from above. Then refactor your code to meet the needs of what you just read. Repeat until you have refactored the hell out of your code. At the end you should not only know what OO is all about but you should be able to explain why it is important and how to get their the first time. Learning how to refactor is a key to good code too. What is right now is not right tomorrow.

Solution 3:

Too many people think of coding first, objects, last.

You can read all the books you want but that's not going to teach you how to think in an object-oriented fashion--that takes practice and a certain methodology.

  1. Here are a few methods that have helped me: When you're away from work and open-minded you can practice by looking at everything as an object. Don't look at these objects and wonder how you're going to program them, look at them as properties and functions only and how they relate or inherit from each other. For example, when you see a person, they are an object and therefore would represent a class. They have properties like hair color, skin tone, height, etc. They do certain functions as well. They walk, talk, sleep, etc. Some of the functions these people do returns results. For example, their working function returns a dollar amount. You can do this with everything you see because everything is an object. Bicycle, car, star, etc.

  2. Before coding a project, design it by using post-it notes and a dry-erase board. This will make good practice until you get the hang of this. Think of your specific object/function/property. Each of those items will have its own post-it note. Place them as a hierarchy on the dry-erase board. In this regard, function/properties will be placed under the object. If you have another object, do the same for that one. Then ask yourself, do any of these post it notes (objects/functions/properties) relate to each other. If two objects use the same function, create a parent object (post-it note) and put it above the others with the reusable function under the new note. Draw a line using the dry-erase marker from the two child objects to the parent.

  3. When all this is done, then worry about the internals of how the class works.

Solution 4:

My suggestion would be to learn something different.

Learn functional programming, and apply what you learn from that to OOP. If you know C++, play around with generic programming.

Learn non-object-oriented languages.

Not just because you should use all these things as well (you should), or because they should completely replace OOP (they probably shouldn't), but because you can apply lessons from these to OOP as well.

The secret to OOP is that it doesn't always make sense to use it. Not everything is a class. Not every relationship or piece of behavior should be modeled as a class.

Blindly trying to apply OOP, or striving to write the best OOP code possible tends to lead to huge overengineered messes with far too many levels of abstraction and indirection and very little flexibility.

Don't try to write good OOP code. Try to write good code. And use OOP when it contributes to that goal.

Solution 5:

In many fields there's a "eureka" moment where everything kind of comes together.

I remember feeling frustrated in high school geometry. I didn't know which theorem to apply on each step of the proof. But I kept at it. I learned each theorem in detail, and studied how they were applied in different example proofs. As I understood not only the definition of each theorem, but how to use it, I built up a "toolbox" of familiar techniques that I could pull out as needed.

I think it's the same in programming. That's why algorithms, data structures, and design patterns are studied and analyzed. It's not enough to read a book and get the abstract definition of a technique. You have to see it in action too.

So try reading more code, in addition to practicing writing it yourself. That's one beauty of open source, you can download lots of code to study. Not all of that code is good, but studying bad code can be just as educational as studying good code.