Flat, Single, Leafs

The Benefits of Small for Composition

I recently completed a drop-dead simple conversion module of a Google sheet into a table of objects when it dawned on me over coffee why keeping our object domain models flat, single, and on the end (leafs) was so obviously valuable. I could simply look at each row in the table as the attributes of the object and be done with it. But even more obvious to most is that the object can always be represented just as a single associative array (hash, map, collection).

Look I’ve known and taught this for a long time, but for some reason during that easy conversion I fell in love with JavaScript above all other language (including my favorite, Go). Looking at a hash as an object is perhaps the most brilliant, forward-thinking concept Brendan Eich added to the language when he created it. To my knowledge no other language has ever done this (maybe Lua). I wonder if that was inspired by Scheme as so much of the rest way.

Moral of the Story

Keep the objects in your domain model small, with only one layer and independent. Then make other objects out of them that associate them if necessary, say like in a relational join table. This is kind of like suggesting you simply normalize your data and objects. I know that sounds obvious to so many, but to beginners it is absolutely essential.