Dead code problem

apastuhov profile image Aleksey Pastuhov Updated on ・3 min read

Originally published at apastuhov.com on November 3, 2017

You work with the big code base and your project has hundreds or even thousands of files? I assume that you have dead code in your project. I want to tell you about the problem of dead code and how we can deal with it. But first, let's define what code can be called dead.

Dead code definition

Dead code is any code which will never be executed. It may be some condition, loop or any file which was simply created but wasn't used in your project.

Why is it a problem?

It is a problem because that code has no sense! You can drop it and everything will work as well. I want to mention base problems of dead code:

  • A newcomer will read it, and he/she would try to understand that code, or even use it.
  • It takes some space in your repository and working directory.
  • It would be interpolated by your IDE, so it will eat more memory than it needs.

And here is what will happen with your project if you have dead code.

Everything will die

Why is it a problem of JavaScript?

JavaScript is a very active programming language. There are billions of applications and plugins developers create every day. For example, NPM (package manager for Node JS applications) has more than 450 000 packages, see modulecounts! It is more than 1 TeraByte of code! I assume that one package has 2MB size. And I am sure that more than 25% of it - is a dead code... which is used by nobody.

You do not work with NodeJS? Are you front end developer? Good! I assume that your client changes his mind. :) And you will develop all the features he wants. But you will do it in a hurry, so you will not have any time to fix old code, or even drop it.

We always have some changes for you

Any existing solutions

If you Google it, you would find that everybody uses Google Closure, or WebPack. But I insist that it is not a real solution to the problem. That tools will just remove dead code for public scripts. But they do not tell you what code you should remove.

Solutions in other programming languages

Let's look at some other popular languages:

No comments.. JavaScript is an outsider.

What is an ideal solution

Heh.. we saw that there are some analogs for other languages. So let's imagine some ideal solution for JavaScript. It must detect:

  • Unreachable code
  • Oxbow code
  • Dead store
  • Dead files

Unreachable code

Permanent return statement.

function temp() {
    return 1;
    var a = 2; // Dead Code

Needless conditions.

function temp() {
    var a = 2;
    if (a > 3) {
         return 3; // Dead Code

Needless loops.

function temp() {
var a = 2;
while (false) {
return 3; // Dead Code

Oxbow code

function temp() {
// ...
function private_calculation() {
return 22;
// ...
this.calc = function () {
// Previously private_calculation was called here...
return 22;
// ...

Dead store

function func(a, b) {
var x;
var i = 300;
while (i--) {
x = a + b; // Dead store

Dead files

Just imagine that you have some file with code, but that file isn't used at all in your project: no import, no <script src="..."> link to that file.


Here I told you about JavaScript problem of dead code and how we can deal with it. For now, there is no solution which will tell us what code can be safely removed.

Write here your opinion - What features must have a tool for dead code? And I will update current post!

Update (Sep 2017)

Starting from version 59 of Chrome you can test your code usage coverage with dev-tools.


It is really good tool to define old/outdated/dead code, but it has one big issue:

It tells you how much code was used, versus how much was loaded.

To define unused code - you need to go through all features on your website via clicking on all the buttons on your UI.

Posted on Nov 4 '17 by:

apastuhov profile

Aleksey Pastuhov


Software Developer, open-source fan. On my blog, I write about frontend, backend, testing, and other IT stuff.


markdown guide