1

I've read a lot of articles about "public vs getter/setter", but I still wonder if there is any good part about public variable.

Or the question is:

If you're going to make a new awesome programming languange, are you still going to support public variable and why??

chrisyue
  • 187
  • 9

4 Answers4

1

A public variable essentially means you have a global accessible/changeable variable within the scope of an object. Is there really a use case for this?

Take this example: you have a class DatabaseQueryHandler which has a variable databaseAccessor. Under what circumstances would you want this variable to be:

  1. Publicly accessible (i.e. gettable)
  2. Publicly settable

Option #1 I can think of a few - you may want to get the last insert ID after an insert operation, you may want to check any errors the last query generated, commit or rollback transactions, etc., and it might make more logical sense to have these methods written in the class DatabaseAccessor than DatabaseQueryHandler.

Option #2 is less desirable, especially if you are doing OOP and abiding by SOLID principles, in particular regards to the ISP and DIP principles. In that case, when would you want to set the variable databaseAccessor in DatabaseQueryHandler? Probably on construction only, and never at any time after that. You probably also want it type-hinted at the interface level as well, so that you can code to interfaces. Also, why would you need an arbitrary object to be able to alter the database accessor? What happens if Foo changes the variable DatabaseQueryHandler->databaseAccessor to be NULL and then Bar tries to call DatabaseQueryHandler->databaseAccessor->beginTransaction()?

I'm just giving one example here, and it is by no means bullet proof. I program in PHP (dodges the hurled rotten fruit) and take OOP and SOLID very seriously given the looseness of the language. I'm sure there will be arguments on both sides of the fence, but I would say that if you're considering using a public class variable, instead consider what actually needs to access it, and how that variable is to be used. In most cases the functionality can be exposed via public methods without allowing unexpected alteration of the variable type.

e_i_pi
  • 3,855
  • 3
  • 24
  • 41
1

I agree with almost everything that's been said by everyone else, but wanted to add this:

Public isn't automatically bad. Public is bad if you're writing an Object Class. Data Classes are just fine. There's nothing wrong with this class:

public class CommentRecord
{
    public int id;
    public string comment;
}

... why? Because the class isn't using the variables for anything. It's just a data object - it's meant to be just a simple data repository.

But there's absolutely something wrong with this class:

public class CommentRecord
{
    public int id;
    public string comment;
    public void UpdateInSQL()
    {
        // code to update the SQL table for the row with commentID = this.id
        // and set its UserComment column to this.comment
    }
}

... why is this bad? Because it's not a data class. It's a class that actually does stuff with its variables - and because of that, making them public forces the person using the class to know the internals of the class. The person using it needs to know "If I want to update the comment, I have to change the public variable, but not change the id, then call the UpdateInSQL() method." Worse, if they screw up, they use the class in a way it wasn't intended and in a way that'll cause unforseen consequences down the line!

If you want to get some more info on this, take a look at Clean Code by Robert Martin, Chapter 6, on "Data/Object Anti-Symmetry"

Kevin
  • 2,041
  • 7
  • 20
0

Simple answer is: yes, they are bad. There are many reasons to that like coupling and unmaintanable code. In practice you should not use them. In OOP the public variable alternative is Singleton, which is considered a bad pracitce. Check out here.

Izbassar Tolegen
  • 1,714
  • 1
  • 14
  • 31
0

It has a lot to do with encapsulation. You don't want your variable to be accessed anyhow. Other languages like iOS (objective-c) use properties:

@property (nonatomic, strong) NSArray* array;

then the compiler will generate the instance variable with it's getter and setter implicitly. In this case there is no need to use a variable (though other developers still prefer to use variables). You can then make this property public by declaring it in the .h file or private by declaring it in the .m file.