learnblock.id.blockstack

Sep 136 min read

How is Clarity different from Solidity ?

Clarity is Blockstack ’s smart contract programming language for use with the Stacks blockchain. Clarity supports programmatic control over digital assets within the Stacks blockchain (for example, BNS names, Stacks tokens, and so forth).

Unlike the Turing complete programming languages known so far, Clarity is a non-Turing complete language that intends to avoid the issues of a Turing complete programming language and also it aims to provide the users an option to do static analysis well before the code is being implemented.

In my previous article on "Introducing Clarity" , we have seen what is Clarity and why it is designed as a non-Turing complete and Interpreted language. In this article, let's see how Clarity is different from one of the most popular smart contract languages Solidity .

Clarity Vs Solidity

Let's start with the basics first.

Solidity is the language used to write smart contract programs for the Ethereum blockchain, whereas Clarity is the language used to write smart contracts for the Stacks 2.0 blockchain.

Solidity was initially proposed in 2014 by Gavin Wood and later developed by the Ethereum project’s Solidity developer team. It is currently in version v0.7.

Clarity was launched in April of this year, but has already seen an explosion of interest and experimentation. Hundreds of Clarity programs are already sprinkled around Github, with community members cranking out more everyday.

Turing completeness

Clarity is a non-Turing complete language, whereas Solidity is a Turing complete language .

In a Turing complete programming language such as Solidity, you can express, for example, infinite loops, while in Clarity you cannot. To put it differently, in Solidity it might not be possible to explore all execution paths without actually executing the code, but in Clarity we can statically analyze all possible outcomes. This helps developers analyze and uncover possible bugs, before even implementing or executing the code. This avoids a whole host of issues in the future, and the overall quality of the code is improved.

Gas Estimation

Clarity is a decidable programming language by design.

A major advantage of Clarity being a decidable language is that contract termination and the transaction fee can be guaranteed, whereas in Solidity that is not the case.

Compiler/No compiler

Clarity is an interpreted programming language whereas Solidity is not.

In Clarity, contracts are written in human-readable form, and are accessible to any professional auditor or casual onlooker. The code deployed onto the Stacks 2.0 blockchain is “What You See Is What You Get", and prevents the surfacing of the compiler bugs. Whereas, In Solidity, the source code is not published , only the compiled version of the source code is published to the blockchain. This causes the difficulty to audit the code once pushed into the chain.

Design Principles

When it comes to design principles, Solidity differs from Clarity by using an imperative style of programming (think C++, Python and JavaScript), whereas Clarity uses a functional style (think Scala, Scheme, and LISP).

When it comes to structure, Solidity is an object-oriented programming language that supports inheritance, libraries, and user-defined types. Whereas Clarity is not object-oriented, and prioritizes precise and unambiguous syntax that allows developers to predict exactly how their contracts will be executed.

Security

If you’ve heard of smart contracts, you’ve likely also heard about some of their notable failures. As more assets are created and stored in smart contracts, their vulnerabilities become more consequential as the prize money for exploiting them grows .

When it comes to security, Clarity has post-conditions on tokens. This allows users to proactively defend their assets from theft or destruction by unknown attackers or unknown bugs in other contracts .

In addition, the set of reachable code in Clarity can be efficiently determined, so you can ensure that a transaction only ever runs the code you have vetted yourself first. In Solidity, it is much more difficult to track all possible runtimes of a contract.

There have been a handful of extremely high profile exploits around the blockchain industry. In 2017, ‘The DAO’ hack abused a recursive call exploit to completely eviscerate one of early-blockchain’s most promising smart contract use cases. Re-entrancy is always a concern and is what caused the Parity multi-sig wallet to lose over $30 million in $ETH. The halting problem, where a program gets trapped in a particular code snippet, can cause big computational problems and is what forced Ethereum to implement the gas system.

All of these vulnerabilities were in some way caused by unnecessary complexity accompanying a Turing-complete programming language such as Solidity.

Clarity’s support for types and a type-checker eliminates whole classes of bugs like unintended casts, re-entrancy exploits, reads of uninitialized values, and whole lot of security vulnerabilities which are not prevented in Solidity.

Check out this example:

Now that , we have seen the design differences between Clarity and Solidity , let's see how easy it is to get started with Clarity vs Solidity.

There are many frameworks out there in the wild, such as Truffle or OpenZeppelin, that help developers quickly get started with Solidity. However, the syntax of Solidity is not very clear and it’s immensely time-consuming to audit programs.

Clarity files are simple, and readable in their original form. More advance development frameworks like Truffle are in the works (side-project idea for all the inspired people) but all that is needed is a Node Js and a text editor.

Take a look at another basic example: The classic ‘Hello World’ program in Clarity and Solidity:

Hello world in Clarity

Hello world in Solidity

For Clarity contracts to be written and executed, all that's needed is the latest version of Node JS, and a text editor or IDE of your choice.

Test your contract with any of the Stacks nodes (local or testnet). We fondly call the local environment as mocknet, versus the Testnet label: Argon. There’s also an awesome Clarity starter kit which makes getting started even easier. There are a lot of resources, tutorials, demos, boilerplates, and hackathons available to get started with Clarity.

Conclusion

Let's see the summary of differences between Clarity and Solidity.

Every smart contract programming language has its pros and cons and I have tried jotting down the primary differences between Clarity and Solidity in this article. If Clarity interests you, check out the Blockstack docs and get started ! Let's build a new user-owned internet together with a more powerful and secure smart contract language!!

Share this story