Categories
Geekpulp

How to function correctly

Coding is a fairly amazing thing to be able to do. There are effectively no limits to what can be done when you’re more or less writing the universe from scratch. Having said that if Spider-Man movies have taught us anything (and I think we can all agree they have) it’s that with great power comes great responsibility. So yes you can do anything, but the truth is your functions at least shouldn’t do everything. When you’re writing a new function make sure it’s only doing what its name implies. Take the following example:

function setHipsterEBikeSpeed( speed ) {
  brewHipsterCoffee( “Double shot decafe soy latte” );
  if ( speed < 20 ) {
    return “Low and slow”;
  { else if ( speed < 40 ) {
    return “Fast and furious”;
  } else {
    return “Might be safer to walk”;
  }
}

The name of the function implies we are going to use it to set the speed of a hipster e-bike, and while that is true it’s also brewing a hipster coffee at the same time, despite not making that clear in the name.

Ideally, the best approach here is to not brew coffee while setting the speed of a hipster ebike (this is true in life and code), as functions are best to be unitaskers the vast majority of the time. If for some reason your function needs to do a combination of two or more things then make sure it’s descriptive enough to communicate what it will do. Other developers will thank you for it.