Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What good are public variables then?

I'm a total newbie with tons of ?'s in my mind and a lot to experience with C++ yet! There's been something which I find really confusing and it's the use of public variables, I've seen tons of code like this:

class Foo {  private:     int m_somePrivateVar;  public:     void setThatPrivateVar (int const & new_val) {         m_somePrivateVar = new_val;     }      int getThatPrivateVar (void) const {         return m_somePrivateVar;     }  }; 

Why would anyone hide that variable and implement accessors and mutators when there's nothing done in them more than assigning the new value just as it got received (no range checking etc.) or returning the value without just as it is? Well I've heard some reasons and some of them are convincing in some cases, but imagine implementing a huge class in such a manner for with a lot of variables which do not need any checking and stuff! Let me ask you this way, When do you use public variables? Do you use that at all?

like image 442
Parsa Jamshidi Avatar asked Mar 02 '11 14:03

Parsa Jamshidi


People also ask

When should you use public variables?

You can use public variables as a shortcut to avoid the need to provide getters and setters, but there is no need to do so, and because there is a negative consequence (increased coupling to the internal design of the class) I would suggest that sacrificing design for brevity in this way is not often a good idea.

What does a public variable do?

“Public Variables” in VBA, as the name suggests, are variables that are declared to use publicly for all the macros we write in the same module as well as in different modules as well. So, when the variables are declared at the start of any macro are called “Public Variables” or “Global Variables.”

Why would you make a variable public?

Why? because it's more easy to code (everything is a member function), also you have more control on the access of that variable, and what Doug T. and the others said about functional abstraction.

Should class variables be public?

7.1. Considerations Regarding Access RightsNever specify public or protected member data in a class. The use of public variables is discouraged for the following reasons: A public variable represents a violation of one of the basic principles of object-oriented programming, namely, encapsulation of data.

Is the use of public variables a bad practice?

From sites, videos, to books, I've read that the use of public variables is a bad practice, but from what I'm getting from this video its saying otherwise. In the video he's uses a struct which by default has a access modifier of "public" vs a class which has a default access of "private".

Is it bad to make a string variable public?

Firstly you state it wrong. its bad to make your variable public i.e: public String name = null; this is bad. You should always do it as private String name = null; To understand why, you need to dig a bit into the ideology of OOPs OPPS ideology states that each object of your class will have 2 things:

Are public variables overused in Python?

I think it's a little bit overused, some cases public variables are acceptable, but for big classes it's problematic because when somebody want to get some information, he will just search for the Getters and won't check if there are any public variable... so my advice: Be careful with it, but rarely public variables are okay. – Melkon

When to use public functions instead of constructor?

What works well is to use constructorsto set up the state of an object, then use public functions to retrieve values of member variables, but only if that's needed. If you need to mutate the object after construction, then provide very specific methods for that purpose.


2 Answers

By hiding the variable and adding methods now, the class designer allows for inserting arbitrary code into those methods in the future without breaking tons of code that use the attributes directly.

Also note that providing a lot of accessor/mutator methods is generally a sign that your class design needs another look for possible improvement. Class methods should implement actual logic, not just provide access to each member.

I use public variables only in struct form. For example, I might have a database table that represents a string->value mapping, where value is a composite data structure. I'd just write a structure and use for example std::map<std::string, MyStruct> to represent the database table. I don't need to actually do work on the data, merely be able to look it up and make use of it when required.

As noted in a couple comments, even structs can often benefit from judicial use of methods, for example a couple of common constructors to keep the members sanely initialized, a clear function to reuse the structure, etc.

like image 62
Mark B Avatar answered Sep 30 '22 22:09

Mark B


IMO the most compelling reason for setters/getters is isolating change. If you need to add range checking, for example, if you already have a setter, you can easily do that in your setter without impacting client code. If you don't already have a setter, then all client code needs to be updated to use getters/setters which could be a nightmare.

like image 40
Doug T. Avatar answered Sep 30 '22 22:09

Doug T.