Difference between setting variables through methods and setting actual variable [closed]

StackOverflow https://stackoverflow.com/questions/19072922

  •  29-06-2022
  •  | 
  •  

Question

I'm not experienced in programming, but I was wondering what the difference was between setting a variable through a method.

Ex:

void setShowFPS(boolean b){
    this.showFps = b;
}

vs. just calling the variable

showFps = false;
Was it helpful?

Solution

Typically, it is good practice for your class variables to be private, which means they can't be set except by methods and constructors of the class the variables belong to. The advantage of using methods to set variables (these methods are referred to as setters) is that you can provide validation logic within the method.

For example, if you have a variable within your class that is supposed to represent the hour of the day, you will only want it to hold a value 0 to 23. Your setter can throw an exception any time the user of the method attempts to set the variable to anything outside this range.

OTHER TIPS

Generally it is best to not leave your variables exposed to users outside the class. A method offers you more control - for example, if you would like to trigger an event when your Boolean variable gets set to true, you can do it in your method, but you cannot do it when the variable is set:

void setShowFPS(boolean b) {
    this.showFps = b;
    // Trigger the event
    if (b) {
        OnFpsSet();
    }
}

In addition, C# offers you properties that give you as much control and encapsulation as the method does, and they look like variables on the outside. It is more idiomatic to use properties instead of single-argument setter methods:

bool _ showFps;
bool showFps {
    get {
       return _showFps;
    }
    set {
       _showFps = value;
    }
}
void setShowFPS(boolean b){ this.showFps = b; }

This is a relatively simple procedure. Imagine you had a field of type double, for instance, where it had to have an odd integer portion. You'd set showFps to be private, and then access it through a direct getter and a validating setter(shown here in Java syntax):

void setF(double f_) throws IllegalArgumentException { 
    int ip=(int) f;
    if(ip%2!=1) throw new IllegalArgumentException();
    this.f = f_; 
}

Setters can do things like updating other fields indirectly affected(such as a last-change timestamp, for instance). Anyway, if you ever had to update the internal code to use a BigInteger, for instance, the ability to get and set via doubles externally keeps users of your class from breaking on your changes.

C# would use properties for this, possibly.

I assume you mean why encapsulate a private variable?

There are several reasons:

  1. Validation logic - with an explicit setter, you can control the valid range of values the field can be set to. If someone tries to set an invalid value, you can throw an exception.

  2. Hide how an object implements its contract. This lets you change the way your class works later without breaking other code that relied on your implementation.

  3. Perform several updates atomically. More complicated operations might not be able to all be done "at the same time". By using synchronization and locking mechanisms, you can allow safe publication in a multi-threaded environment.

its all based on encapsulation and abstraction concept : combining/binding data and code together so that your code is not altered accidentally. making the variables private/inaccessible to outside directly can prevent the the accidental modification of the data(inturn access the data by providing getters/setters/methods). NOTE : object oriented programming is oriented more on data than on the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top