Skip to content

Latest commit

 

History

History
113 lines (65 loc) · 3.9 KB

File metadata and controls

113 lines (65 loc) · 3.9 KB

Code Smell 157 - Balance at 0

Code Smell 157 - Balance at 0

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

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.

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

It was just a UX smell.

The blockchain was unreachable 💩

Sample Code

Wrong

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

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 or return code is thrown and masked with a 0.

Tags

  • UX

Conclusion

Always follow The Least Astonishment principle as a guide.

Relations

Code Smell 12 - Null

Code Smell 139 - Business Code in the User Interface

Code Smell 73 - Exceptions for Expected Cases

Code Smell 72 - Return Codes

More Info

Null: The Billion Dollar Mistake

Credit

Photo by Jasmin Sessler on Unsplash

Disclaimer

Code Smells are just my opinion.

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)

Software Engineering Great Quotes


This article is part of the CodeSmell Series.

How to Find the Stinky Parts of your Code