tags:

views:

276

answers:

8

I am a c++ programmer, and I am looking forward to learning and mastering OO design.I have done a lot of search and as we all know there is loads of material, books, tutorials etc on how to achieve a good OO design. Of course, I do understand a good design is something can only come up with loads of experience, individual talent, brilliance or in fact even by mere luck(exaggeration!).

But sure it all starts off with a solid beginning & building some strong basics.Can someone help me out by pointing out the right material on how to start off this quest of learning designing right from the stage of identifying objects, classes etc to the stage of using design patterns. Having said that I am a programmer but I have not had a experience in designing.Can you please help me take someone help me out in this transition from a programmer to a designer? Any tips,suggestions,advice will be helpful.

[Edit]Thanks for the links and answers, I need to get myself in to that :) As i mentioned before I am a C++ programmer and I do understand the OO basic concepts as such, like inheritance, abstraction, polymorphism, and having written code in C++ do understand a few of the design patterns as well.what i dont understand is the basic thought process with which one should approach a requirement. The nitty grittys of how to appraoch and decide on what classes should be made, and how to define or conclude on relationships they should have amongst themselves.Knowing the concepts(t some extent) but not knowing how to apply them is the problem i seem to have :( Any suggestions about that?

+4  A: 

As you said, there is nothing like experience. You can read every existing book on the planet about this, you'll still not as good as if you practice.

Understanding the theory is good, but in my humble opinion, there is nothing like experience. I think the best way to learn and understand things completely is to apply them in some project(s).

There you'll face difficulties, you'll learn to solve them, sometimes perhaps with a bad solution : but still you'll learn. And if at any time something bothers you and you can't find how to solve it nicely, we'll be here on SO to help you ! :)

ereOn
+2  A: 

I can advice you the book "Head First Design Patterns" (search Amazon). It is a good starting point, before seriously diving into the gang of fours' bible, and it shows design principles and the most used patterns.

vulkanino
My 2 cent: even though it is a good book to start working with design patterns, it is not a good starting point for learning how to design. You better have an idea of what the patterns are in what you're designing before letting a book decide how to do it for you.
stefaanv
generally I dont find the head first series very helpful. often the book provides too much distractions from the core messages and concept.
poseid
@vulkanino: Thanks for the reference to Head first patterns book. It is an Amazing book!
Als
+1  A: 

In a nutshell : Code, criticize, look for a well-known solution, implement it, and back to first step till you're (more or less) satisfied.

As often, the answer to this kind of question is : it depends. And in that case, it depends how you learn things. I'll tell you what work for me, for I face the very problem you describe, but it won't work with everybody and I would not say it's "the perfect answer".

I begin coding something, not too simple, not too complex. Then, I look at the code and I think : all right, what is wrong ? For that, you can use the first three "SOLID principles" :

  • Single responsibility (are all your classes serving a unique purpose ?)
  • Open/Close principle (if you want to add a service, your classes can be extended with inheritance, but there is no need to alter the basic functions or your current classes).
  • Liskov Substitution (all right, this one I can't explain simply, and I'd advise reading about it).

Don't try to master those and understand everything about them. Just use them as guideline to criticize your code. Think chiefly about "what if I want to do this now ?". "What if I work for a client, and he wants to add this or that ?". If your code is perfectly adaptable to any situation (which is almost impossible), you might have reached a very good design.

If it's not, consider a solution. What would you do ? Try to come with an idea. Then, read about design patterns and find one that could answer your problem. See if it matches your idea - often, it's the idea you had, but better expressed and developped. Now, try to implement it. It's going to take time, you'll often fail, it's frustrating, and that's normal.

Design is about experience, but experience is acquired by criticizing your own code. That's how you'll understand design, not as a cool thing to know, but as the basis for a solid code. It's not enough to know "all right, a good code has that and that". It's much better to have experienced why, to have failed and see what whas wrong. The trouble with design pattern is that they are very abstract. My method is a way (probably not the only one nor the best) to make them less abstract to you.

Raveline
Here is an answer with the SOLID principle: http://stackoverflow.com/questions/845966/oop-good-class-design/845984#845984, although I agree that SLID sounds sexier ;)
stefaanv
Oops. Corrected, thanks :)
Raveline
Thanks for the link, I need to get myself in to that :) As i mentioned before I am a C++ programmer and I do understand the OO basic concepts as such, like inheritance, abstraction, polymorphism, and having written code in C++ do understand a few of the design patterns as well.what i dont understand is the basic thought process with which one should approach a requirement. The nitty grittys of how to appraoch and decide on what classes should be made, and how to define or conclude on relationships they should have.
Als
A: 

The core concepts in my mind are:

  1. Encapsulation - Keep as much of you object hidden from both the prying eyes and sticky fingers of the outside world.
  2. Abstraction - Hide as much of the inner workings of you object from the simple minds of the code that needs to use your objects

All the other concepts such as inheritance, polymorphism and design patters are about incorporating the two concepts above and still have objects that can solve real world problems.

doron
+9  A: 
  1. (very) Simple, but not simplist design (simple enough design if you prefer) : K.I.S.S.
  2. Prefer flat hierarchies, avoid deep hierarchies.
  3. Separation of concerns is essential.
  4. Consider other "paradigms" than OO when it don't seem simple or elegant enough.
  5. More generally : D.R.Y. and Y.A.G.N.I help you achieve 1.
Klaim
+1 for consideration of other paradigms.... :)
liaK
It's even more important in C++ where you have natively access to other paradigms. In other languages, it often require to add a language over (C#/F#, Java/Groovy/Scala for example)
Klaim
@Klaim: C# has native support for many more functional features than C++.
missingfaktor
Yes, that don't go against what I said. :)
Klaim
@Missing Faktor: So? C# doesn't have the same support for generic programming, for example, as C++ does. And @Klaim wasn't comparing C++ to C# *exclusively*, but to other languages in general.
jalf
@Klaim: Thanks you got me started on some pretty cool OOD concepts :)
Als
Cool that you can already apply all that :)
Klaim
+1  A: 
  1. No-solo-work. Good designs are seldom created by a single person only. Talk to your colleagues. Discuss your design with others. And learn.
  2. Don't be too smart. A complex hierarchy with 10 levels of inheritance is seldom a good design. Make sure that you can clearly explain how your design works. If you can't explain the basic principles in 5 minutes, your design is probably too complex.
  3. Learn tricks from the masters: Alexandrescu, Meyers, Sutter, GoF.
  4. Prefer extensibility over perfection. Source code written today will be insufficient in 3 years time. If you write your code to be perfect now, but inextensible, you will have a problem later. If you write your code to be extensible (but not perfect), you will still be able to adapt it later.
Patrick
+4  A: 

There is no secret. It's sweat, not magic.

It's not about doing one thing right. It's balancing many things that must not go wrong. Sometimes, they work in sync, sometimes, they work against each other. Design is only one group of these aspects. The best design doesn't help if the project fails (e.g. because it never ships).

The first rule I'd put forward is:

1. There are no absolutes Follows directly from the "many things to balance. D.R.Y., Y.A.G.N.I. etc. are guidelines, strictly following them cannot guarantee good design, if followed by the letter they may make your project fail.

Example: D.R.Y. One of the most fundamental principles, yet studies show that complexity of small code snippets increases by a factor of 3 or more when they get isolated, due to pre/post condition checking, error handling, and generalization to multiple related cases. So the principle needs to be weakened to "D.R.Y. (at least, not to much)" - when to and when not is the hard part.

The second rule is not a very common one:

2. An interface must be simpler than the implementation

Sounds to trivial to be catchy. Yet, there's much to say about it:

The premise of OO was to manage program sizes that could not be managed with structured programming anymore. The primary mechanism is to encapsulate complexity: we can hide complexity behind a simpler interface, and then forget about that complexity.

Interface complexity involves the documentation, error handling specifications, performance guarantees (or their absence), etc. This means that e.g. reducing the interface declaration by introducing special cases isn't a reduction in complexity - just a shuffle.

3-N Here's where I put most of the other mentions, that have been explained already very well.

Separation of Concerns, K.I.S.S, SOLID principle, D.R.Y., roughly in that order.


How to build software according to these guidelines?

Above guidelines help evaluating a piece of code. Unfortunately, there's no recipe how to get there. "Experienced" means that you have a good feel for how to structure your software, and some decisions just feel bad. Maybe all the principles are just rationnalizaitons after the fact.

The general path is to break down a system into responsibilities, until the individual pieces are managable.

There are formal processes for that, but these just work around the fact that what makes a good, isolated component is a subjective decision. But in the end, that's what we get paid for.

If you have a rough idea of the whole system, it isn't wrong to start with one of these pieces as a seed, and grow them into a "core". Top-down and bottom-up aren't antipodes.

Practice, practice, practice. Build a small program, make it run, change requirements, get it to run again. The "changing requirements" part you don't need to train a lot, we have customers for that.

Post-Project reviews - try to get used to them even for your personal projects. After it's sealed, done, evaluate what was good, what was bad. Consider the source code was thrown away - i.e. don't see that sessison as "what should be fixed?"

Conway's Law says that "A system reflects the structure of the organizaiton that built it." That applies to most complex software I've seen, and formal studies seem to confirm that. We can derive a few bits of information from that:

  • If structure is important, so are the people you work with.
  • Or Maybe structure isn't that important. There is not one right structure (just many wrong ones to avoid)
peterchen
+1  A: 

I'm going to quote Marcus Baker talking about how to achieve good OO design in a forum post here: http://www.sitepoint.com/forums/showpost.php?p=4671598&postcount=24

1) Take one thread of a use case.

2) Implement it any old how.

3) Take another thread.

4) Implement it any old how.

5) Look for commonality.

6) Factor the code so that commonality is collected into functions. Aim for clarity of code. No globals, pass everything.

7) Any block of code that is unclear, group into a function as well.

8) Implement another thread any old how, but use your existing functions if they are instant drop-ins.

9) Once working, factor again to remove duplication. By now you may find you are passing similar lumps of stuff around from function to function. To remove duplication, move these into objects.

10) Implement another thread once your code is perfect.

11) Refactor to avoid duplication until bored.

Now the OO bit...

12) By now some candidate higher roles should be emerging. Group those functions into roles by class.

13) Refactor again with the aim of clarity. No class bigger than a couple of pages of code, no method longer than 5 lines. No inheritance unless the variation is just a few lines of code.

From this point on you can cycle for a bit...

14) Implement a thread of use case any old how.

15) Refactor as above. Refactoring includes renaming objects and classes as their meanings evolve.

16) Repeat until bored.

Now the patterns stuff!

Once you have a couple of dozen classes and quite a bit of functionality up and running, you may notice some classes have very similar code, but no obvious split (no, don't use inheritance). At this point consult the patterns books for options on removing the duplication. Hint: you probably want "Strategy".

The following repeats...

17) Implement another thread of a use case any old how.

18) Refactor methods down to five lines or less, classes down to 2 pages or less (preferably a lot less), look for patterns to remove higher level duplication if it really makes the code cleaner.

19) Repeat until your top level constructors either have lot's of parameters, or you find yourself using "new" a lot to create objects inside other objects (this is bad).

Now we need to clean up the dependencies. Any class that does work should not use "new" to create things inside itself. Pass those sub objects from out side. Classes which do no mechanical work are allowed to use the "new" operator. They just assemble stuff - we'll call them factories. A factory is a role in itself. A class should have just one role, thus factories should be separate classes.

20) Factor out the factories.

Now we repeat again...

21) Implement another thread of a use case any old how.

22) Refactor methods down to five lines or less, classes down to 2 pages or less (preferably a lot less), look for patterns to remove higher level duplication if it really makes the code cleaner, make sure you use separate classes for factories.

23) Repeat until your top level classes have an excessive number of parameters (say 8+).

You've probably finished by now. If not, look up the dependency injection pattern...

24) Create (only) your top level classes with a dependency injector.

Then you can repeat again...

25) Implement another thread of a use case any old how.

26) Refactor methods down to five lines or less, classes down to 2 pages or less (preferably a lot less), look for patterns to remove higher level duplication if it really makes the code cleaner, make sure you use separate classes for factories, pass the top level dependencies (including the factories) via DI.

27) Repeat.

At any stage in this heuristic you will probably want to take a look at test driven development. At the very least it will stop regressions while you refactor.

Obviously, this is a pretty simple process, and the information contained therein shouldn't be applied to every situation, but I feel like Marcus gets it right, especially with regards to the process one should use to design OO code. After a while, you'll start doing it naturally, it'll just become second nature. But while learning to do so, this is a great set of steps to follow.

EricBoersma