Do I need accessors and mutators for every class variable?

Started by
18 comments, last by StoneMask 11 years, 7 months ago
I myself like to make setters and getters but the way from MichaBen:
Personally I try to avoid setters that refer to a specific variable, as that would mean the object who calls it has to know how this class works internally. Instead, I prefer to have functions that invoke a specific state change or give me the information I need.[/quote]
is really a great way.
You should know your class and focus it only on the specific tasks.
But sometimes there is no time or you have a lazy phase (never ever listen to this phase, please!), then can you make your class properties (variables) public, but only if it is something like a data storage (old-fashioned structs).
Example:

class A
{
/*Very much variables*/
int Variable1;
int Variable2;
int Variable3;
...
}
/*You want to return more than one variable, therefore use a class.*/
A foo(int i)
{
A a;
a.Variable1 = sin(i*PI);
a.Variable2 = cos(i*PI);
a.Variable3 = sin(i*PI)*cos(i*PI);
/*And so on...*/
return a;
}

Class "A" will be never used somewhere else.

This above looks sometimes useful and time saving, right?

Forget it! It is a mess. You can never be really sure that class "A" will not be used somewhere else!
This example above is a design mistake. Give yourself time and make some design. Use setter/getter or better real members (like these from MichaBen).
You will be in the safer site and will not have a headache or acute laziness later.

This failure happened often to me years ago.
Of couse the example is not wrong, but for me it's just disgusting. Use it if you like it. smile.png I do not.
Reading, Reading, Reading... why do you read not more?
I have a blog: omercan1993.wordpress.com look there for more content
And I also do some art: omercan1993.deviantart.com
And whats about the Pear3DEngine? Never heard of it? Go and look!
Yeah, and currently I do this: SimuWorld

PS: Please look at this poll on my blog about scripting languages: A opinion poll about scripting language
Advertisement

...good information...

I'd just like to point out the fact that you have too many damn points. Ignore the fact that I've upvoted you a few times already :P

Beginner in Game Development?  Read here. And read here.

 

So the short answer is no, you do not. First you determine which variables really need to be accessed by objects outside of that class. Then through some poking and prodding, you determine which variables are read-only and therefore need accessors. And which variables will need to updated and therefore need mutators. But if you are doing things like:
[source lang="csharp"]public int getScore()
{
return this.playerScore;
}

public int setScore(int points)
{
this.playerScore = points;
return this.playerScore;
}[/source]

Then just do this:
[source lang="csharp"]public int playerScore;[/source]

Beginner in Game Development?  Read here. And read here.

 

Many of the variables are being accessed by code in my main.cpp, where the program determines what to do based on what I get from the class object in terms of information.

Many of the variables are being accessed by code in my main.cpp, where the program determines what to do based on what I get from the class object in terms of information.


Thats probably your problem, you're trying to force an OO structure (a class) on a non OO design which is why you need so many accessors and mutators.

a class should have methods that do things with the class instance and possibly a few methods to inspect the state of the object.

For example, you have AddLevel and AddXP methods. atleast one of those is redundant, possibly both.

consider this:

player.attack(monster[6]);

if the monster dies the player can get the xp value from the monster directly, add it to its current XP and if necessary increase the level, no other class has to be able to do this.
Other classes might want to read the current level of the player object though (to be able to show it in for example the UI) and you could possibly want an addXP method if you need to trigger xp gains without the player doing anything. (I can't think of a single case where you'd want to do that though, xp gains are almost always the result of an action performed by the player object)

Also, OO is not the only or necessarily the best way to do things, if you prefer a different approach feel free to use it, but don't try to force OO structures on non OO code, it gets very painful very quickly. (if you just want to group data logically structs are a far better match than classes)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
In addition to what was already said, my very concise answer to your question would be: No.
I won't go into any design/architecture points because that wasn't your question, so here's a little check-list that might be worth looking at:
• is the class variable used outside the class it is defined in?
No: make it private. End of list.
• Is the class variable only used by derived classes?
Yes: make it protected
• Is the class variable only used for read access outside the class?
Yes: make it private and declare a read-only accessor. End of list
• Are there any constraints on the contents of the variable, i.e. range checks, consistency issues, etc.?
No: make the variable public. End of list.
Yes: make the variable private and declare a mutator that checks for/enforces the constraints.

While OO-purists would point out that having public class variables is a sign of bad design, I tend to think more practically. C++ is a multi-paradigm language for a reason and I personally see no point in debates over stylistics.

Oops, I just noticed that Alpha_ProgDes wrote exactly this smile.png

Also, OO is not the only or necessarily the best way to do things, if you prefer a different approach feel free to use it, but don't try to force OO structures on non OO code, it gets very painful very quickly. (if you just want to group data logically structs are a far better match than classes)

I was just thinking this and was going to post it. But since you have already mentioned, I'll just reiterate by quoting you. smile.png

Beginner in Game Development?  Read here. And read here.

 


While OO-purists would point out that having public class variables is a sign of bad design, I tend to think more practically. C++ is a multi-paradigm language for a reason and I personally see no point in debates over stylistics.

Oops, I just noticed that Alpha_ProgDes wrote exactly this Posted Image

But yours has more detail and a good point about C++.

Beginner in Game Development?  Read here. And read here.

 

If the variable needs bounds checking, validation, anything of those sorts - then yes.
So when you're saying it should be more exclusively OO or not, you're saying my functions should either take place within the class object or in the main code? And how do I return a state to the main function when the struct or class only exists in my player's struct or class? Is it a simple Player::(struct) temp? I could probably find this out on my own, but I want to cover my bases, as I don't quite know what is poor practice yet. I have, however, figured out that it's probably easier to have a collide function that checks what the player collides with, then make appropriate alterations. Like say, colliding with nothing will cause me to move forward, and colliding with a heart will increase health. Most stat alterations happen through collisions. The only exceptions would be when I buy things from a shop, which uses external code from the class. How would I handle that? Would I just add in an addHealth function anyway?

Also, since the inventory is really only one thing, followed by two gained abilities, do you think a bool array would be good? Like, position one is whether or not fast travel is enabled, position two is whether or not I have a certain ability, etc.? It seems simpler than going with a vector or something, and even though I know it's negligible in terms of memory, I'm a somewhat minimalistic person.

I have more questions, but these are the more immediate ones. I was re-reading through the responses since I've cleared my mind of all the complicated stuff and looked through everything with fresh eyes, so to speak.
Thanks for your help.

This topic is closed to new replies.

Advertisement