As humanity moves towards the digitization of everything, basic coding becomes a must even for the average person that is not keen on tech. That requirement is even more apparent when it comes to the blockchain sphere. If we really seek true decentralization, each of us should understand how things work at least on the elemental level. Whether you are an experienced software engineer, or an average Joe, the first thing you should do before learning how blockchain functions is to choose a programming language.
For a new, rumbling technology that is developing at a rushing pace, the task of picking a programming language can be daunting. There are plenty of them available and various startups and open source communities chose different languages to build their products, as their focus and sets of desired features vary. There is no standard and at this point in time; your choice would pretty much come to your personal criteria and the type of applications you crave to build.
In this article, we will review the most popular programming languages that are used in blockchain development to help you out with choosing the most appropriate option. Let’s not put it off any longer and go deep down the rabbit hole!
The most popular programming languages in the blockchain
First and foremost, we need to understand which of the programming languages are used the most when it comes to writing code in the blockchain. You will probably see some familiar names on this list.
To begin reviewing the languages for blockchain programming, we need to take a look at their granddad – C++. This language was released in 1985 by Bjarne Stroustrup to make the original C more flexible. More than three decades later, it was used to build Bitcoin.
Unlike the original C, which is process-oriented, C++ is object-oriented. This approach makes C++ more time-efficient when it comes to writing code. The data along with the functions is stored in so-called objects that can be reused later on in other programs.
Due to its maturity, it’s not a surprise that in terms of the blockchain development, featurewise C++ is quite robust. This language has memory control that ensures speed by efficient CPU management, threading that allows running parallel/non-parallel tasks simultaneously, move semantics for copying data conveniently, compile-time polymorphism for better performance through dividing responsibilities, and code isolation for separating data structures. In addition, its user base consists of more than four million developers.
The one major drawback of C++ for blockchain developers is that it’s not smart contract ready. It doesn’t mean that one cannot code a smart contract on C++, it means that some modifications have to be implemented. This is the reason why blockchain-specific languages started to pop up.
Pros: independent and multiplatform, high-speed, similar to C
Cons: rather difficult to master, complex and sometimes hard to debug, no garbage collection
Best suited for: advanced users, understanding principles of Bitcoin
C# is younger than C++, but it’s quite old too. Created at the end of the ‘90s within Microsoft, it quickly gained traction in the developer community. It’s an OOP (objective-oriented programming) language that provides great features for enterprise-grade applications, cloud, and cross-platform development. Largely backed by Microsoft, this language is a convenient tool both for professionals and casual coders.
Not only is C# flexible and easy to comprehend if you are familiar with C++ or Java, but it’s also packed with features. In addition to some of the C++ functionality, C# offers .NET framework & its class library, the common language runtime, and development frameworks (such as WPF, XAML, and ASP.NET).
C# is favored by big players in the blockchain for a few reasons:
Its syntax is easy to comprehend for the large chunk of developers that are already familiar with C++ (4.4 million devs).
It allows writing portable code for mobile devices.
It’s affordable due to the BizSpark program.
Among the most prominent startups that base their code on C# are NEO and Stratis. In addition, C# has a dedicated Bitcoin framework (NBitcoin). Microsoft isn’t falling behind and introduces the vNext blockchain for its Azure platform.
Pros: object-oriented, strongly typed, dynamic code support
Cons: weak Linux support
Best suited for: building infrastructure blockchains, Windows users
Pros: Object-oriented, prototype-based, supports functional programming
Cons: Dynamic, weakly typed
Best suited for: Entering blockchain space, building dApps
Python was created in a minimalistic manner. That is why this language is extremely easy to learn and is a great entry point for beginners. It was created in 1991 and has served a plethora of purposes like application development, network servers’ development, IoT and others. It may be not as recognized as JS yet, but it certainly makes more buzz in the tech world.
Python has been recognized by the industry’s superstars. IBM Hyperledger implemented it in Sawtooth and Ethereum has created its own iteration of this language: Pyethereum.
Pros: Very easy to learn, premade libraries and frameworks, efficient
Cons: Mostly used as a server language, not so smart context
Best suited for: Building dApps for the beginners
Golang (or Go) is an open-source language derived from C. It’s a very young language that was launched in 2007 by devs from Google. Go implementation of the syntax is more Python-like – it’s made simple to provide efficiency and make projects made with it future-proof.
Go’s strong sides for blockchain programming include the availability of modular programming that makes the development process flexible, convenient testing tools, and a huge library of functions. It would be most appealing for cloud-computing developers, as distributed calculations are very simple in terms of this language. Although this language requires compilation, it’s a good factor for security, as all the errors will be shown to the person writing the code before it’s launched. In addition, parallel programming along with Goroutines increase the efficiency of the code and applications’ throughput. This is crucial for scalability, which is a pain point in the blockchain space today. Lastly, Go runs with the OS, not on top of it. This eliminates an extra step when creating EVMs, hence increasing the speed.
Go is a well-known language in the blockchain sphere. Consider IBM Hyperledger that is used Golang for coding smart contracts on its Fabric. Docker is not a blockchain startup, but it works with the distributed systems and is built with Go.
Pros: Memory security, garbage collection, strongly typed
Cons: Not so easy to learn, relatively small community, a small number of packages
Best suited for: Building cloud dApps
Solidity is young, like Golang. It was released in 2014 by Christian Reitwiessner and his team. It’s a language that was created in order to write smart contracts on Ethereum, which is why it’s most popular in the Ethereum ecosystem. It’s worth noting that the ecosystem has grown hundreds of times in the past two years because of the various startups flooding the market. As a result, Solidity has a large user base. Its standard (ERC20) has become the staple for the industry.
Currently, the major use cases for Solidity are EVM and smart contracts. It’s not a lot, but those are extremely important and blockchain-specific as much as it could be. If you want to build a dApp, Solidity is your go-to choice.
Pros: Statically typed, easy to learn, trendy
Cons: Not universal, vulnerable
Best suited for: Development of the smart contracts
Basics of coding on the blockchain
Now, that we have reviewed the most popular programming languages in the blockchain, it’s time to get out hands dirty with actual coding. Let’s see how the basic operations are performed in terms via different programming languages.
Before we begin, it’s a good thing to refine our knowledge about what a blockchain is. A blockchain is a distributed ledger with transactions input in a chain of blocks. A block is a placeholder for transaction records. Each new block has a pointer to the previous one in the form of a hash function. After the block is full, a hash is generated using all of its transactions and attached to it. There is also a timestamp server that prevents double-spending and a consensus mechanism that allows nodes to agree on the correct version of the ledger.
Time to put theory into practice. The purpose of this is not to build a complete app, but to show what the process looks like.
Every blockchain out there has a hard-coded initial block, called a genesis block. Let’s create one.
Image by Utoday
Now we can create another block. In order to do so, we’ll type getLatestBlock().
Image by Utoday
The consensus mechanism is a simple ‘for’ loop function that runs over all the blocks, starting from 1, because there’s no need to revise the 0 genesis block.
The data structure for a blockchain on C# is rather straightforward. It has Block & Blockchain classes. Here they are.
Image by Utoday Image by Utoday
If we visualize the structure, it looks like this:
Here’s how we add new blocks:
In order to make our blockchain reach consensus, e.g. validate it, we need only 2 lines of code along with an IsValid method that will check data in each of the blocks and compare hashes.
Now we are reaching territories of more accessible languages, this should be more fun if you don’t have any coding experience. In order to create a blockchain, we need a runtime environment, such as Node.js. Each of the additional blocks will have the following data: index, time of creation, misc (like sender & receiver addresses and the amount transferred), previous block’s hash, own hash. Once it’s done, it’s time to move over to hard-coding of the genesis block:
Image by Utoday
As we have gotten the genesis block, we shall now add blocks with new data via a nextBlock function. This function will automatically go through all 5 types of data and include them in each new block.
Image by Utoday
This time we will start with understanding how a transaction in Python looks like. It’s a simple timestamped post with some data attached.
Image by Utoday
These transactions will get into the blocks. Let’s introduce this class.
We will use SHA-256 which is utilized in Bitcoin to create hashes for validation of our blockchain’s integrity.
Now we can add the blocks, but first, let’s introduce the Proof-of-Work algorithm that ensures that the data in the blockchain wasn’t corrupted.
After that’s done, we are clear to start the blockchain.
We’ll start with defining our blocks’ properties. BPM is arbitrary data, like network transactions, addresses, etc.
Image by Utoday
In order to create blocks, we need to hash data in them and chain by corresponding results. The code for hashing data within a block in Go looks like this:
Finally, we can start making new blocks, using the generateBlock function. Time to write the consensus, which is represented by isBlockValid function.
As all things are set, we are ready to launch the block production process. Note that at this point the blockchain will need a server to run on, so there are some extra bits of code that reflect this.
Image by Utoday
Building smart contracts differs from what we have just review, but it’s still simple. First, we need to create a contract and name it. This contract is for a person’s will. In case they die, the funds shall be automatically distributed to specific addresses.
Image by Utoday
Here, we specify the owner’s Ethereum address (owner), the amount left for distribution (unit) and the conditional variable for the owner’s state (bool). The next step is to add modifiers that extend the conditional logic to provide a function with the owner’s state.
As this is done, we can outline the addresses that will receive the heritage. Then we continue to specify the distribution of funds in case isDeceased = true. Please note that at this stage we can hide the function from the public by putting ‘private next to the ‘payout’ function. Voila, the code can now be deployed.
Image by Utoday
To sum up, there is no ultimate blockchain-focused programming language. The blockchain itself is in its infancy and there are unexplored horizons and unstepped grounds. Like in many other spheres, the best possible way of getting around confidently is to become a full-stack developer, which may be a tough task, considering how much knowledge you will need to have. Thankfully, many of the programming languages have similarities in syntax and structure to ease the transition.