The two-ways rule


Posted on: January 6, 2022

Posted by:

The rule: One way of doing something is better than two.

So when the language gets a new way of doing something that is only slightly better than the previous version, or not better at all, I always stick with the way I was doing it before. That way when reading the code I never have to wonder why did the author choose this way in one place and another way elsewhere.

Coding tricks is not why we program -- we program to create new utility, and to create something that is easy not to break when it needs maintenence.

It's very common and imho totally wrong to not adopt the conventions of code you're contributing to.

Another way of emphasizing prior art.

If you can get to zero ways of doing something that's way better than one, and way-way better than two or more.

There are exceptions. Sometimes they add a feature to a language that is so sensible you have to use it. For example, initial values for parameters to functions in JavaScript. Or not having to say name: name in an object definititon, just name will do. Another one, the forEach function in JavaScript is more sensible than the equalent for version.

See the rule in the standards-makers howto. It's the same idea.

Sometimes I don't have time to write a full prose version of a post, I think it's better to write the skeletal version rather than publish nothing at all.

Read More

Related Products to this Post

Leave a Reply

Add New Comment