views:

187

answers:

5

Exactly what the topic title says,

In which cases would you prefer using public functions to change local variables over just defining that variable as public and modifying it directly?

A: 

If you want to change a variable inside a class, your best doing it through Properties.

Its not good practice to have variable's modified on the outside.

Think of future development too. You could put some logic behind a Property without changing the whole program.

Sir Psycho
+2  A: 

Obviously if you want changing the variable to have some other effect on the object's state (like recalculating some other property of the object) you must use a mutator function.

If it's possible to set the variable to something that places the object in an invalid state, you should probably also use a mutator function. This way you can throw an exception (or return an error, or just ignore) if something illegal is about to happen. This does wonders for debugging.

But if some variables can be modified with mutator functions, and others are public, the programmer needs to keep track of which is which. This is a waste of time and effort so in some cases it's easiest to just use mutator functions for everything.

Artelius
+4  A: 

Don't expose the data members directly: using opaque accessors means you can change the implementation at a later date without changing the interface.


I should know. I take the short cut from time-to-time, and have had occasion to regret it.

dmckee
+1  A: 

If you look at an object purely in term of service, you realize that exposing a variable is not a good way to expose those services.

The API must reflect what the object is all about (for it to achieve a high cohesiveness), and if you define a setValue(...), it is not so much because you need a way to -- today -- changes a variable, but because it makes sense for the object to expose this service.

So:

  1. Don't provide accessors or mutator function to every single member of every single class you write. Only provide accessors/mutator functions if the accessors/mutator methods are a sensible and useful part of the class's interface (API).

  2. Don't think of these methods as accessors or mutators. Instead, think of them as methods that access or mutate a certain abstract property of the object that happens to be represented by a single member today, but may be computed in a more complex manner tomorrow.

VonC
+1  A: 

You should mention what language you are dealing with, since that will affect the answer.

Your first thought should be about the API to your class. If you want to keep that API stable (and you should!), then consider how you might change today's simple variable into a full-blown method later.

In many languages, you can't change a variable to a method without changing the calling code. C, C++, and Java fall into this category. Never use public variables in these languages, because you won't have any wiggle room later.

In Python, you can change a variable to a property without changing the callers, so you don't have to worry up front: use public variables.

C# I believe has properties that can let you change variables to methods transparently, but I am not sure.

Ned Batchelder