Automating dead code cleanup

Meta’s Systematic Code and Asset Removal Framework (SCARF) has a subsystem for identifying and removing dead code. SCARF combines static and dynamic analysis of programs to detect dead code from bo…

Will Shackleton
9 min readintermediate
--
View Original

Overview

The article discusses Meta's Systematic Code and Asset Removal Framework (SCARF), which automates the identification and removal of dead code through a combination of static and dynamic analysis. It highlights the benefits of SCARF in improving code quality, reducing developer costs, and facilitating the deprecation process for products.

What You'll Learn

1

How to leverage SCARF for automated dead code removal

2

Why combining static and dynamic analysis is crucial for accurate code cleanup

3

When to apply dependency graph analysis to identify dead code

Prerequisites & Requirements

  • Understanding of static and dynamic code analysis concepts
  • Familiarity with Glean for extracting code dependency graphs(optional)

Key Questions Answered

How does SCARF automate the removal of dead code?
SCARF automates dead code removal by using a combination of static, runtime, and application analysis to identify unreachable code. It generates change requests for code deletion, improving system quality and unblocking unused data removal when dead code references data assets.
What is the impact of SCARF on codebases at Meta?
SCARF has analyzed hundreds of millions of lines of code and has automatically deleted over 100 million lines of code through more than 370,000 change requests, significantly improving code quality and reducing maintenance overhead.
What are the challenges in dead code removal with SCARF?
While SCARF accelerates dead code removal, it does not fully address issues related to interconnectivity and the complexity of accurately identifying all types of code usage, which can lead to false positives or negatives during automated deletions.

Key Statistics & Figures

Lines of code deleted
100 million
Over 370,000 change requests have been generated by SCARF.
Increase in dead code removal
50%
Transitioning from analyzing individual symbols to the entire dependency graph led to this increase in coverage.

Technologies & Tools

Tool
Glean
Used for extracting code dependency graphs from compilers.
Tool
Codemodservice
Automates code changes at scale, including the creation of change requests for dead code removal.

Key Actionable Insights

1
Implement SCARF to automate the identification and removal of dead code in your projects.
Using SCARF can significantly reduce the manual effort required for code maintenance and improve overall code quality by ensuring that unused code is systematically cleaned up.
2
Regularly update and fine-tune the dependency graph used by SCARF to enhance its accuracy.
Maintaining an accurate dependency graph allows SCARF to make better decisions about which code can be safely removed, thus minimizing the risk of accidental deletions.

Common Pitfalls

1
Relying solely on static analysis can lead to incorrect assumptions about code usage.
Static analysis may miss dynamic references or runtime features that affect code dependencies, leading to potential false positives in dead code identification.

Related Concepts

Static And Dynamic Code Analysis
Dependency Graphs
Automated Code Refactoring
Software Maintenance Best Practices