Getters and setters
(→The alternative to setters) |
|||
Line 1: | Line 1: | ||
<Long tedious background explanation of getters & setters goes here> | <Long tedious background explanation of getters & setters goes here> | ||
− | * A 'Getter' also known as ''accessors'' are methods that return a | + | * A 'Getter' also known as ''accessors'' are methods that return a value to the caller. |
− | + | * A 'Setter' also known as ''mutators'' are methods that set a value in the receiver. | |
− | * A 'Setter' also known as ''mutators'' are methods | + | |
The two should not be combined as this creates unexpected side effects for the unsuspecting developer. This idea is called [[Command query separation]]. | The two should not be combined as this creates unexpected side effects for the unsuspecting developer. This idea is called [[Command query separation]]. | ||
− | Using setters rather than having direct access to fields is good practice. It allows the class to maintain the integrity of the data | + | Using setters rather than having direct access to fields is good practice. It allows the class to maintain the integrity of the data by enforcing constraints, if any, on exactly what values can be set. |
====The problem with getters==== | ====The problem with getters==== | ||
− | Using getters can be useful for [[Ken Auer 1995|Reusability through self-encapsulation]]. | + | Using getters can be useful for [[Ken Auer 1995|Reusability through self-encapsulation]]. However, it can be misused very easily. The purpose of a getter is to retrieve information. If getters are mostly being used from outside the class to get access to member variables one should ask why is [[Keep related data and behavior in one place|data and functionality not in the same place]]. It may be that you are getting out values to do work that the class should be doing for itself. This sometimes leads to breaking the [[Law of Demeter]]. In cases like this it is better to [[Tell, don't ask]]. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
====The alternative to setters==== | ====The alternative to setters==== | ||
− | There is an | + | There is an alternative to setters: ''[[Immutable object|immutable objects]]''. Many programmers will already be familiar with this concept since Java, C# and other languages make strings immutable. The value of an immutable object is set at creation and cannot be changed. This guarantees that there are no side effects. Many concepts will not work without the concept of immutable objects such as [[flyweight]] and the "canonical representation" mechanism for strings in Java. |
'''Rule for good design, version 1:''' | '''Rule for good design, version 1:''' | ||
Line 27: | Line 22: | ||
* Elsewhere, use the getters & setters. | * Elsewhere, use the getters & setters. | ||
− | Simple. | + | Simple. Except we just killed [[Encapsulation]]. |
− | + | ||
− | Except we just killed [[Encapsulation]]. | + | |
'''Rule for good design, version 2:''' | '''Rule for good design, version 2:''' | ||
− | * As above, but only add the getters | + | * As above, but only add the getters and setters as necessary. |
− | If the type or name of foo changes, | + | If the type or name of foo changes, we can keep the methods the same and avoid the propagation of this change. By doing this we are [[Information hiding|hiding information]] and [[Encapsulate that which varies|encapsulating change]]. |
− | + | '''Rule for good design, version 3:''' | |
+ | * As above, but always use the getters & setters, even within the object containing the field. | ||
− | + | This suggests that the only methods allowed to touch the field directly are the getters & setters themselves. | |
− | + | ||
− | + | ||
− | + | ||
This idea is explained by [[Ken Auer 1995]]. But first, it helps to realise there is more than one [[Encapsulation boundary]] behind which we might hide fields. | This idea is explained by [[Ken Auer 1995]]. But first, it helps to realise there is more than one [[Encapsulation boundary]] behind which we might hide fields. |
Revision as of 03:17, 27 July 2009
<Long tedious background explanation of getters & setters goes here>
- A 'Getter' also known as accessors are methods that return a value to the caller.
- A 'Setter' also known as mutators are methods that set a value in the receiver.
The two should not be combined as this creates unexpected side effects for the unsuspecting developer. This idea is called Command query separation.
Using setters rather than having direct access to fields is good practice. It allows the class to maintain the integrity of the data by enforcing constraints, if any, on exactly what values can be set.
The problem with getters
Using getters can be useful for Reusability through self-encapsulation. However, it can be misused very easily. The purpose of a getter is to retrieve information. If getters are mostly being used from outside the class to get access to member variables one should ask why is data and functionality not in the same place. It may be that you are getting out values to do work that the class should be doing for itself. This sometimes leads to breaking the Law of Demeter. In cases like this it is better to Tell, don't ask.
The alternative to setters
There is an alternative to setters: immutable objects. Many programmers will already be familiar with this concept since Java, C# and other languages make strings immutable. The value of an immutable object is set at creation and cannot be changed. This guarantees that there are no side effects. Many concepts will not work without the concept of immutable objects such as flyweight and the "canonical representation" mechanism for strings in Java.
Rule for good design, version 1:
- Whenever you declare a field, foo, make it private.
- Add a public getter called getFoo(), or isFoo() for booleans.
- Make the return type of the getter the same as the field.
- Add a public setter called setFoo() & pass a parameter of foo's type.
- Inside the object, access the field directly.
- Elsewhere, use the getters & setters.
Simple. Except we just killed Encapsulation.
Rule for good design, version 2:
- As above, but only add the getters and setters as necessary.
If the type or name of foo changes, we can keep the methods the same and avoid the propagation of this change. By doing this we are hiding information and encapsulating change.
Rule for good design, version 3:
- As above, but always use the getters & setters, even within the object containing the field.
This suggests that the only methods allowed to touch the field directly are the getters & setters themselves.
This idea is explained by Ken Auer 1995. But first, it helps to realise there is more than one Encapsulation boundary behind which we might hide fields.
See Also
- Riel's heuristics 9.2 Do not change the state of an object without going through its public interface.