Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have been learning C# and am trying to tackle some common projects / works of my own to become even better. Currently I am working on understanding the Mars Rover Problem. I read the description and have seen several solutions, they seem nice and follow the principles of OO design.

  • How do you come to understand what classes you need to make for a project?
  • Is there a book I can read to grasp the concept of OO design better?

I understand in theory, but I just don't get how and what I would do when actually given a problem.

For instance I see the Mars Rover problem and instantly in my head I think Ok I can get that done with just 3 classes, a Rover, Grid and a CommandCenter class. Then I look at online examples and there are a lot more classes, interfaces and overall breakdown.

share|improve this question

marked as duplicate by gnat, Bart van Ingen Schenau, Florian Margaine, Corbin March, Dan Pichelman Aug 7 '13 at 15:55

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
I would start with trying to understand the four tenets of object orientation briefly discussed in this article here codebetter.com/raymondlewallen/2005/07/19/… . These tenets are the foundation upon which object oriented systems are built. Here is a much more deep dive and walkthrough article on the principles of OO which should help to make things much clearer codeproject.com/Articles/567768/… . –  bUKaneer Aug 5 '13 at 21:48
    
@bUKaneer Those articles are very helpful, Thanks! And I didn't come across those other questions on my initial search, but I definitely think that some of the answers to those questions apply here too. –  RWhite Aug 6 '13 at 19:56
    
The solution published is overkill IMO. I wouldn't use nearly that much code. For example, it is ridiculous to have eight functions and 50 lines of code dedicated to calculating the new direction resulting from turning right or left. –  kevin cline Aug 6 '13 at 20:35
    
voted to reopen - and not just because my answer was accepted ;) - I think this question is less a how-do-i-learn-oop and more of a given-a-problem-where-do-i-start –  Steven A. Lowe Aug 7 '13 at 17:25

5 Answers 5

up vote 3 down vote accepted

How do you come to understand what classes you need to make for a project?

Examine the nouns and verbs in the problem/solution description. It may not give you the final model, but it's a good place to start. For example, in the Mars Rover Problem we have:

A squad of robotic rovers are to be landed by NASA on a plateau on Mars.

(Nouns are bolded, verbs italicized.)

a rover lands on a plateau

redRover.landOn(plateau)

a squad (collection) of rovers lands on a plateau

redSquadron.landOn(plateau)

and so on

Is there a book I can read to grasp the concept of OO design better?

Yes, several. I like Booch's books, but your mileage may vary. In any event, read more than one.

share|improve this answer
    
I remember you were also writing one of your own. Right? :) –  mlvljr Aug 6 '13 at 20:39
    
@mlvljr: yes, obviously that got stalled by Life and other unimportant trivia. Haven't given up though! –  Steven A. Lowe Aug 6 '13 at 20:48
1  
Knew that! G'luck, mr. Lowe! :) –  mlvljr Aug 6 '13 at 20:49

Start Test-Driven Development (TDD). It almost forces you to write more OO code.

For me, I only really started understanding proper OO after doing MVVM and TDD together. Writing testable code is soooo much easier when you're writing OO code.

share|improve this answer
    
-1 there needs to be something more that TDD, its possible to do TDD in functional programming languages and OO code is not the result –  jk. Aug 6 '13 at 12:10
1  
TDD force you to decouple your code, that is a given. In OO language like Java or C#, TDD usually lead to OO code, since OO is the easy way to decouple in those languages. TDD is not the end of the story, of course, but it sure is of great help! –  Laurent Bourgault-Roy Aug 6 '13 at 18:46
    
@jk: OO code is not a goal. It is a practice that helps us meet the goal of having a working, maintainable system. –  kevin cline Aug 6 '13 at 20:33

Personally, I would probably use only a few functions to solve that particular problem, let alone a few classes. More code doesn't imply better code.

However, if you're concerned that your classes are too big, a good learning exercise is to keep splitting up your solution into more and more classes until it's impossible to split it up further.

Sometimes it's easier to learn a principle by taking it to extremes. Then you can back off until it looks more appropriately applied. It's like focusing a lens. It's easier to see the proper focal point by taking it too far then backing up.

share|improve this answer

Experience. I don't know how else to put it. Learn what you can from experienced developers. Look at good open source projects and study their design. Anyone can memorize the software development patterns, its a much bigger step to apply them to business solutions in a meaningful way.

share|improve this answer

There are some questions, if you will ask to yourself everytime before writing a class or method or sub-routine te definitely following the OO Design:

  1. Classes: Every time you need to define something that is completely exclusive then use a class instead of overburdening existing class. E.g.

  2. Use Degenerate Classes whenever required: Every time when you need to put some common variable together then use Degenerate classes to hold all of them together. E.g

       class Point    {
       int x;
       int y;
       Point(int x, int y)
       {
          this.x = x;
          this.y = y;
       }
       }
    

    Now you can simply use Point class object to access x and y anywhere.

  3. Always break every task in Methods. Eg.

    accessibility return_type method_name(params) { sub_routines1; sub_routines2; sub_routines3; }

    if you ever need a large body of method to do something then break its inner functionality in sub-routines which will work as helper methods to fulfill the functionality of the method, they are part of. You can then also use sub-routines anywhere you want to use it.

    1. Always take care of Making classes instantiable or non-instantiable based on your requirement.
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.