Logo

dev-resources.site

for different kinds of informations.

Absence of null in Solidity

Published at
8/31/2022
Categories
solidity
web3
null
Author
fassko
Categories
3 categories in total
solidity
open
web3
open
null
open
Author
6 person written this
fassko
open
Absence of null in Solidity

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:

  • int or uint256 default value is zero 0;
  • for bool it is false;
  • address type default value is zero address 0x0000000000000000000000000000000000000000.

If we want to create a struct, the default value is a tuple of all its member's default values.

For an enum default value is the first case. This can be a very foreign approach, but that is because all the enum cases behind the scenes are an array of uint8 integers.

Dynamically sized types

With dynamically sized types, it is a different story:

  • default value for string is an empty string;
  • array's default value is an empty array;
  • bytes default value is empty or no bytes.

Code along

To illustrate default values in the code, let's create an Employee struct with an enum EmployeeType.

enum EmployeeType {
  Employee,
  Contractor,
  PartTime
}

struct Person {
  EmployeeType employeeType;
  bool deleted;
  string name;
  uint256 yearOfBirth;
  address walletAddress;
  uint256[] doorAccess;
}
Enter fullscreen mode Exit fullscreen mode

Now we can initialize a new instance of the Employee struct. Remember, we don't need to provide any member values but allocate them in the memory.

Person person;
Enter fullscreen mode Exit fullscreen mode

If we print out the person value, we will get a tuple of default values for each Person struct member.

tuple(uint8,bool,string,uint256,address,uint256[]): 0,false,,0,0x0000000000000000000000000000000000000000,
Enter fullscreen mode Exit fullscreen mode

Checking nullability

Now that we know the default values, we can check if something is "null" or, to precisely say, it has the default value.

function check() external view {
  console.log(person.deleted == false);
  console.log(bytes(person.name).length == 0);
  console.log(person.yearOfBirth == 0);
  console.log(person.walletAddress == address(0));
  console.log(person.employeeType == EmployeeType.Employee);
  console.log(person.doorAccess.length == 0);
}
Enter fullscreen mode Exit fullscreen mode

Once we execute this function, we get true for all the checks. This is how we can understand that there is no value defined.

 Deleted true
 Name true
 Year of birth true
 Wallet address true
 Employee type true
 Door access true
Enter fullscreen mode Exit fullscreen mode

Worth mentioning is that we converted it to bytes for the' string' type and checked its length. There is another way to achieve that, but that we are going to talk about in one of the future posts.

TL;DR

Solidity programming language doesn't have the nullability feature, which is common in many languages like Swift and JavaScript. Instead, types have the default values, like zero 0 for uint. It is vital to know the default values because it comes in handy once we want to check if something is or isn't defined.

Links

null Article's
30 articles in total
Favicon
How Imburse Payments Ships High-Quality APIs Faster
Favicon
Need to Verify Your JSON Schema? Here's a Few Ways to Do It!
Favicon
Code Smell 260 - Crowdstrike NULL
Favicon
Be careful when using NULL in PostgreSQL
Favicon
The most painful reason NULLs are evil
Favicon
Null or Nothing? Unmasking the Mystery of Parameters in Dart
Favicon
La Solución del Billón de Dólares
Favicon
11 Lessons to learn when using NULLs in PostgreSQL®
Favicon
NULLs Are Not The Same – A Guide
Favicon
the (not so big) Bang!
Favicon
Rust's Option type... in Python
Favicon
Understanding Nullable Reference Types in C#
Favicon
Working with NULL in Databases. Turn Your Frustration Into Delight
Favicon
TypeScript: The many types of nothing
Favicon
ERROR: null" or "null pointer exception while invoking FlowService - Storage get operation
Favicon
ServiceNow: 1 thing for safer GlideRecord scripts
Favicon
How NullPointerException can be avoided in Java
Favicon
Consider these facts when dealing with NULL in RDBMS
Favicon
Unhandled Exception: type 'Null' is not a subtype of type 'int' in type cast error when trying to call function with no int
Favicon
When <nil> is not <nil>
Favicon
Absence of null in Solidity
Favicon
How to Check for Null in Javascript
Favicon
Javascript Tagalog - Null
Favicon
Kotlin 基礎 Part 1 -- !! や ?: と ?.let で Nullable な値を処理する
Favicon
Remove null check, use the Optional
Favicon
Handling null: optional and nullable types
Favicon
More JS Concepts
Favicon
Valores null e undefined no JavaScript
Favicon
Javascript default parameter for null and undefined
Favicon
How I Learned to Stop Worrying and Love NULL in SQL

Featured ones: