Jump to content

Mutator method

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Lax4mike (talk | contribs) at 19:07, 1 March 2011 (→‎Java example: clarification). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer science, a mutator method is a method used to control changes to a variable.

The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle of encapsulation. According to this principle, member variables of a class are made private to hide and protect them from other code, and can only be modified by a public member function (the mutator method), which takes the desired new value as a parameter, optionally validates it, and modifies the private member variable.

Often a "setter" is accompanied by a "getter" (also known as an accessor), which returns the value of the private member variable.

Mutator methods may also be used in non-object-oriented environments. In this case, a reference to the variable to be modified is passed to the mutator, along with the new value. In this scenario, the compiler cannot restrict code from bypassing the mutator method and changing the variable directly. The onus falls to the developers to ensure the variable is only modified through the mutator method and not modified directly.

In programming languages that support them, properties offer a convenient alternative without giving up the utility of encapsulation.

In the examples below, a fully implemented mutator method can also validate the input data or take further action such as triggering an event.

Implications

The alternative to defining mutator and accessor methods, or property blocks, is to give the instance variable some visibility other than private and access it directly from outside the objects. Much finer control of access rights can be defined using mutators and accessors. For example, a parameter may be made read-only simply by defining an accessor but not a mutator. The visibility of the two methods may be different, it is often useful for the accessor is public while the mutator remains protected, package-private or internal.

The block where the mutator is defined provides an opportunity for validation or preprocessing of incoming data. If all external access is guaranteed to come through the mutator, then these steps cannot be bypassed. For example, if a date is represented by separate private year, month and day variables, then incoming dates can be split by the setDate mutator while for consistency the same private instance variables are accessed by setYear and setMonth. In all cases month values outside of 1 - 12 can be rejected by the same code.

Accessors conversely allow for synthesis of useful data representations from internal variables while keeping their structure encapsulated and hidden from outside modules. A monetary getAmount accessor may build a string from a numeric variable with the number of decimal places defined by a hidden currency parameter.

Modern programming languages often offer the ability to generate the boilerplate for mutators and accessors in a single line—as for example C#'s public string Name { get; set; } and Ruby's attr_accessor :name. In these cases, no code blocks are created for validation, preprocessing or synthesis. These simplified accessors still retain the advantage of encapsulation over simple public instance variables, but it is common that, as system designs progress, the software is maintained and requirements change, the demands on the data become more sophisticated. Many automatic mutators and accessors eventually get replaced by separate blocks of code. The benefit of automatically creating them in the early days of the implementation is that the public interface of the class remains identical whether or not greater sophistication is added, requiring no extensive refactoring if it is.[1]

Manipulation of parameters that have mutators and accessors from inside the class where they are defined often requires some additional thought. In the early days of an implementation, when there is little or no additional code in these blocks, it makes no difference if the private instance variable is accessed directly or not. As validation, cross-validation, data integrity checks, preprocessing or other sophistication is added, subtle bugs may appear where some internal access makes use of the newer code while in other places it is bypassed.

Accessor functions are always less efficient than directly fetching or storing data fields due to the extra steps involved.[2]

Examples

Smalltalk example

  age: aNumber
     " Set the receiver age to be aNumber if is greater than 0 and less than 150 "
    (aNumber between: 0 and: 150)
       ifTrue: [ age := aNumber ]

C example

Note that it is perfectly possible to do object-oriented programming with guaranteed encapsulation in pure C.

In file student.h:

typedef struct student *student_t;
student_t student_new( int age, char *name); 
void student_set_age( student_t s, int age);
int student_get_age( student_t s);

In file student.c:

#include <student.h>
struct student { int age; char * name; };

student_t student_new( int age, char *name) {
  student_t s = malloc( sizeof( struct student));
  s->age = age; s->name = name;
  return s;
}
void student_set_age( student_t s, int age) { s->age = age; }
int student_get_age( student_t s) { return s->age; }

In file main.c:

#include <student.h>

int main() {
 student_t s = student_new(19, "Maurice");
 int old_age = student_get_age(s);
 student_set_age(s, 21);
}

C++ example

In file Point.h

#ifndef POINT_H
#define POINT_H

class Point {
private:
    int _x;
    int _y;
public:

    //Constructors
    //...

    //Getters (Accessors)
    int GetX() const;
    int GetY() const;
    
    //Setters (Mutators)
    void SetX(int x);
    void SetY(int y);

    //Methods
    //...
};
#endif

In file Point.cpp
This example optionally validates the setters.

#include "Point.h"

#include <climits>

//Constructors
//...

//Getters (Accessors)
int Point::GetX() const{
    return _x;
}
int Point::GetY() const{
    return _y;
}
//Setters (Mutators}
void Point::SetX(int x) {
    if(x < 0) x = 0;
    _x = x;
}
void Point::SetY(int y) {
    if(y < 0) y = 0;
    _y = y;
}
//Methods
//...

Python example

This example uses a Python class with one variable, a getter, and a setter.

class Student():
    # A variable to hold the students name
    _name = ""

    # A function to print the name to stdout
    def getName(self):
        print self._name

    # A function to set a new name
    def setName(self, newName):
        self._name = newName

    # Note you can also use the built-in property() function to wrap the methods
    # name = property(getName, setName)

Java example

In this example of a simple class representing a student with only the name stored, one can see the variable name is private, i.e. only visible from the Student class, and the "setter" and "getter" is public, namely the "getName()" and "setName(name)" methods.

public class Student {
    private String name;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param newName
     * the name to set
     */
    public void setName(String newName) {
        this.name = newName;
    }
}

C# example

This example illustrates the C# idea of properties, which are a special type of class member. Unlike Java, no explicit methods are defined; a public 'property' contains the logic to handle the actions. Note use of the built-in (undeclared) variable value.

public class Student 
{
    private string name;

    /// <summary>
    /// Gets or sets student's name
    /// </summary>
    public string Name 
    {
        get 
        {
            return name;
        }

	set 
        {
            name = value;
        }
    }
}

In recent C# versions, this example may be abbreviated as follows, without declaring the private variable name.

public class Student
{
    public string Name { get; set; }
}

Using the abbreviated syntax means that the underlying variable is no longer available from inside the class. As a result, the set portion of the property must be present for assignment. Access can be restricted with a set-specific access modifier.

public class Student
{
    public string Name { get; private set; }
}

VB.NET example

This example illustrates the VB.NET idea of properties, which are used in classes. Similar to C#, there is an explicit use of the Get and Set methods.

Public Class Student

    Private _name As String

    Public Property Name()
        Get
            Return _name
        End Get
        Set(ByVal value)
            _name = value
        End Set
    End Property

End Class

In VB.NET 2010, Auto Implemented properties can be utilized to create a property without having to use the Get and Set syntax. Note that a hidden variable is created by the compiler, called _name, to correspond with the Property name. Using another variable within the class named _name would result in an error. Privileged access to the underlying variable is available from within the class.

Public Class Student
    Public Property name As String
End Class

Delphi example

This is a simple class in Delphi language that illustrates the concept of public property that access a private field.

interface

type
    TStudent = class
        private
            fName: string;
            procedure SetName(const Value: String);
        public
            /// <summary>
            /// Set or get the name of the student.
            /// </summary>
            property Name:String read fName write fName;
    end;

PHP example

It this example of a simple class representing a student with only the name stored, one can see the variable name is private, i.e. only visible from the Student class, and the "setter" and "getter" is public, namely the "getName()" and "setName('name')" methods.

class Student {
    private $name;

    /**
     * @return the $name
     */
    public function getName() {
        return $this->name;
    }

    /**
     * @param $newName
     * the name to set
     */
    public function setName($newName) {
        $this->name = $newName;
    }
}

Perl example

package Student;

sub new {
	bless {}, shift;
}

sub set_name {
	my $self = shift;
	$self->{name} = $_[0];
}

sub get_name {
	my $self = shift;
	return $self->{name};
}

1;

Or, using Class::Accessor

package Student;
use base qw(Class::Accessor);
__PACKAGE__->follow_best_practice;

Student->mk_accessors(qw(name));

1;

Or, using Moose Object System:

package Student;
use Moose;

# Moose uses the attribute name as the setter and getter, the reader and writer properties
# allow us to override that and provide our own names, in this case get_name and set_name
has 'name' => (is => 'rw', isa => 'Str', reader => 'get_name', writer => 'set_name');

1;

Ruby example

In Ruby, individual accessor and mutator methods may be defined, or the metaprogramming constructs attr_reader or attr_accessor may be used both to declare a private variable in a class and to provide either read-only or read-write public access to it respectively.

Defining individual accessor and mutator methods creates space for pre-processing or validation of the data

class Student
  def name
    @name
  end

  def name=(value)
    @name=value
  end
end

Read-only simple public access to implied @name variable

class Student
  attr_reader :name
end

Read-write simple public access to implied @name variable

class Student
  attr_accessor :name
end

References

  1. ^ Stephen Fuqua (2009). "Automatic Properties in C# 3.0". Retrieved 2009-10-19.
  2. ^ Tim Lee (1998). "Run Time Efficiency of Accessor Functions". Retrieved 1998-07-13. {{cite web}}: Check date values in: |accessdate= (help)