Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go from bad to good OOP design?

I'm reading a lot about good and bad practices in OOP design. It's nice to know your design is bad, or good. But how do you get from bad to good design? I've split the interface (xaml) and codebehind from the main businesslogic class. That last class is growing big. I've tried splitting it up into smaller classes, but I'm stuck now. Any ideas on how to split large classes? The main class has 1 list of data of different types. I'm doing calculations on the total, but also on the individual types. I've got methods to perform these calculations which are called from events handled in the codebehind. Any ideas where to go from here?

Additional Info:

We are already about 6 months into this project. I've worked with object oriented laguages for years (first c++, java and now c#), but never on a large project like this one. I believe we've made some wrong turns in the beginning and I think we need to correct these. I can't specify any details on this project at the moment. I'm going to order one or two books about design. If I separate all the classes, how do I stick them back together? Maybe it's even better to continue this way to the first release and rebuilt parts after that, for a second release?

like image 255
Sorskoot Avatar asked Jan 13 '09 20:01

Sorskoot


2 Answers

Practice and read. Repeat :)

Some recommended books:

  • Clean Code by Robert C Martin
  • GoF Design Patterns
  • Refactoring by Martin Fowler

Personally I also liked Head First Design Patterns, but the style may not be for everyone. There's a similar book called C# 3.0 Design Patterns (see ora.com). It has much of the same stuff, but in a more traditional manner.

like image 123
Brian Rasmussen Avatar answered Sep 30 '22 14:09

Brian Rasmussen


I highly recommend picking up Code Complete. It's a great book that offers tons of good advice on questions like yours.

To give you a quick answer to your question about how to split large classes, here's a good rule of thumb: make your class responsible for one thing, and one thing only. When you start thinking like that, you quickly can identify code that doesn't belong. If something doesn't belong, factor it out into a new class, and use it from your original class.

Edit: Take that thinking down to the "method" level, too - make your methods responsible for one thing, and one thing only. Helps break large (>50 line) methods down very quickly into reusable chunks of code.

like image 22
Rob Avatar answered Sep 30 '22 13:09

Rob