This one is bothering me for a while now 🙂
Suppose we have a variable – why is writing a Set function better practice than simply modifying variable’s data (and setting the variable to be public instead of private)?
It’s less coding that way and I can’t see any “security” issues.
Sometimes when setting a variable, you may want to do something else with the given value other than instantly place it in the class’s variable: for instance, you may want to validate it or update another value that is related.
Basically, it lets the class which owns that variable control what can be done to it, and the specific series of events that occur when it is altered.