Find unused code in Swift

Caomus TP
te<h @TDG
2 min readJul 11, 2023

--

Due to the fact that there's a lot of code that is not used but still exists in the project we are working on, we figured out how to manage these codes. And according to some research, there are third-party development frameworks to find all the unused code, which is the periphery.

Periphery

Photo from https://github.com/peripheryapp/periphery

periphery is a tool to identify unused code in our app.
It can also find unused classes, structs, protocols, functions, properties, constructors, enums, type aliases, and associated types that are not used.

*Peripher y only support swift ≥ 5.8

Configuration

To use it, first we need to install the periphery using brew

brew install peripheryapp/periphery/periphery

Once installed, install Cocoapods next.

pod 'Periphery'

How To Use

For basic use, we will use the command periphery scan --setup
There will be simple questions to answer in order to configure the .periphery.yml file. But we can fix it later as well.

Example .periphery.yml. when there are multiple projects in a workspace

workspace: Demo.xcworkspace
retain_assign_only_properties: true
retain_unused_protocol_func_params: true
retain_objc_accessible: true
retain_public: true
external_encodable_protocols: true
schemes:
- Debug
targets:
- Module1
- Module2
verbose: false
skip_build: false
strict: false
format: xcode

Once the setup configuration is complete, you can run the command periphery scan and wait for the results.

Example result format: xcode

That’s it; now we can know the unused code of our project. And we go in to edit or delete it. Our project will be cleaner and less

Thank you, everyone, for coming to the end here. Next time, I will show you how to integrate with Xcode. We don’t need to run the command at all. We just pressed run from the target, and the results are already out.

see ya. 🚀 🥳 🎉

#Periphery #iOS #Swift #xcode

--

--

Caomus TP
te<h @TDG

• IOS Developer • Game Programmer • Unity • C++ • Ionic • Github >> https://github.com/caomus • Site >> https://icaomus.wordpress.com/