How To Safely Delete Legacy Code

21 Mar, 2018

Why

Imagine you have a function being called in several places and you want to reduce its usage or remove this function completely.

Let's say, you have a function `foo` you want to get rid of, and several different modules with its usage:

// foo.js
module.export = function () {
  console.log('foo')
}

// moduleA.js
if (conditionA) {
  foo()
}

// moduleB.js
if (conditionB) {
  foo()
}

// ...
// moduleX.js
if (conditionX) {
  foo()
}

You don't really know when conditionA, conditionB, and conditionX happens, but you're sure they will be executed within several hours in production.

How

The idea is to log how often the function gets entered. For example, send a tracking event with the function caller to your analytics system and wait for several hours.

// foo.js
module.export = function (caller) {
  tracker.send({ target: 'foo', data: caller })
  console.log('foo')
}

// moduleX.js
if (conditionX) {
  foo('moduleX')
}

Now you can see in your analytics dashboard the real usage of this function, and decision on its removal should be easier.

References