Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why uglifyjs doesn't remove dead code?

For instance, I have the following code:

if ("a" !== "a") {
    console.log('really?');
}

var a = 5;

Then I write uglifyjs code.js -o code.min.js. As a result, I have the following:

if("a"!=="a"){console.log("really?")}var a=5;

How do I enable removing the dead code inside the if-statement?

like image 912
user2991036 Avatar asked Aug 20 '15 04:08

user2991036


People also ask

Is it OK to delete dead code?

Deleting dead code is not a technical problem; it is a problem of mindset and culture. There is often the sense that if code is not doing anything, it has no effect, so it’s OK to leave it. It is worth keeping in mind that exactly the same reasoning also allows us to remove it: if it’s not doing anything, remove it.

Does static analysis really work to find dead code?

Sometimes dead code is genuinely unreachable, and static analysis can tell you this. The effectiveness of static analysis, however, depends on tools, language and architecture, but it’s a good start.

Is it OK to remove code that is not doing anything?

There is often the sense that if code is not doing anything, it has no effect, so it’s OK to leave it. It is worth keeping in mind that exactly the same reasoning also allows us to remove it: if it’s not doing anything, remove it. Let the version control system remember it for you.

Should superfluous code have been removed years ago?

They should have removed the superfluous code years before. There are sometimes very practical reasons why flags, records, etc. are repurposed rather than new ones added, but if it is at all possible to add without recycling, addition is preferred. There was no escalation process in the company for dealing with a rogue trading system.


1 Answers

Despite this question has already got an accepted answer, I think it's worth mentioning that

  1. UglifyJS2 does remove dead code

  2. To turn this feature on, you need to set appropriate option either in CLI (uglifyjs --compress unused,dead_code) or in the options object if you invoke uglify programmatically (uglify(compress: { unused: true, dead_code: true });).

like image 51
meandre Avatar answered Sep 27 '22 16:09

meandre