Introduction to Solidity

Get a brief overview of Solidity and how it compares with other common languages.

What is Solidity?

Solidity is a statically typed, object-oriented, high-level language for developing and implementing smart contracts on the Ethereum blockchain. It’s the curly-bracket language that allows us to write smart contracts.

Solidity is highly influenced by languages such as Python, C++, and JavaScript and has been designed for use on the EVM.

Solidity and JavaScript

If you currently code in JavaScript, you’ll soon discover that Solidity syntax is very similar, but the languages are different in many ways.

Press + to interact

Dynamically vs. statically typed

JavaScript is dynamically typed, while Solidity is statically typed. This means that when writing in Solidity and declaring variables, we have to explicitly state the type of value that the variable is going to hold, and this can’t be changed later. But in JavaScript, the compiler can interpret the intended data type, and we can reassign a value with a different data type to the variable. For example, in JavaScript we can do this:

let x = 20;
x = "new string";

We define a new variable and assign a value of 20 to it, making it assume an integer type. In the next line, however, we reassign new string to it, making it a string type. Compare this to Solidity below:

uint x = 20;
// will work
x = 40;
// will throw error
x = "new string"

Here, we define a new variable and immediately give it an integer type. In Solidity, uint means “unsigned integer,” which we’ll discuss in more detail later. In the next line, we attempt to assign a new value of 40. This will work, but not in the next line. Solidity won’t let us assign a string to a variable we already defined as an integer.

Compiled vs. interpreted

Solidity is a compiled language, whereas JavaScript is interpreted. This means we can immediately run JavaScript code on a browser or node environment. But in Solidity, we need to first compile the code into opcodes that can be interpreted by the EVM.

Immutability

A major feature in Solidity and smart contracts in general is that once the code is compiled and deployed, we cannot modify or delete it, and it will run forever on the EVM. This calls for extra care and proper review of smart contract code and rigorous testing on private networks and testnets before deploying to the mainnet, especially if the contract involves user data or funds, which most smart contracts do.

In JavaScript and other server-side programming languages, all we need to do is correct or modify the code and redeploy it to the server. We can also shut down the server or delete the code if necessary.

Contracts

Contracts in Solidity can be compared to classes in OOPObject-oriented programming (OOP) is a programming paradigm that uses objects to design software. These objects represent data and procedures to manipulate that data. OOP focuses on using classes to create objects, which can then interact and perform tasks, allowing for code reuse, modularity, and improved maintainability. languages like Java. Solidity supports features like inheritance,Inheritance is the process of defining multiple contracts that are related to each other through parent-child relationships. composition,Composition refers to combining multiple contracts or data types together to create complex data structures and contracts. constructors, methods, and interfaces. It also supports encapsulation by providing visibility modifier keywords like public, external, and private to define how data and variables in a contract can be accessed externally or by child contracts. Like classes, contracts can be instantiated using the keyword new.

To define a contract in Solidity, we use the keyword contract.

Press + to interact
contract TestContract {
address owner;
string testString;
constructor(string _testString) {
owner = msg.sender;
testString = _testString;
}
function changeString(string newString) public {
if (msg.sender == address(creator)) {
name = newName;
}
}
}

The code shows just some of the features mentioned, like contract, a constructor, and a method called changeString that’s publicly accessible by other contracts.

Other differences

There are several other differences between Solidity and JavaScript. A major one is that we’re charged by the line to run code in Solidity. In JavaScript, we can flex our coding skills by writing extended, complex code or keeping it concise as long as the job gets done in a straightforward manner. In Solidity, we always write code in the most cost-efficient way. We try to keep the code as minimal as possible, and we’re always looking for less expensive approaches.

Another difference is that smart contracts can interact with other smart contracts deployed on the blockchain. We can’t interact with other JavaScript code on separate servers unless it’s exposed via an API.

Solidity compiler

We already know that Solidity needs to be compiled into EVM-readable code to be executed on the blockchain. The compiler converts code written in Solidity to a collection of byte instructions. A common compiler is solc, which is available via npm. In this course, we’re going to take a deep dive into creating smart contracts with Solidity.