Jump to content

Is it bad practice to create public variables instead of using setters/getters?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Since the contest started I stopped posting weekly (or bi-weekly, sometimes monthly) discussion threads. I figure I'll start again with this question:

Do you think it is bad practice to create public variables instead of using setter/getter methods? Why? I'm referring to a class and how the variables of that class are set:

Public Variable
class someClass {
public variable;
}

localClass = new someClass();
localClass->variable = "CodeCall";

Setter/Getter
class someClass {
    private variable;
    function setVariable(value) {
       this->variable = value;
   }
   function getVariable(value) {
       return this->variable;
   }
}

localClass = new someClass();
localClass->setVariable("CodeCall");

What do you think of the C# ability to auto-implement setter/getters:
[highlight=c#]
public class Point {
public int x { get; set; }
}[/highlight]

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I think it depends a LOT on the type of data. Anything that requires validation should use getter/setters. Anything that requires synchronization with other values should use getters/setters.

On the other hand, if something really has no guard/control characteristics, it's probably not a big deal. I've seen GUI libraries that take both views on public member variables, and the issue is really about solid design.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
I would also look at how your class is going to be used. If it's a program to automate something like personal use or a one time only or even something that wont need to be edited later, why code the extra functions?

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
It's amazing how much of my "one shot, single purpose" code has been reused in other programs.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
technica

technica

    Learning Programmer

  • Members
  • PipPipPip
  • 63 posts
One thing that we learn in schools is lot of public variables can waste lot of memory.

It nearly depends on the requirement and the actual scope of use of a certain variable. It is always suggested that variables inside a function or procedure should be private, instead of declaring them as public at beginning of the project.
Also it is also advised to declare variables only when they are required.

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Private variables make it much easier to change the internal logic.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
I think variables should be considered private in OOP but I like Python's (and other languages, but I'm such a Python fan :P ) accessors(controlled set and get) and also its ability to simulate numerical data types by using methods like __add__ and __div__, you can do arithmetic with custom vectors, or with just any class you like.
Such a practice can supress the need of accessors

#8
Natrobius

Natrobius

    Programmer

  • Members
  • PipPipPipPip
  • 166 posts
It is a better practice to assume that the code will be used in other projects. It is also a better practice to implement checks into everything to prevent runtime errors. So, in general I support the use of getters and setters.