Code style: how do you order map keys?

332 views
Skip to first unread message

Yuri Govorushchenko

unread,
Sep 18, 2019, 5:06:44 PM9/18/19
to Clojure
I usually try to organize related things in different files in the same order to ease code reviewing, reading and writing new code. For example: order of protocol method declarations, their definitions in records and unit tests. And I was wondering what's the best way to order keys in maps and specs.

The simple rule could be to always sort keys lexically, so that the order stays the same across files and there's no additional thinking needed when adding new keys.
But with `s/keys` it's not that simple since `:req`/`:req-un` can break the consistency across the files. E.g. during spec definition the correct lexical order is `:req-un [:a/x :a/y ::d]`, but during "usage" it will be different: `{:d ... :x ... :y ...}`.
The lesser drawback of this approach is that some human-friendliness is lost, e.g. keys `host, port, db, user, password` must be ordered as `db, host, password, port, user`.

Do you try to maintain the same order of map keys (including keys in specs) across the files? If yes, what rules do you follow?

Thank you.

Matching Socks

unread,
Sep 20, 2019, 6:10:01 AM9/20/19
to Clojure
Clarity is important.  Considering the conundrum of sorting namespace aliases, vs using the fully-qualified name as the implicit sort key despite the apparent alias; and the burden of shuffling everything after a refactoring; any sorted order will sooner or later be "broken", so you might as well aim for clarity from the start.  If other participants resist, you can proceed in two stages.  First, rename all variables using a section of Unicode where nobody knows the collation sequence.  
Reply all
Reply to author
Forward
0 new messages