Codebases are growing over time (most of the times). They do also go after minor or major refactors. And regardless how careful or not is the team, there are often cases that there is some unused code left in the codebase.
In this article we will use the Periphery(https://github.com/peripheryapp/periphery) tool to detect unused code.
- Let’s create a project and name it UnusedCodeTest.
- Install through brew the periphery
1brew install peripheryapp/periphery/periphery
- Next let’s add an unused class in the target:
123class UnusedClass {}
- Also we add an unused method, a used method and a used method with an unused parameter:
123456789101112131415161718192021222324struct ContentView: View {var body: some View {VStack {Text("Hello, world!").padding()}.onAppear(perform: {methodCalled()methodUnusedParameter(a: 3, b: 4)})}func methodCalled() {}func methodUnCalled() {}func methodUnusedParameter(a: Int, b: Int) {_ = a * a}}
- Now let’s run the command:
1periphery scan --setup
- As a result we have a yml file with these details
12345project: UnusedCodeTest.xcodeprojschemes:- UnusedCodeTesttargets:- UnusedCodeTest - Now let’s see how good is the tool:
It found 3 out 3 issues! Not bad at all!