How to Use Code Inventory to Remove Unused Code
Code Inventory of Azul Intelligence Cloud provides reports of code execution at the class/method level. Thanks to this reports, you can find the unused code in your projects and reduce the maintenance cost.
Finding Unused Code
A code base keeps evolving, and as new functionalities get added, some methods are no longer used. In a healthy project with a high unit test coverage, it isn’t always clear which parts of a project became obsolete.
Detection by Static Code Analyzers
Static code analyzer plugins in your IDE and CI/CD tools often struggle to identify truly unused code, as methods and classes may still be referenced in unit tests. Even if a piece of code isn’t used in production or during normal execution, it may still be considered as used by the analyzer due to its presence in test suites. As a result, developers may miss opportunities to refactor or remove unused code, leading to increased maintenance costs and complexity.
For instance, test coverage reports generated by Jacoco provide valuable insights, but they lack the difference between the code that gets called by tests versus the code that is being used once the application is deployed and used.
Detection by Azul Intelligence Cloud’s Code Inventory
Compared to static code analyzers, Intelligence Cloud knows what code gets executed in your production environment. It’s able to detect public classes that, for instance, during other analysis were considered "live" because they get called by a test, but are never called in the production environment.
Other examples that will only get discovered in production:
-
Feature flags that are no longer used.
-
Specific use cases of the code which were used years ago, but are no longer needed.
-
Switch/Case and If/Else logic that never happens.
To effectively identify and eliminate unused code, a more nuanced approach is needed that takes into account the different contexts in which your code is executed. By combining Code Inventory reports with other tools and techniques, you gain a clearer understanding of what code is truly unused and make informed decisions about how to refactor or remove it.
Using Code Inventory in QA or Production Environments
Code Inventory can be used in QA and testing environments to find unused code. But as only the production environment has the actual business load, and the Intelligence Cloud Agent causes a very minimal overhead, we advise collecting the data from your production system.
By using a good AppEnv to separate the runs, you can identify the code that doesn’t run in production. In the end, you want to be able to identify and seperate three groups:
-
All the code you have.
-
The code which is called during tests.
-
The code which you run in production.
Finding Unused Code
-
Use the Intelligence Cloud Web UI or API to find the code that is marked as unused.
-
Your development team will probably already recognize parts of the code that they were suspicious about.
-
Mark these unused methods as
@Deprecated
as a first step. -
Keep monitoring the code and follow up if the deprecated code gets flagged by other methods that are still used.
-
Change the
@Deprecated
methods to@Deprecated(forRemoval = true)
and make sure your builds and tests don’t fail. -
Repeat this flow until no more unused code gets found.
Note
|
Depending on your release cycle, the duration of this flow may vary. Let your development team decide on the best approach to go from one step to the next one. |
Integration With Moderne OpenRewrite
Moderne is a software vendor that helps enterprises to safely refactor, secure, and migrate code across multiple repos and billions of lines of code by leveraging OpenRewrite, which is an open source software auto-refactoring tool (OSS project). The Moderne Platform can use Code Inventory’s data to automatically tag, flag and eventually remove unused and dead code at scale, across multiple repositories.
Moderne leverages OpenRewrite to perform semantic analysis and automated refactoring of code using search and transformation recipes, which are expert, rules-based programs that enable accurate, automated code changes. Typically, these changes are implemented in phases:
-
Moderne annotates all unused and dead code identified by Code Inventory for deprecation as a warning to the development team.
-
Moderne flags all unused and dead code for removal (i.e., forRemoval=true) to indicate to developers the code should no longer be used/referenced. If Code Inventory discovers that previously flagged unused and dead code is being run, Moderne will automatically remove all annotations
-
Moderne removes all references to the unused and dead code, which can help identify unit tests that may fail, routines that may break, etc.
-
Moderne automatically deletes all unused and dead code from the application’s code repository.
Note
|
How long Code Inventory monitors your applications, as well as the timing of each phase in the process is controlled by you. |
For more information on Azul’s partnership with Moderne, click here.