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?
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.
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.
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.
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.
Despite this question has already got an accepted answer, I think it's worth mentioning that
UglifyJS2
does remove dead code
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 });
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With