User Tools

Site Tools


Sidebar

Welcome to DIDO WIKI

dido:public:ra:1.2_views:3_taxonomic:4_data_tax:08_objects:07_opers:09_general

This is an old revision of the document!


2.3.4.8.4.6 Pure Methods

Overview

Return to Top

Pure methods are limited to are often referred to as Pure Functions. Pure Functions are a cornerstone in Functional Programming and are designed to produce no Side Effects. Pure Functions are characterized as follows1):

  • Are dependent only on
    • Declared input parameters
    • Algorithm to implemented
    • Values within the scope of the function, therefore, it can not
      • Depend on accessing any values defined outside the function scope (i.e., another field in the same class, or global variables)
      • Modify mutable values outside the function scope (i.e., other fields in the same class, or global variables)
      • Use external input or output (I/O). It can’t rely on input from files, databases, web services, UIs, etc; it can’t produce output, such as writing to a file, database, or web service, writing to a screen, etc.
  • Do not modify input parameters

Figure 1 provides a grapic represeting a Pure Function. Basically, the Pure FUnction is an isolated piece of logic that given the same input always produces the same output. It's isolation means it has no unintended sideeffects outside of itself and only the inputs determine the processing. Another way to think of a Pure Function is at their center there is a Deterministic Algorithm (Also see Black Box Testing).

Figure 1: Pure functions

In some languages (i.e., C, C++, Rust, PHP, JavaScript/ECMAScript), it is possible to have methods (i.e., procedures or functions) existing outside the class container. Java and C# requires operations to exist within a class container, and therefore does not support General Methods. C++ does not recommend having General Methods outside of a class, but because C++ is more or less an extension of C, it does support them.

Often, the architecture and design of Functional Programs depends on the identification, design and creation of pluggable, reusable functions. Many of the frameworks used in modern applications reaching across many tiers rely heavily on stateless, client-server Representational State Transfer (REST) models and Command Line Interfaces (CLIs) .

Figure 2 graphically represents pure functions used in a Functional Program.

  • The pure functions used in the Functional Program are identified (See items A, B, and C)
  • The functions are from a reuse repository (i.e., library), or they can be created especially for the new Functional Program
  • The Functional Program is responsible for the lifecycle of each data element (i.e., State Variables)
  • The order of the functions is established in the Functional Program
  • The association is made of the functions with appropriate the State Variables (which are Input Data and which are Output Data)
  • The Functional Program is executed:
    1. The Functional Program is started
    2. Calls are made to the functions in the desired order (i.e., steps 2-4) and the State Variable values are passed into or out of the functions
    3. The Functional Program is terminated
Figure 2: The use of pure Functions in a Functional Program

DIDO Specifics

Return to Top

Ethereum's Solidity is an Object-Oriented Programming (OOP) supporting four closely related object container types (Java and C++ have just one class):

contract Contracts, in Solidity, are similar to classes in object-oriented languages. They contain persistent data in state variables, and functions that can modify these variables. Calling a function on a different contract (instance) will perform an EVM function call and thus switch the context such that state variables in the calling contract are inaccessible. A contract and its functions need to be called for anything to happen. There is no “cron” concept in Ethereum to call a function at a particular event automatically.
interface Interfaces

are similar to abstract contracts2), but they cannot have any functions implemented. There are further restrictions:

  • They cannot inherit from other contracts, but they can inherit from other interfaces.
  • All declared functions must be external.
  • They cannot declare a constructor.
  • They cannot declare State Variables.
  • They cannot declare function modifiers modifiers.

Some of these restrictions might be lifted in the future.

Interfaces are basically limited to what the Contract ABI can represent, and the conversion between the ABI and an interface should be possible without any information loss.

  • Note: The difference between an Interface and an Abstract Contract is the interface has no implementations for the function. And abstract contract can have some functions with implementations and some without implementations.

The following is an examle Interface called EIP 20: ERC-20 Token Standard3).

pragma solidity ^0.8.7;
// SPDX-License-Identifier: MIT

interface IERC20 {
    function totalSupply()
      external 
      view 
      returns (uint256);
    function balanceOf
      ( address account 
      ) 
      external 
      view 
      returns (uint256);
    function transfer
      ( address recipient, 
        uint256 amount
      ) 
      external 
      returns (bool);
    function allowance
      ( address owner, 
        address spender
      ) 
      external 
      view 
      returns (uint256);
    function approve
      ( address spender, 
        uint256 amount
      ) 
      external 
      returns (bool);
    function transferFrom
      ( address sender, 
        address recipient,
        uint256 amount
      ) 
      external 
      returns (bool);
    event Transfer
      ( address indexed from, 
        address indexed to, 
        uint256 value
      );
    event Approval
      ( address indexed owner, 
        address indexed spender, 
        uint256 value
     );
} // End  IERC20 interface
library
1)
Alvin Alexander, AlvinAlexander.com, The Definition of “Pure Function”, Accessed: 30 December 2021, https://alvinalexander.com/scala/fp-book/definition-of-pure-function/
2)
Abstract contracts are contracts that have at least one function without its implementation. An instance of an abstract cannot be created. Abstract contracts are used as base contracts so that the child contract can inherit and utilize its functions. GeeksForGeeks, Solidity – Abstract Contract, 13 July 200, Accessed: 2 January 2022, https://www.geeksforgeeks.org/solidity-abstract-contract/
3)
Crypto Market Pool, Blockchain Engineer Resource, Interface in Solidity smart contracts, Accessed: 2 January 2022, https://cryptomarketpool.com/interface-in-solidity-smart-contracts/
dido/public/ra/1.2_views/3_taxonomic/4_data_tax/08_objects/07_opers/09_general.1641253816.txt.gz · Last modified: 2022/01/03 18:50 by nick
Translations of this page: