Should we use all variables as private?

8
  • Should we always use the attributes of a class as private?
  • What do private variables help prevent?
  • How do you decide whether a particular property should be private or not? CASE, by default, each field is private, so why are there public data members in a class?
  • Under what circumstances should a variable be made public?

Searching the internet, numerous "tutorials" are found, speaking as defined as a private attribute, but does not give an attribute usage scope when you should use one and not another.

The best definition I found was in SO :

  

Private :   The only class that has access to the attribute is the class itself that defines it, that is, if a Pessoa class declares a private attribute called nome , only the Pessoa class will have access to it.

    
asked by anonymous 04.05.2017 / 03:44

1 answer

14

What you usually read are so-called good practices. The person says what you should do without saying why, often because the person does not even know why. It is common for a person to be just repeating what he "learned" without questioning why.

  

Should we always use the attributes of a class as private?

I do not think so, there are people who think so. It depends on a number of circumstances and the language you are using, not to mention the philosophy you have adopted.

  

What do private variables help prevent?

Prevent other classes from accessing that variable directly, as you already know.

  

How do you decide whether a particular property should be private or not? CASE, by default, each field is private, so why are there public data members in a class?

Each has a criterion. Some people decide that they will all be private. Some decide to make public only those that need maximum performance. Others choose to make public all variables that only access their value in a simple way without needing processing. Others look at the domain as a whole to make a decision.

Again this all depends on the language you are using, the accessory technology that may require a certain form, the philosophy adopted, the maintenance requirements, how the class will be consumed, just to name the main ones.

Each has maintenance implications if it ever needs to change. Everything public is part of the API, changing that can be traumatic. But it depends. If you have full control over the application this is not so difficult if you have the right tools.

You have cases that you want the variable to really be just a detail of implementation, there it has to be private, everything that is public is part of the contract that you need to spice up in most situations, but not all as some people think . You need to be pragmatic.

  

Under what circumstances should a variable be made public?

Whenever you find that the private is not suitable, within the parameters already passed above.

You have a ask more information and links to dig deeper into the subject.

Add-on after editing question

Now you've taken the of I would say that you do not need to use private variables, at least if you have a getter and setter that do nothing but return the value or assign the passed value. Remember PHP is a scripting language.

On the other hand the gain of using the public variable is very small in PHP . The class variables in PHP are the same, there are no optimizations. I still prefer not to put lines of code that I do not even know will be useful.

Note that using magic methods is different from using getXXX() and setXXX() methods since you will have to change the consumer code if you need to include some processing in accessing these variables. But reinforcing that you are doing a script , this is quiet. Are you using PHP to do scripts , right?

Of course, if the variable is genuinely only to be accessed by the class, if it is not creating an artificial encapsulation then the use of the private variable is interesting.

Private variables help achieve cohesion and avoid unintentional coupling .

    
04.05.2017 / 04:10