create account

Ethereum Development - Don't Use Constant, Use View And Pure by igormuba

View this thread on: hive.blogpeakd.comecency.com
· @igormuba ·
$0.09
Ethereum Development - Don't Use Constant, Use View And Pure
I have posted a couple of minutes ago my first tutorial on Ethereum and Solidity develoment, I would now to explain one thing that, apparently, from what I have read online, many people can't understand, I myself took some time to get the hang of it.

In old versions of Solidity (Ethereum's programming language) you used the keyword `constant` to indicate that the transaction would not change any state on the network, this means, it wouldn't have to be verified by the network. This meant that the transaction was very cheap to be executed, in comparisson with transactions that required a change of state of a contract or an address.

The keyword `constant` has been abandoned on solidity 0.4.16 and replaced by the keyword `view` that represents the exact same thing but has a more intuitive name, so, the `view` does not write or change anything, but it does read from the network. The other read only keyword is `pure`, which represents functions that **do not write NOR READ** from the network.

The `pure` keyword is the cheapest transaction you can have, even cheaper than the `view`

Here is an example

```
pragma solidity ^0.5.0;


contract firstClassContract{

    uint private unsignedInteger = 5;

    function aPureFunction(uint newVariable) public pure returns(uint) {
        return newVariable*2;
    }

    function aViewFunction(uint newVariable) public view returns (uint){
        return unsignedInteger*newVariable;
    }

}

```

And now I am gonna call the aPureFunction with the argumento 5, making it multiply 5 by 2
and then call the aViewFunction with the argument 2, making it multiply 5 by 2 too

![](https://cdn.steemitimages.com/DQmPz8pKQdjQuJY19krcStqjNQJcwS4FYQZs5hJzrngzojq/image.png)

I did the same mathematical operation in both two, but look at the price cost difference on them!

![](https://cdn.steemitimages.com/DQmfJSe7uNqdkRnShfxouV9Xrvwvx1f7kzAvETfeKf5xeYt/image.png)
![](https://cdn.steemitimages.com/DQmb25gVWass3yWSSQa3n31twEvoDBcZyG3kSvWrgpq6g3f/image.png)

The `pure` function costs almost half of the price of the view function because it does not fetch data from the contract variables
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 32 others
properties (23)
authorigormuba
permlinkethereum-development-don-t-use-constant-use-view-and-pure
categoryethereum
json_metadata{"tags":["ethereum","development","solidity","programming","dapps"],"image":["https://cdn.steemitimages.com/DQmPz8pKQdjQuJY19krcStqjNQJcwS4FYQZs5hJzrngzojq/image.png","https://cdn.steemitimages.com/DQmfJSe7uNqdkRnShfxouV9Xrvwvx1f7kzAvETfeKf5xeYt/image.png","https://cdn.steemitimages.com/DQmb25gVWass3yWSSQa3n31twEvoDBcZyG3kSvWrgpq6g3f/image.png"],"app":"steemit/0.1","format":"markdown"}
created2018-12-31 00:07:27
last_update2018-12-31 00:07:27
depth0
children0
last_payout2019-01-07 00:07:27
cashout_time1969-12-31 23:59:59
total_payout_value0.079 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,141
author_reputation129,602,803,494,761
root_title"Ethereum Development - Don't Use Constant, Use View And Pure"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id77,630,601
net_rshares189,204,805,732
author_curate_reward""
vote details (96)