Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 760 Bytes

guard.md

File metadata and controls

29 lines (22 loc) · 760 Bytes

Guard clause

If there are any special conditions that a function has to deal with, such as checking the value of the input arguments, then put them at the top of the function.

For example, do this:

var merge = function (x, y) {
  if (x === undefined) return y;
  if (y === undefined) return x;
  // merge x and y here
}

Instead of this:

var merge = function (x, y) {
  if (x !== undefined) {
    if (y !== undefined) {
      // merge x and y here
    } else {
      return x;
  } else {  
    return y;
  }
}

References