Start with why, also when coding

If you never heard ofstart with whybefore, stop what you’re doing and listen to this TED talk , it will change your life.

Basically, when you want to communicate something, starting with why will completely change the way your message is received, positively.

Think about it: when you are looking for a new librairie or a framework, isn’t it frustrating when you land on bare API docs instead of a introduction page, like a README?

Start with why applies to every form of communication and guess what? Code is also a form of communication!

Generally, people code starting with what:

weather((result) => {
const raining = > 0;
  if (raining) {
console.log("Bring your umbrella")

There’s nothing wrong with this code, however, do you notice how it is sequential and not so reusable? You also have to read it until the end to grasp what it does.

To start with why, you can ask yourself, what is the purpose of this code? It’s to receive an alert to bring an umbrella. Let’s start with defining that!

const bringUmbrella = () => alert('Bring your umbrella');

Then, why should the user bring an umbrella? It would be conditional to raining. Let’s define that too:

const ifRaining = (fn) => && fn();

Put it all together:

const bringUmbrella = () => alert('Bring your umbrella');
const ifRaining = (fn) => && fn();

By extracting bringUmbrella and ifRaining it suddenly becomes more functional and modular. We can also see it’s starting to create a nice and reusable API. It’s more readable and also half of the lines of code than the first approach.

Note: In Ruby, it is even easier to start with why…

puts 'Bring your umbrella' if weather.rain?
One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.