Skip to content

Comment on Don't let dicts spoil your code (2020)

Comments

When JavaScript added hash/object deconstruction (both at the argument level and assigning variables) I noticed code has been using Dict-like function arguments everywhere. It makes typing them a bit more of a pain in the ass (especially without default arguments).

I haven’t decided if I like it better than just breaking up objects into arguments in a more simple functional style.

On one hand it’s more predictable but on the other most complex apps start passing around objects for everything. Typescript of course helps with that, as does nearly modularized code (ie not passing in full typed objects outside of the parent module which owns/generates them unless they uniquely operate on the full object).

These are the small rescissions you end up making a hundred times.

Whether to pass in a single dict/object/map/assoc-list or individually separated arguments is a tale as old as LISP (JS has some FP roots if you squint hard enough) and most LISPs (and JS has started doing this as well) mitigates this with destructuring:

Common Lisp: https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node252.html

Clojure: http://blog.jayfields.com/2010/07/clojure-destructuring.html

JS: https://simonsmith.io/destructuring-objects-as-function-para...

JavaScript doesn't have named arguments, so passing a lot of stuff in arguments makes code unreadable.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.