Code Smell 157 - Balance at 0
Tags: webdev, beginners, programming, discuss
Published: 2022-08-10 21:30:00

*Today I expected a payment in my wallet. The balance was 0. I panicked.*

> TL;DR: Null is not 0. Error is not 0. just 0 is 0.

# Problems

- [Nulls](https://dev.to/mcsee/null-the-billion-dollar-mistake-1h1c)

- UX

- Usability

# Solutions

1. Make a clear distinction between a Zero and an error.

# Context

I read a lot about security issues. 

Especially on crypto.

Last week, I read about a [crypto hack thread](https://twitter.com/stephenlacy/status/1554697083331891201).

When my wallet showed me 0 as a balance, I panicked.

It was just a UX smell.

The blockchain was unreachable 💩

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/4b6caebfa8707878eba0416c260fd180)
```python
"""
Below code is automatically generated by code-davinci-002 on GTP3 Codex

1. check balance with blokchain
2. If blockchain is unreachable show 0 as the balance
"""

import requests
import json

def get_balance(address):
    url = "https://blockchain.info/q/addressbalance/" + address
    response = requests.get(url)
    if response.status_code == 200:
        return response.text
    else:
        return 0      
      


```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/f59614042404c4d55eac82d3f6f9c8a0)
```python
"""
Below code is automatically generated by code-davinci-002 on GTP3 Codex

1. check balance with blockchain
2. If blockchain is unreachable throw an error
"""

import requests
import json

def get_balance(address):
    url = "https://blockchain.info/q/addressbalance/" + address
    response = requests.get(url)
    if response.status_code == 200:
        return response.text
    else:
        raise BlockchainNotReachableError("Error reaching blockchain")
```

# Detection

[X] Manual

This is a design smell. 

We can find patterns when an [exception](https://dev.to/mcsee/code-smell-73-exceptions-for-expected-cases-504c) or [return code](https://dev.to/mcsee/code-smell-72-return-codes-19p7) is thrown and masked with a 0.

# Tags

- UX

# Conclusion

Always follow The [Least Astonishment principle](https://en.wikipedia.org/wiki/Principle_of_least_astonishment) as a guide.

# Relations

{% post https://dev.to/mcsee/code-smell-12-null-la4 %}

{% post https://dev.to/mcsee/code-smell-139-business-code-in-the-user-interface-1i4o %}

{% post https://dev.to/mcsee/code-smell-73-exceptions-for-expected-cases-504c %}

{% post https://dev.to/mcsee/code-smell-72-return-codes-19p7 %}

# More Info

{% post https://dev.to/mcsee/null-the-billion-dollar-mistake-1h1c %}

# Credit

Photo by [Jasmin Sessler](https://unsplash.com/@jasmin_sessler) on [Unsplash](https://unsplash.com/s/photos/panic)  

# Disclaimer

Code Smells are just my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

> My real criticism with Null is that it brings back again unnecessarily all the agony of having to choose whether to run your program fast without checking or run it slow with checking.

_Tony Hoare_ (Null Inventor)
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}