Skip to content

re: I did it again. VIEW POST


Tip to prevent this from happening in the future: Extract your if logic into its own function.

Instead of:

if (num % 2 === 0) {}


const isEven = num => num % 2 === 0

if (isEven(num)) {}

This has the additional benefits of making your logic reusable and also making your if statements more readable.



I really like doing this, but sometimes, when looking at the code really quick, in those languages that use the ! operator, I've found myself doing functions like isNotEmpty() so that I don't overlook the ! by mistake.

Kotlin's standard library has methods like isNotBlank() and isNotEmpty() as part of the String class that showcase this.


Ramda has a function called complement that can help you build these functions.

import complement from 'ramda/src/complement'

const isEven = num => num % 2 === 0
const isNotEven = complement(isEven)

Believe it or not, I did exactly that and still managed to miss the not the first time around.

    let hasToken nextToken =
        not (System.String.IsNullOrWhiteSpace nextToken)
code of conduct - report abuse