Flat Is Better Than Nested
I’m pretty sure the Zen of Python got me a job. For those who
don’t know, the Zen of Python is a neat easter egg in the Python
programming language. If you run import this, it
prints a nice little poem about the key guiding principles behind
the design of the language. Go on, if you have Python installed on
your system, try it!
Pretentious? Yes, definitely. Also it’s applied inconsistently (just like every other guideline in Python) and parts of it have definitely not aged well (to be detailed in another post, perhaps). Nevertheless, I was applying to a startup that was building a team to rewrite their legacy PHP code into Python, and I mentioned the Zen of Python to the hiring manager. He had apparently never heard of it, so I popped open a Python interpreter and spent a little while going over the principles inside. He was really impressed and I’m pretty sure I bombed the rest of that interview, so I can only attribute my success in that job search to the Zen of Python.
There is one oft-overlooked stanza that goes: > Flat is better than nested
I think this is one of the most underappreciated principles in software.
It is decidedly not obvious. “Beautiful is better than ugly”: obvious. “Simple is better than complex”: pretty obvious when you think about it. “Flat is better than nested”: ???
Us computer wizards like to organize things. We like to make
the perfect taxonomy that fits every concept into the right place
in the tree. From the humble object to the
AbstractOpenGLRenderingPipelineWorkerFactory. It
feels good when your carefully designed DDD architecture
where each class has exactly one responsibility and a single five
line method comes together.
This is a mistake. Let’s think about why.
At the most basic level, the scroll wheel on your mouse is a very powerful tool. You can search through and read code much faster with a wheeee on the scroll wheel (even better if you have one of those low-friction ones) than clicking around a file tree or following symbol references.
Nested concepts are hard to reason about. There’s a thing, which is a parent of these other things, and these other things can be of type A, B, or C, and they all come under the umbrella of…
Flat lists are easy to reason about. There’s a thing, and another thing, and another thing…
Your computer likes flat lists. It’s true. Nested structures are awkward to serialize, and often end up strewn across whatever medium (memory, hard drive, etc.) they are being stored on. Flat lists are easy to serialize (they already are serial!) and your computer can make use of cache locality and prefetching techniques to race through data.
We are no longer in the age of needing to meticulously organize our photo collections. I can go to my photo store, type “dad”, and be presented with every photo I’ve ever taken of my father. I didn’t need a “photos of dad” folder, or even naming or tagging schemes. Computers are really, really good at scanning through hundreds of thousands of things to find the thing you’re looking for.
Adding new items to a list is easy. Hey, I’ve got a new thing! Into the collection it goes! I sure am glad I don’t have to build some mental model just to add a new thing, that would be a big context switch and add a whole bunch of friction to adding a new thing (this is why I love systems like zettlekasten/Obsidian).
These are all different applications of nesting: file hierarchies, class inheritance, code indentation, data structures, organization strategies - but the principle is the same.
Unfortunately, there is a limit to how large a list can grow before it becomes unwieldy. Databases don’t just keep everything in one big serial file - instead they segment into a hierarchy of lists called a “B-tree”, at the end of which is a list of manageable size. However, I think that many people, myself included, underestimate how much they can process before they start to get overwhelmed. The human brain is fantastic at selectively applying attention. How many filenames can you scan through in the time it would take to click to open a subdirectory? How many can your computer scan through in that same time?
- omegastick