Friday, June 12, 2015

Ban the > operator

Free your mind and ban the > operator from your source code. Instead of using > you can swap the two comparators so that all your conditionals only use < or <=

Before: if (balance > deposit)

After: if (deposit < balance) 

Here are 3 reasons why this is great.

1. It makes your programming language simpler

Once you remove two operators (> and >=) from your programming language there are two less operators in your programming language. This leads to less complexity.

Some style guides restrict language features to manage complexity.

2. Simplicity leads to consistency; consistency leads to readability

If all your comparator operators face the same direction, you don't need to parse which direction they're facing.

This takes longer to read:
if (cash < 100)
if (term_deposit > 10)
if (deposit < 100)
This is quicker to read:
if (cash < 100)
if (10 < term_deposit)
if (deposit < 100)

3. Less subjectivity leads to less bike shedding

When is the right time to use < or >?

Without > you don’t have to decide!

And if you don’t have to decide then you don’t have to debate which is best!

No comments:

Post a Comment