DEV Community

Discussion on: Which Single Resource Has Most Affected How You Code?

Collapse
 
murrayvarey profile image
MurrayVarey

Sounds awesome! I've shied away from functional programming in the past, so will definitely check this out. Is it something that you use a lot?

Collapse
 
vonheikemen profile image
Heiker • Edited

Sure. Even just using pure functions can change the way you write things.

Whenever I get to write a function from scratch I end up with something like this.

function big_function() {
  const data = get_data();

  const errors = validate(data);
  if(errors.length) {
    show_errors(errors);
    return;
  }

  const clean_data = some_process(data);
  const saved = save_stuff(clean_data);

  if(saved == false) {
    show_errors(['oops, something went wrong']);
    return;
  }

  show_message('we good');
}

I have a 'big function' that coordinates a bunch of 'little functions.'

The things that can have an effect on the outside world get their own functions and most of the time don't process anything, they just show/save/send whatever you throw at them.

Almost everything ends up being a two step process, handle the data and using the data.

The most important thing is to make the little functions as predictable as possible. Given the same inputs should always give you the same result.