Efficiently Eliminate Dead and Duplicate Code in iOS Projects
Written on
Chapter 1: Introduction to Code Maintenance
Once a project is launched, the need often arises to introduce new features, conduct A/B testing, or eliminate outdated functionalities. These modifications are part of the project’s evolution, categorized under adaptive and perfective maintenance. Additionally, bugs may require immediate attention, falling under corrective maintenance.
What ties these changes together? They all involve maintenance actions that modify the codebase, potentially adding or removing code segments. However, these interventions can lead to two undesirable code smells:
- Dead Code: Segments of code that are no longer executable within the codebase due to inaccessibility.
- Duplicate Code: Nearly identical code snippets appearing across various files or lines, often a result of copy-pasting.
The presence of dead code leads to a cluttered codebase, leaving developers uncertain about the purpose of certain code fragments. Duplicate code, on the other hand, poses a more significant challenge, complicating maintenance. Bugs in duplicated code require fixes across all instances, making development cumbersome.
Today, I will introduce two tools that assist in detecting and eliminating these code smells in a semi-automated manner.
8 Best OBD2 apps in 2023 (Free included) - This video reviews some of the best OBD2 applications available in 2023, highlighting both free and premium options.
Chapter 2: Utilizing Periphery for Dead Code Detection
Photo from GitHub.
Periphery is a robust tool designed to identify unused code within applications. It effectively detects not just unused functions, but also structs, enums, and even parameters within function signatures that remain unutilized.
Installation Process
To install Periphery, use Homebrew with the following commands:
brew tap peripheryapp/periphery
brew install periphery
The first command adds Periphery’s repository, while the second installs the tool.
Configuration
After installation, Periphery requires configuration. Initiate the process by running:
periphery scan --verbose --setup
The --verbose flag ensures that a YAML configuration file is displayed after setup, allowing you to copy it into a .periphery.yaml file for future use. If you have previously set up the configuration, you can skip the --setup flag in subsequent runs.
Usage
Once configured, you can execute the tool using:
periphery scan
I ran Periphery on a sample project, and here’s a glimpse of the output:
The tool’s output allows us to scrutinize the codebase and verify the accuracy of identified dead code. Most of the time, the tool provides reliable results, enabling us to safely remove unused segments. Rare false positives may occur, primarily due to protocol extension implementations or internal functions that need better testing.
In summary, having a tool like Periphery is essential for reducing code size and eliminating lingering unused code from the codebase.
Chapter 3: Detecting Duplicate Code with CPD
Photo from PMD’s docs.
The Copy-Paste Detector (CPD) is a tool that identifies duplicated code in your codebase and is part of the PMD suite.
Installation Steps
Since CPD is Java-based, follow these steps:
- Install Java 8 for Mac.
- Download CPD from the GitHub release page.
- Unzip the downloaded archive.
- Move the lib folder from the unzipped archive to /usr/local/.
- Place the bin folder from the archive into /usr/local/.
Rename the run.sh file to something more descriptive.
Usage
To utilize CPD, navigate to your Swift project's root folder and execute:
run.sh cpd --minimum-tokens <N> --files . --language swift
Here, --minimum-tokens specifies the minimum number of identical tokens for code to be considered duplicated. A typical range for this value is between 20 and 50, allowing for effective detection of reusable code segments.
The tool can analyze recursively through specified folders, making it a comprehensive solution for identifying duplicate code.
I ran CPD on a test project, and the results were as follows:
Upon receiving the output, you can navigate to the relevant sections and decide to refactor the code into reusable functions.
Chapter 4: Conclusion
In this article, we examined two effective tools for detecting and eliminating dead and duplicate code in our codebases. Utilizing these tools helps maintain a clean development environment by reducing stale code fragments and enhancing code quality.
Fortunately, introducing dead and duplicate code is not a daily occurrence. These tools can be employed periodically, perhaps every 3 to 6 months. A wise practice is to allocate time for housekeeping sprints, allowing the team to refactor code, address backlog tasks, and run these tools to remove code smells.