Hacker News new | ask | show | jobs
by kubami 1889 days ago
IMHO having this as a rule that keys in dictionaries are sorted alphabetically can be a valid rule. But this should be enforced in tooling, not "rules", and not through micromanagement. Much like using opinionated code formatters - like `prettier` you mention. It makes lives of everyone easier down the line.

Sometimes this comes from years of experience (mostly pain ;)). Some leaders, while coming from a good place - I want to help you, and others, avoid pain in the future - communicate it very badly - do it like that... because I say so!

2 comments

> having this as a rule that keys in dictionaries are sorted alphabetically can be a valid rule

Ordering keys to prevent randomness is very helpful to spot changes when using "git diff", doing code reviews, comparing files side by side.

Often it helped me spotting bugs due to a missing or unwanted element.

I wish code linters had enabled by default with the ability to disable it with some simple syntax.

I had a look at prettier's github issues in the mean time and

> No, prettier tries not to change the semantics (specifically, the AST) of the program, and sorting imports/keys could break that guarantee. See here for more details and context, as well as links to similar/duplicate issues: #1684 (comment)

it makes sense not to make it a default.

https://github.com/prettier/prettier/issues/2460 https://github.com/prettier/prettier/issues/1684#issuecommen...

I think another option is to just let it go and you can "fix" it later if you want and have the chance. Just put it in a "cleanup" commit or something. Don't be passive agressive about it, make it a team practice. Sometimes people will change the style of your code, that's ok, it's not a slight against you.

However, if you get in a situation where people are changing styles back and forth then you need to talk it out. E.g. Alice inserts a blank line, Bob removes the blank line, Alice inserts the blank line again - time to talk about blank lines and code style.

In this case, if Alice sorts the dictionary keys and Bob couldn't care less, it seems both can have what they want by simply letting Alice sort the keys. And if it becomes a common thing instead of a one-off, maybe talk about it.