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.

Categories
Geekpulp

Simplify conditional statements

Consider the following code:

function danceLikeNoOnesWatching( person ) {
  if ( person.danceMoves === “amazing” ) {
    return false;
  } else {
    return true;
  }
}

Looks good right? It’s logical and easy to understand, just your standard if statement. All true, but over the course of an entire project you’ll make a lot of considtional statements like this and using 5 lines of code when 1 will do can create some serious size increases to your code. To reduce the codes size, just return the output of the conditional like this:

function danceLikeNoOnesWatching( person ) {
  return person.danceMoves === “amazing”;
}

Simpler, still easy to understand and now light weight.

Categories
Geekpulp

Avoid abbreviating variables

Do you know what’s super annoying when reading someone else code? Abbreviated variable or function names. Until late 2017 I hadn’t done any coding in years so I was rusty as all hell, and truthfully I still am. One thing I noticed when looking at other peoples code was how it made it far slower to read and learn from when all the variables and function names are abbreviated. Yes, it might be faster for the person writing the code at the time, but anyone else exposed to that code carries a bit more mental load to decipher it. Take this simple code for example:

var prsn = {
  n: "Emma",
  a: 7,
  loc: "Palmerston North",
  frnds: [ "Ella", "Megan", "Tom" ]
};

prsn.prntFrnds = function() {
  this.frnds.forEach( function( frnd ) {
    console.log( frnd );
} );
}

Now compare this to the same function without abbreviated variables and function names:

var person = {
  name: "Emma",
  age: 7,
  location: "Palmerston North",
  friends: [ "Ella", "Megan", "Tom" ]
};

person.printFriends = function() {
  this.friends.forEach( function( friend ) {
    console.log( friend );
} );
}

Both examples are technically the same. However, it’s far easier to understand the second example at a glance, especially for a beginner who’s already looking at something relatively unfamiliar.

So the next time you catch yourself writing a block of code just remember to take a second for the next person reading it. Your code should be as descriptive as possible, so even a junior developer understand what it’s for at a glance… or someone who hasn’t seen the code in a long while… chances are that someone will be you at some point.