Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are public variables faster than using getters and setters?

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?

like image 937
RainingComputers Avatar asked May 26 '17 12:05

RainingComputers


People also ask

Why are getters and setters better than public?

The main difference between making a field public vs. exposing it through getters/setters is holding control over the property. If you make a field public, it means you provide direct access to the caller. Then, the caller can do anything with your field, either knowingly or unknowingly.

Do you need getters and setters for public variables?

You don't NEED them, but many say you should use them anyway (or rather, they disdain the use of public variables in the first place).

Should getters and setters be public or private?

Usually you want setters/getters to be public, because that's what they are for: giving access to data, you don't want to give others direct access to because you don't want them to mess with your implementation dependent details - that's what encapsulation is about.

Do getters and setters speed up compilation?

Getters and setters can speed up compilation. Getters and setters provide encapsulation of behavior. Getters and setters provide a debugging point for when a property changes at runtime.


1 Answers

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.

  • getters and setters are a symptom of trying to solve a problem in the wrong place: if you have an operation X in class A that uses variables from class B, then you should probably define operation X in class B, then call it from class A.
like image 64
utnapistim Avatar answered Sep 30 '22 17:09

utnapistim