Ethereum
$204.70 0.02%
ETH · 58w

How to write clean, elegant Solidity code using function modifiers — Colony

How to write clean, elegant Solidity code using function modifiers

One of the most interesting features in Solidity are function modifiers. When added to a function body they extend it, allowing us to abstract parts of our code and write functions with fewer conditionals checks intertwined with state changes.

Creating a function modifier

The syntax looks similar to declaring a function, except it uses the modifier keyword. The underscore on indicates how the original function is changed:

modifier assertValueIsOne(uint value) { assert(value == 1); _; // original function goes at this point} function doSomething(uint value) assertValueIsOne(value){ } Use cases

Checking for invariants or conditions using require, revert (throw is deprecated, see more here) or assert accordingly:

// For parameterless modifiers, we can omit parenthesismodifier isAllowed { assert(msg.sender == owner); // contract state _;} modifier isValidAddress(address target) { require(target != 0x0); _;} modifier isValueBiggerThanZero(uint value) { require(value > 0); _;} function isOwner(address target) constant returns (bool) { return target == owner;} function simpleFunction(address target) isValidAddress(target) isValueBiggerThanZero(msg.value) isAllowed // omitted parenthesis for parameterless modifiers payable{ // do something} Please note that modifiers are applied to the original function in the order they are presented on the function declaration, i.e: isValidAddress, isValueBiggerThanZero, isAllowed and then payable.

Checking for msg.sender permissions:

modifier isOwner { assert(owner == msg.sender); _;} function finalize() isOwner { finalized = true;}

Raising events before or after a function is called:

event StartEvent();event FinishEvent(); modifier logEvents { StartEvent(); _; FinishEvent();} function toggle () logEvents { } Conclusion

Before formal verification or code coverag...

Continue on blog.colony.io
Recent news
ETH +0.02% · twitter.com · Now

Interview with CEO of Changelly

check an #exclusive interview with @ilyabere CEO of @Changelly_team https://t.co/JtPjLoyZV5 about #bitcoin #ethereum #cryptocurrency #crypto— kingscrown (@kingscrownBTC) October 24, 2018
ETH +0.02% · twitter.com · 5h

The post-Tether stablecoin wars have begun

The post-Tether stablecoin wars have begun:Paxos/TrueUSD - BinanceTrueUSD -BittrexGemini Dollar - GeminiCircle USDC - Coinbase Maker Dai - HitBTC (lol)— James Spediacci (@JamesSpediacci) October 23, 2...
ETH +0.02% · intelauthentic.com · 7h

Using Deep Learning to Predict ETH Prices

Ethereum prices have fluctuated wildly in the past year, with daily double digit percent gains and losses the norm in the current state of affairs. Intelligence Authentic LLC endeavors to understa...
ETH +0.02% · medium.com · 8h

How ICOs Attract Investors

In order to attract investors, blockchain ventures devise new and interesting ways to garner interest in their concept. The majority of blockchain projects choose a bonus structure or a combination…
ETH +0.02% · ec.europa.eu · 8h

European countries join Blockchain Partnership

On 10th of April 2018, 21 Member States and Norway agreed to sign a Declaration creating the European Blockchain Partnership (EBP) and cooperate in the establishment of a European Blockchain Services ...
ETH +0.02% · twitter.com · 10h

Coinbase on Twitter: "Coinbase is launching support for the USDC stablecoin. Starting today, Coinbase customers in supported jurisdictions can buy, sell, send, and receive USDC at https://t.co/bCG11KveHS and in our mobile apps. Learn more here: https://t.co/ZPKaIUAAsW… https://t.co/uKbSAXe0pv"

Coinbase is launching support for the USDC stablecoin. Starting today, Coinbase customers in supported jurisdictions can buy, sell, send, and receive USDC at https://t.co/bCG11KveHS and in our mobile ...
ETH +0.02% · cryptoblockchainevents.com · 10h

CoinGeek Week London

In November 2018, the true pioneers of cryptocurrency and blockchain will be converging at our CoinGeek Week Conference, allowing them to share their knowledge, experiences and discuss the unique a…
ETH +0.02% · medium.com · 11h

GPU support arrived to Golem Brass 0.18.0

We are stoked to present a new major update for Brass Golem — 0.18.0. This new update comes with all kinds of goodies, the most prominent of which is the one all of us have been waiting eagerly for —…
ETH +0.02% · theverge.com · 12h

HTC's blockchain phone is out, selling for 4.78 ETH

HTC just announced more specs for its much-hyped blockchain phone, the Exodus 1, and is letting people sign up for preorders. The phone contains a wallet that’s kept in a secure area "protected from t...