Jump to content

  • Log In with Google      Sign In   
  • Create Account


Wilfrost

Member Since 06 Oct 2012
Offline Last Active Nov 28 2012 09:50 PM

#4989858 For Beginners: The Importance Of Object Oriented Design

Posted by Wilfrost on 13 October 2012 - 01:04 PM

This is good stuff, please keep it coming!


#4987848 "Getters" and "Setters" - do you write them if you don't...

Posted by Wilfrost on 07 October 2012 - 09:15 PM

If you're writing software using object-oriented techniques, you rarely need getters and setters. Conversely, if you have a lot of getters and setters, you're not using object-oriented techniques, you're doing structured procedural programming. Don't fool yourself that because you use the 'class' keyword you're necessarily doing anything object-oriented.

An object-oriented ball doesn't ever need something else to tell it what its size is. Nothing else needs to know its size. It knows how to collide with walls and paddles. It knows how to draw itself.


I really like the way you stated that. I guess I have some thinking to do about the way my "objects" handle themselves.


#4987847 "Getters" and "Setters" - do you write them if you don't...

Posted by Wilfrost on 07 October 2012 - 09:10 PM

Why write a function for something that's never going to change? While this may not occur much in the home-brew & hobby-est areas, I feel that you're adding the ability to mess with your game. You're just asking someone to abuse a function that was never meant to be used.


I guess I just have this little voice in the back of my head that says "Hey, you might want to have control over that some day... better write a set function for it"

Sorry if this reads weird - I have a bloody headache. :/


Hope your head feels better!

"Getters" and "Setters" are an interface to the data contained in your class. It is typically good practice to do this when you allow data in the class to be accessed externally.
For instance if you had a variable in your Ball class named "speed", you may want to increase this as the game goes on longer. This would require a setter because the game needs to externally increase it. Then lets say you would like to check to see if the ball is off screen. This would require a getter for the position to figure out where the ball is.
Of course you could make the data public in the class but this is considered bad style and should be avoided except when absolutley necessary.

If it is only used in the class and never changed outside the class it should not have a getter or setter.

Hope this helps.


I does! I appreciate everyone's opinion on the subject.

If you do have a class that needs to have a lot of accessible/modifiable attributes, consider just using a struct with public data members and no functions.


Are structs still around? I know that you can *technically* use them in C++ but I've never seen them called for in any C++ book I have, and tbh I have NEVER seen a struct used in Java - the language I have the most experience in (which is not saying much).

Thanks for all the great insights everyone, keep them coming!


#4987830 First "game" in C++

Posted by Wilfrost on 07 October 2012 - 08:10 PM

I'm currently working on my first game - a clone of pong written in Java. After coding for several hours I had "hit the wall" so to speak, so I went and found a "Write pong in Java" tutorial online. I looked through it, to see how they had tackled the problems I was having trouble with. After understanding their code I went back and continued working on my code with the knowledge I had gained. This way I'm not just doing a copy / paste. I can call the code my own because I have written every line of it myself. All I "took" from the tutorial was the basic concept of how to attack the problem. You might try finding a similar Zork tutorial and scan through it for optimizations that will make your code easier to handel. Happy coding!


PARTNERS