Reputation: 564
I am writing a custom physics engine for a game that I am making, my physics object class has tons of variables (distance, velocity, acceleration, mass, gravity, force, impulse duration etc....). Will creating a setter and getter function for each of these variables impact performance? (There will be at least 100 instances of this class at a given time)
Also should I create setters and getters? I heard public variables are really bad practice but there are a lot of variables, can this be an exception?
Upvotes: 8
Views: 3603
Reputation: 1618
simply public variable violates OO design principles, but getter setter does not, That why we need to the user always getter and setter
Upvotes: 0
Reputation: 27365
Will creating a setter and getter function for each of these variables impact performance? (There will be at least 100 instances of this class at a given time)
Probably no. Theoretically yes, but in practice, the cost of calling an extra function to get a particular value is negligible. The only way it will impact performance is if you end up calling these methods all the time (say ... 50000 times per second).
Also should I create setters and getters?
Probably no. Good OO design follows a guideline called "tell, don't ask". Usually you should see what operations you need these variables for, then either implement those operations in the class, or implement them in a class that has access, or use a different model (visitor pattern comes to mind).
I heard public variables are really bad practice but there are a lot of variables, can this be an exception?
Having public variables is not bad practice. Having public variables when you have invariants on them, is bad practice.
For example, if you have a variable measuring the weight of an object, you will want to make sure this cannot be set to an invalid value (such as a negative amount, or a ridiculously large amount). If you make the variable public, you will either have to check the value you set everywhere in client code where you modify it, or give up on validating that value.
Both are bad, as they are errors that couldn't exist if you had a propper setter, with validation.
In short, having public variables is only acceptable if you have no invariants on them.
Considerations:
use public variables, if setting any value permitted by the variable type is OK, at any point (you have no invariants)
use private variables if you have invariants.
use public methods to define operations, not "access to internal variables".
design your public API in terms of operations you want to perform when you look at it from the client code, not in terms of variables present in the internal implementation.
In this context, getters and setters make sense some times (rarely), but not because you have the variables in the class.
Upvotes: 11
Reputation: 1
Will creating a setter and getter function for each of these variables impact performance?
As for simple getter/setter functions, they can be usually inlined by the compiler so there won't be a performance impact.
Also should I create setters and getters? I heard public variables are really bad practice
Public variables aren't inherently a bad practice, though usually you want to encapsulate your data in a class, especially if there are calulations applied on them.
Classes which only provide getter/setter functions are useless, these can usually simply replaced with a struct
with all public variables.
Upvotes: 9