IntelliJ IDEA
Software Development
Code Optimization
Unused Code
Programming Tips

How to use IntelliJ IDEA to find all unused code?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

IntelliJ IDEA, developed by JetBrains, is one of the most comprehensive IDEs for software development, particularly popular among Java developers. One of its powerful features is its ability to identify and highlight unused code, which not only helps in cleaning up the codebase but also in enhancing the overall code quality and maintainability.

Understanding Unused Code

Unused code refers to any part of the code that is no longer in use or is not reachable during the runtime, including unused variables, methods, classes, and imports. This can occur for several reasons such as legacy code, refactoring, or changes in requirements. Leaving unused code in the project can lead to confusion, increased maintenance costs, and potential errors.

How to Use IntelliJ IDEA to Find Unused Code

1. Using Code Inspections

IntelliJ IDEA has a built-in feature called inspections which can automatically detect problems ranging from bugs, spelling problems, and also unused code.

Steps to Enable and Use Inspections for Unused Code:
  • Open Project Settings: Go to File > Settings (or IntelliJ IDEA > Preferences on macOS), then select Editor > Inspections.
  • Enable Unused Code Inspections: Look for the Java section (or any relevant section for the language you are using), expand it, and check options like Unused declaration and Unused symbol. Adjust the inspection's severity and options as needed.
  • Run Inspections: Right-click on any directory in the project explorer that you want to inspect, select Analyze > Inspect Code. You can choose the whole project or specific scopes.
  • Review Results: Inspections results will be displayed in a separate window, grouped by categories. Unused code entries will be found under suitable categories.

2. Analyzing Run-time Code Dependencies

IntelliJ IDEA Ultimate also supports detecting unused code based on run-time dependencies:

  • Run with Coverage: Start your application with coverage (found under Run > Run with Coverage). This will monitor which parts of the codebase are actually used during execution.
  • Analyze Results: After stopping the application, you can browse the coverage results, showing you parts of the code that have never been executed.

3. Usage of Advanced Plugins

For even deeper analysis, consider using additional plugins available through IntelliJ's plugins marketplace. Plugins like SonarLint can provide further insights and highlight dead code that might not be immediately obvious.

Best Practices for Removing Unused Code

  • Verify Before Removal: Always ensure that the code is genuinely unused. Sometimes, what seems like unused might be invoked via reflections or used externally.
  • Check Under Version Control: Before removing any parts of code, check if it's under version control. This will allow you to retrieve it if needed.
  • Refactor Progressively: Prefer gradual cleanup over massive code removals, especially in large projects.

Summary Table: Features and Steps

FeatureTool/MethodSteps or Methods InvolvedScope Tips
Basic CleaningCode Inspections1. Enable relevant inspections 2. Inspect Code 3. Review & ActUse project scope for comprehensive cleanup
Runtime AnalysisRun with Coverage1. Run application with coverage 2. Analyze non-executed codeBest for dynamic analysis
Plugin SupportThird-party Plugins1. Install Plugin(s) 2. Run Plugin-specific analysisSuitable for ongoing code quality monitoring

By routinely identifying and removing unused code, you can maintain a cleaner, more efficient codebase. IntelliJ IDEA provides robust tools and features to aid in this cleanup, making it an indispensable tool for developers seeking to improve their code quality and maintainability.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.