Euphorichuman

Euphorichuman

`nil` or empty list?

I’m managing my Postgres database with Ecto and I have come across a situation: In some of the fields for some of my records, I save empty values: For this case, it would be an empty list [].

Empty list or nil, which is better or more efficient?

Marked As Solved

eksperimental

eksperimental

empty list.
See this PR that was an optimization to MapSet.
https://github.com/elixir-lang/elixir/pull/5954

Also Liked

dorgan

dorgan

Tangential fun fact: In Erlang, the empty list [] is internally called nil, and it has it’s own position in the erlang term ordering :slight_smile:

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

I would first and foremost prioritize clarity, then performance here, since the difference is negligible in most cases. If you have a column that can either have a value, or no value, then use nil to mean “no value”. If you have a collection of things and that collection can be empty, then use an [].

al2o3cr

al2o3cr

If you’ve got a JSONB column or a nullable array column, using nil is going to cause the usual SQL NULL behaviors while using [] will not.

Last Post!

al2o3cr

al2o3cr

If you’ve got a JSONB column or a nullable array column, using nil is going to cause the usual SQL NULL behaviors while using [] will not.

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130286 1222
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49084 226
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement