Jump to content

  • Log In with Google      Sign In   
  • Create Account

help/follow me evolve as a programmer[starting with Pong]!

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • This topic is locked This topic is locked
22 replies to this topic

#21 warnexus   Prime Members   -  Reputation: 1369


Posted 17 April 2013 - 01:50 PM

The code you posted up contains 2 classes:




For a game or any type of software application, the codebase must be broken up into multiple classes so your codebase can be maintained and reusable. In programming, an application is only good if you can reuse the code for let's say another project or easy to maintain so when someone else looks at your code and want to add a new feature they can easily look at your code and say "I understand what he did". Explain why you wrote the code the way you did using comments. We can't read your mind about what you did.


Learn and understand how to restructure your code which is a process of moving and tweaking your code around so the code can be easy to and reusable.


The issue I see is with the PongGame class. PongGame class is consider to be a "godclass"(at least for the time being) which means it is a class that knows too much about the internal implementations of other objects in your game (whether it be variables or methods of other objects). You want to have all the variables and methods of Ball inside a new class you create called Ball. So create a new class called Ball and put all those variable there and then create the Ball object inside the PongGame class. 


What PongGame class is suppose to know is that it only needs to set up the objects in the game not have variables of the Ball. So when it is time to start the game loop, it knows it needs to load the ball, paddle, score and then in game loop: draw and update and check for collision.


Why would you want to do this? Let's say you want to add a new feature, instead of a ball moving in the game, I want to replace it with something else that is constructed differently than your Ball. You would have to erase all the variables and methods that is part of the Ball in your PongGame and that would be a pain to do. 


If you wrote a new class called Ball. Chances you only need to erase this line of code:


Ball ball = new Ball();



So it is important to think long term about re-usability, maintainability and adding new features.


And the issue is not only about the Ball. Your game can be restructure like this:







Another important thing is to avoid using magic numbers.

An example from your code is this:


You may know what this is now: But what about one year from now?


(width/2)-150, (height/2)-40 


You want to assign the number 150 to a variable and write what the variable is used for.

Same thing for 40. It seems to be an offset value to me but to what exactly

the offset value is suppose to do I would not know because I cannot read your mind.

With clear variable assigned to the number, this offset value is much more clear about what it is.


an example that is much more clear would be:

int stringOffsetX = 150;

int stringOffsetY = 40;


(width/2) - stringOffsetX, (height/2)-stringOffsetY;


this looks way better, right?


I hope these ideas help you to restructure your codebase so the code can be reusable, maintained and adding new features or removing current features are a breeze.

Edited by warnexus, 17 April 2013 - 02:01 PM.


#22 timgranstrom   Members   -  Reputation: 472


Posted 17 April 2013 - 02:00 PM

My apologies! I forgot to edit the topic update, here is a link to the new version of the game!




I do understand your reasoning though, and just realised that I've got a few magic numbers left, such as the string code!


I would love it if you maybe could review the new code!


Thank you :)

#23 rip-off   Moderators   -  Reputation: 7618


Posted 17 April 2013 - 04:14 PM

Please keep using your journal, but only use the forums for specific questions.


We have a new trend of posting "code review" requests, you may create threads for that.

Edited by rip-off, 17 April 2013 - 04:16 PM.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.