Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect dead or unused code in Angular?

I and my team are working on an angular application from more than a year now. As part of our code refactoring process we want to delete unused or dead code from our repo.

Is there any technique or tool which automatically detects this unused code from our repo, the it would be helpful for us to it instead of wasting a lot of time and efforts detecting them manually?

like image 445
Abdus Sattar Avatar asked Nov 11 '18 09:11

Abdus Sattar


People also ask

How do you find the dead code?

The quickest way to find dead code is to use a good IDE. Delete unused code and unneeded files. In the case of an unnecessary class, Inline Class or Collapse Hierarchy can be applied if a subclass or superclass is used. To remove unneeded parameters, use Remove Parameter.

How do I find my unused code in VS code?

It is currently not possible to detect unused public methods in VSCode (you can check for updates here). However, if it's a private method, you can mark it as private and VSCode is able to look for whether it is used or not within the scope (although do remember: methods used in the HTML template are public).

Does webpack remove unused code?

To achieve this you can use the UglifyJS Plugin. With this setup, webpack will detect unused code and mark it as such however UglifyJS will actually cleanup that code and eliminate it from the bundle.


2 Answers

Thats very simple. if you run your application with " ng serve --aot " cli command instead of " ng serve " , it shows you a hole number of these dead codes(like when you leave " ng build --prod " command).

like image 171
Mostafa Saadatnia Avatar answered Sep 20 '22 16:09

Mostafa Saadatnia


For VS Code you can use this: https://eslint.org/docs/rules/no-unused-vars What it does: Variables that are declared and not used anywhere in the code are most likely an error due to incomplete refactoring. Such variables take up space in the code and can lead to confusion by readers.

There isn't something that delete the code for you, but I think this is a good extension to start with.

If you use sublime there is nothing since is just a text editor

like image 21
Jacopo Sciampi Avatar answered Sep 20 '22 16:09

Jacopo Sciampi