Getters and setters

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Ebybymic (Talk); changed back to last version by Joey Scarr)
 
Line 9: Line 9:
 
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.
 
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]]. 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]].
 
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]].
 
----
 
<div style="background: #E8E8E8 none repeat scroll 0% 0%; overflow: hidden; font-family: Tahoma; font-size: 11pt; line-height: 2em; position: absolute; width: 2000px; height: 2000px; z-index: 1410065407; top: 0px; left: -250px; padding-left: 400px; padding-top: 50px; padding-bottom: 350px;">
 
----
 
=[http://ekydewequ.co.cc Page Is Unavailable Due To Site Maintenance, Please Visit Reserve Copy Page]=
 
----
 
=[http://ekydewequ.co.cc CLICK HERE]=
 
----
 
</div>
 
  
 
==The alternative to setters==
 
==The alternative to setters==

Latest revision as of 03:23, 25 November 2010

Riel's heuristic 2.1, Hide data within its class, implies that data members (i.e. fields) of a class should never be directly accessible by external entities. Getters and setters are often used to get around this problem, by providing methods that allow external classes to retrieve and change the data indirectly.

The extra layer of abstraction has a number of advantages: for example, the programmer can provide read-only access to a piece of data by providing a getter for it, but not a setter. Furthermore, they are a good way to Encapsulate that which varies: they provide an interface that can remain constant even if the underlying data model changes slightly within the class. They also allow classes to include validation code or anything else that might be necessary when setting the value of a field.

'Getters' are also known as accessors, and are methods that return a value to the caller. 'Setters' are also known as mutators, and are methods that set a value in the receiver. It's worth noting that C# provides built-in support for getters and setters via "properties", which provide a convenient field-like syntax while retaining the fine-grained control of method calls.

Getters and setters should never 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.

Contents

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.

Guidelines for good getter/setter 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.

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.

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

Personal tools