Logo

dev-resources.site

for different kinds of informations.

The most painful reason NULLs are evil

Published at
5/31/2024
Categories
rails
null
Author
epigene
Categories
2 categories in total
rails
open
null
open
Author
7 person written this
epigene
open
The most painful reason NULLs are evil

I keep harping on about doing null: false everywhere, especially for strings and booleans, but sometimes there are sneaky exceptions for number fields, where a default of 0 does not make sense and the values will not be available for a time, some draft records etc.

You have to be extremely careful then because apparently NULLs are not "not equal" to anything. What do I mean?

Consider these User records:

id: 1, age: 20
id: 2, age: 25
id: 3, age: nil
Enter fullscreen mode Exit fullscreen mode

How would you query for all users who are not 20?
where.not(age: 20), right? Sorry to say, but User#3 will be omitted from such queries. 😫

You have two options:

  • denullify the age column (may be impossible)
  • tweak the query to handle the silly null edge-case:
where.not(age: 20).or(where(age: nil))
Enter fullscreen mode Exit fullscreen mode
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: