An Accessor Method is an Operation defined by Object-Oriented Programming (OOP) providing access to Field Data. There are generally two Accessor Methods defined: get
and set
. In some Object-Oriented (OO) paradigms, the Accessor methods are further defined as:
Some benefits of using a Mutator Methods include:
The following discussion uses c++
, however, it is very similar to Java and a “verbose C#” form. C# has some shorthand ways of coding accessor and mutator operations (see the next section).
In C++, the accessor methods are usually implemented as getter operations and the mutator functions are usually implemented as setter operation. The use of the prefix get
and set
at the beginning of a function name is merely a programming convention, and there is nothing within the C++ language to enforce the convention, however, following the naming convention makes it easier for others to read and use the code.
Example of using Getters and Setters in C++
#include <iostream> using namespace std; class Account { // Private attributes private: int balance; // Public Accessor and Mutator operations public: // setBalance sets the balance in the account void setBalance ( int _newBalance ) { balance = _newBalance; } // End setBalance // getBalance returns the current balance in the account int getBalance() { return balance; } // End getBalance }; // End Account class // main used as a unit tester of the Account class int main() { Account account; account.setBalance ( 50000 ); cout << myObj.getBalance(); return 0; } // End Main tester
C# has a shorthand way of specifying the Accessor and Mutator methods, which reduces the chance of errors in the code since it is generated.
Example of using Getters and Setters in terse C#
using System; class Account { public int balance { get; private set; } } // End Account Class
The following is a verbose Ethereum example using Solidity of Smart Contract using Accessor and Mutator methods (i.e., Getters and Setters).
Exaple of using Accessor / Mutator Methods in Solidity
pragma solidity ^0.6.0; // A simple smart contract contract MessageContract { string private message // private is the default visibility = "Hello World"; function getMessage() public returns ( string ) { return message; } // End getMessage function setMessage ( string newMessage ) public { message = newMessage; } // End setMessage } // End MessageContract
As with C#, the Solidity compiler automatically creates getter functions for all public state variables. In our contract, by changing the visibility of the state variable message
to public, a function with no parameters (i.e., ()
) returning a string
representing the current state of the message
state variable.
Setters
are not automatically created since the function would be visible to anyone that access the Smart Contract.Exaple of using Accessor / Mutator Methods in Solidity
pragma solidity ^0.6.0; // A simple smart contract contract MessageContract { string public message = "Hello World"; function setMessage ( string newMessage ) public { message = newMessage; } // End setMessage } // End MessageContract
Solidity also allows for attributing functions beyond just public visibility with the use of two keywords: view and pure:
view
functions only read from the contract state variables and can not modify the contract state. This is usually added to getters.Example of using Accessor Method and the view attribute in Solidity
pragma solidity ^0.6.0; // A simple smart contract contract MessageContract { string public message = "Hello World"; function getMessage() public view returns ( string ) { return message; } // End getMessage function setMessage ( string newMessage ) public { message = newMessage; } // End setMessage } // End MessageContract
When a function is attributed as view, the following things are considered modifying the contract's state and the compiler gives warnings:
selfdestruct
via calls
pure
functions do not read or modify the contract state variables and are only used for computations (i.e., mathematical calculations) or table lookups.Exaple of using a pure attribute in Solidity
pragma solidity ^0.6.0; // A simple smart contract contract SupportContract { function add ( uint firstNumber, uint secondNumber ) public pure returns ( uint ) { return firstNumber + secondNumber; } // End add } // End SupportContract
block
, tx
, msg
(msg.sig
and msg.data
can be read).revert()
and require()
operations are allowed in a pure
to ensure no state changes occur when an error occurs.
: Note: See: https://www.tutorialspoint.com/solidity/solidity_pure_functions.htm
[char]Review