Sitemap
Better Programming

Advice for programmers.

Member-only story

Absence of Null in Solidity

3 min readAug 31, 2022

--

Photo by Nick Smith on Unsplash.

One of the weirdest Solidity programming language’s quirks is the absence of null. Coming from Swift, where nullability is one of the core building blocks, it felt foreign. At first, I didn’t understand how to code without such a useful feature. This post elaborates on how to find a way around that in Solidity when building smart contracts.

Concept of undefined

The concept of undefined, null, nil, None, etc. exists in languages like JavaScript, Java, Python, Swift, .etc. but it does not exist in Solidity.

In Solidity, we can call it zero or default value concept instead. That is because each value gets a slot in the memory once it is created, and should contain something.

Default values

To talk about default values, we should split the Solidity types into two blocks:

  • dynamically sized types like string, bytes, and arrays;
  • non dynamically sized types like int, bool and address.

Non dynamically sized types

With non-dynamically sized types, the game is pretty straightforward. Here are the default values for those:

--

--

Kristaps Grinbergs
Kristaps Grinbergs

Written by Kristaps Grinbergs

Blockchain, mobile and fullstack developer. Startup founder. Conference speaker. Mentor. Passionate about building products, sustainability and Web 3.0.

Responses (1)