Piggybank’s JavaScript Code Guide
Oscar Godson
441

While I completely agree with the importance of the 3 principles you brought up here, I think you got it wrong, like many others, with regard to Explicitness.

Being explicit is important, but it does _not_ have to come at the expense of brevity and elegancy.

Writing (let alone reading, or worse, maintaining) long, verbose and tiring code like the one you suggest as ‘explicit’ is just throwing the baby out with the bathwater.

To me, it seems that as long as you keep in mind the need for clarity and explicitness but also do not forget the advantage of minimalism, you can enjoy both worlds. Following code is simpler, cleaner, clearer and shorter than both your examples:

const fadeAction = dataAdded ? 'fadeId' : 'fadeOut';
Widget[fadeAction]();

If I had 5 ‘if’ clauses like you suggested, I would be drowning in unimportant boilerplate code, missing the important parts. I think that the above approach presents a better balance between explicitness and minimalism.

Show your support

Clapping shows how much you appreciated Dani Kenan’s story.