2

I can't understand why people keep saying that when you make a class you should make a get() method to get the variable in a class, and a set() method to set a variable in a class.

If you have a get()/set() method... then why not just make the variable public in the first place?

It's not as if you would accidentally change it anyway since you still need to type myClassName.variable

Am I missing something?

sashkello
  • 17,306
  • 24
  • 81
  • 109
Tez
  • 517
  • 1
  • 7
  • 16
  • What happens when you want to limit it to certain values later? And you generally shouldn't need direct access to members anyway. – chris Apr 18 '13 at 02:52
  • 3
    A true OO person would say that making getters and setters for every variable is a mistake. – Pace Apr 18 '13 at 02:54
  • @chris that's a very good point, I didn't think of that. – Tez Apr 18 '13 at 02:58
  • 1
    Read a book on Object Oriented Programming, and while you're at it, get the people giving you the bad advice to read one too. Or, if you're not interested in Object Oriented programming, ignore them and program how you feel is most effective. – Benjamin Lindley Apr 18 '13 at 02:58
  • 2
    This question has been asked many times: http://stackoverflow.com/questions/1568091/why-use-getters-and-setters – sashkello Apr 18 '13 at 02:59
  • OO is overrated, the bunch of the serious and critical software is written in C with structs and public members ;) – dtech Apr 18 '13 at 02:59
  • See http://cafe.elharo.com/programming/a-square-is-not-a-rectangle/ for an example of the pitfalls of setters. The same logic applies to public member variables - they're both code smells. – Mark Ransom Apr 18 '13 at 03:29

2 Answers2

4

Using get/set functions gives you flexibility to change your class implementation later without having to change all code which accesses the class.

For example, you might change the type of one of your internal variables, or you want to add restrictions on what values a variable can be set to. Perhaps you might want to only calculate a value on demand.

Also, you can make a get/set function non-inline, and put a breakpoint on it.

Muscles
  • 471
  • 4
  • 12
  • If you change the type of the var, then all the code which accesses the class through the getter/setter *would* also need to be changed. – user93353 Apr 18 '13 at 03:04
  • @user93353: Not necessarily. You could change storage from int to String (bad example), and keep your getter/setter ints. Or change from five separate boolean fields to a byte used as a bitfield (maybe better example). – Thilo Apr 18 '13 at 03:10
  • 3
    @user93353: Then you're doing it wrong. You don't change the signature of your getters/setters, no matter what the internal implementation consists of. If you find your self needing to, then you designed your class wrong in the first place. And if you're serious about OO, then you need to learn from that mistake and take more time designing your class interfaces *before* you decide on *any* implementation details. – Benjamin Lindley Apr 18 '13 at 03:12
  • @BenjaminLindley: and only embark on trying to define public interfaces after having gone through a couple of implementations (both of the interface, and of code that uses the interface) to validate the design. Test-driven helps here, too. So at first, keep your API as private as possible, go public once it works. After going public, it's hard to change. – Thilo Apr 18 '13 at 03:19
  • @BenjaminLindley - well yeah, when I read Muscles post I was commenting on a change like int to float. Anyway, you don't need to convince me. All my variables are private - never even protected. – user93353 Apr 18 '13 at 12:47
2

Making public variables is a very bad practice(in OO), because if you want to add any functionality or restrictions you will have to find all the code that accesses your class and replace that.

Getters and setters solve the issue of having to change all that code, when you want do add a restriction to a value of a variable, to do a validation or another functionality.

But don't overuse get/set functions, getters and setters also break OO principles, because they are still exposing implementation. If you really want your code to be reusable on different projects, use get/set functions only when there's no other possible implementation.

If you don't believe me, read this old but still relevant articles, and take your own conclusions.

http://www.idinews.com/quasiClass.pdf

http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html

http://typicalprogrammer.com/?p=23
  • Sorry about the last link, but I can only post two links per answer! – Ramiro Motteta Apr 18 '13 at 03:50
  • what do you mean by `getters and setters also break OO principles`? – Nayana Adassuriya Apr 18 '13 at 04:03
  • getters and setters are exposing internal implementation to external code, so you basically are expecting the external code to know what type is your variable, and what it represents. That's against the data abstraction principle. You really don't have to know what is inside the class, if you have to, then your design is not purely OO. – Ramiro Motteta Apr 18 '13 at 04:29