briisk/briisk-javascript

Functions instead of methods

Closed this issue · 1 comments

If the method is not manipulating an instance of a class (if there is no this keyword in the method) then this is a function that should be outside of the class. Why? Because this method is not operating on the object, it's independent. It will be possible to use it in other places of the application. Also, if it is a public function, testing will be easier.

// bad
class SomeClass {
  addItem(arr, item) {
    return arr.concat(item);
  }
 ...
}

// good 
class SomeClass {
 ...
}

function addItem(arr, item) {
  return arr.concat(item);
}

I woulda add to this definition that if the method is not manipulating an instance of a class then this is a function that should be outside of the class.