How to Identify Dead Code in a Legacy Java Project?

0
1
Asked By TechSavvy77 On

Hey everyone! I recently joined a team as a senior developer, and we're diving into a sizable Java codebase with around 500,000 lines of code split across 30 services, which was developed by different contractors over the years. We're trying to make sense of the existing logic, but one of our biggest challenges is identifying dead code. We're often wasting time following code paths that lead to unused functionality due to constant conditions in if statements or similar scenarios. It would really help us to find out if a method has been called recently, like within the last month, to boost our productivity.

I've come across some tools and suggestions:

- **Jacoco**: It seems perfect for my needs, but I've been warned that it's too heavy for production. Makes sense since it wasn't designed for that.
- **JFR**: This tool mainly focuses on profiling, and from what I've seen, it doesn't provide the insights I require.
- **AspectJ**: This might be the best option, but I'd need to explicitly define which methods to instrument, and that could be a challenge. I'm thinking of setting up a low-key QA PoC for this to gather some insights without disrupting our workflow.

Given that we also have a Datadog subscription, any suggestions on how to tackle this would be super helpful!

5 Answers

Answered By CodeJunkie42 On

While I haven't done this exact thing myself, I've read about how Facebook automates their dead code cleanup. It sounds like they have a pretty solid system in place, so checking out their engineering blog could give you some solid insights: [Facebook's Blog](https://engineering.fb.com/2023/10/24/data-infrastructure/automating-dead-code-cleanup/).

Answered By RealDevTalk On

Honestly, with a project size like yours, if it's really messy, you might just feel overwhelmed. Sometimes a clean slate can be more beneficial than trying to salvage everything. But proceed with caution of course!

Answered By CloudGuru99 On

You might want to look into Azul Intelligence Cloud; they have tools specifically designed for managing and identifying dead code. Here's a link to their product page: [Azul](https://www.azul.com/products/components/code-inventory/). That could save you some time and hassle!

Answered By JavaNinja88 On

I've got a workaround for using Jacoco! You could launch an additional instance of your application with Jacoco enabled and send a small percentage of your traffic there—like 1-5%. It’s a bit clunky, but it gets the job done. You could also consider AWS X-Ray for sampling purposes. Just a thought!

DevExploration -

That's a smart way to integrate it! I hadn't thought about selectively routing traffic like that.

Answered By CleanupMaster On

I've tackled this before, and it's been a bit of a balancing act. One strategy is to keep a detailed document of what you've identified as unused code as you find it, and make it part of your process to check these flows whenever you touch legacy code. Another method I've seen is being aggressive—removing code you suspect is dead and monitoring metrics closely. It can be risky, but if you have good telemetry, it can give you an edge.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.