Private vs public

Started by
10 comments, last by Zoner 13 years, 2 months ago

[quote name='pcgamer' timestamp='1296548144' post='4767864']
I realise the need for private variables in case we're working as a team. But even if it is a large project, but which I'm doing on my own, would it still be as important as the first?
Yes. Code that you wrote months (or weeks) ago may as well have been written my someone else.
...........
[/quote]

Wow! Thank you for the very helpful post!
Your examples about the health made understand what you meant. By following your way we can know for sure when the variable is being changed and act on it rather than having for example to make a thread to check if that variable has changed.
I'm thinking like you said, that if properly designed, there is still no need to explicitly put the variables as private. But I guess doing so ensures that they're being used the way they're being used. (I knew that before, but I never really quite believed it)
I had a problem with health in a small text-based game I made as a small project some time ago. Had I done this it would have saved me a lot of trouble! :o
Thank you again for you're help!


To explain that in an easy sentence, you should avoid setting variables on objects, and instead TELL YOUR OBJECTS WHAT TO DO.
...................
So you have a room full of game objects, and you throw a giant fireball in there. All you have to do is call the TakeDamage function on all the objects, and now they all act accordingly. The enemies all burn up for massive damage and kick the bucket. The destructible props all burst into flames. The random passerby NPCs all take major damage and die. The plot NPCs either don't take damage, or take damage but never go blow 1 HP. The other players in the area may or may not take damage depending on any number of variables (no friendly fire, no friendly fire on team mates, anything goes)....
.................


AOE spells... Still very far away from getting there, but when I will definitely remember your post :lol:



Thank you everyone else also for your posts. You helped me gain further grasp of things, and now I don't feel like I should put private just because I have to! :P
I will read the material given to me and see what I can get out of it.
Advertisement
If you care about future maintenance, using functions instead of directly accessing fields makes re-factoring much much easier.
http://www.gearboxsoftware.com/

This topic is closed to new replies.

Advertisement