Marcel
Marcel

Reputation: 502

How to check if constant is used in project

I have a big project with many Objective C and Swift files. There's a Constants.h file which contains many constants with #define. It looks like this:

#define kConstant1  @"constant1"
#define kConstant2  @"constant2"
#define kConstant3  @"constant3"
#define kConstant4  @"constant4"
...

In the project there are many files (Swift and Objective C) which use these constants. Unfortunately there are also constants which aren't used by any file in the code, so they are unused variables. I want to find out which constants are used and which are not, to delete the constants which aren't used. There are about 3000 constants in this file so it would take too long to search through the code manually for each constant.

Is there any other way to find out which #define variable is used by the code?

Upvotes: 1

Views: 334

Answers (1)

Nima Yousefi
Nima Yousefi

Reputation: 817

Three suggestions:

  1. Download JetBrains AppCode and try running your code through its diagnostics. AppCode tools might find the unused constants.

  2. Write a script to go through each file and look check for the presence of the constants. If a constant is found, mark it as found. When all the files are checked, whatever isn't marked can be deleted. You can write this script in anything you want -- bash, ruby, python, swift, etc -- and you can either hard code the constants in or extract them from the Constants.h file.

  3. Comment or remove each constant one by one and build the project. The compiler will throw an error if one of the constants you removed is used in the project.

Unfortunately, there is no magic bullet here. This is fundamentally a time-consuming process. :(

Upvotes: 1

Related Questions