Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect unused methods and #import in Objective-C

After working a long time on an iPhone app, I realized that my code is quite dirty, containing several #import and methods that are not called or useful at all.

I would like to know if there's any compiler directive or way to detect those useless lines of code. Does Xcode have any tool to detect this?

like image 560
Hectoret Avatar asked Sep 21 '09 21:09

Hectoret


People also ask

How do you find the dead code?

The quickest way to find dead code is to use a good IDE. Delete unused code and unneeded files. In the case of an unnecessary class, Inline Class or Collapse Hierarchy can be applied if a subclass or superclass is used. To remove unneeded parameters, use Remove Parameter.

How do I get an unused code from VS code?

To find unused members with a Code Analysis Ruleset, from the Visual Studio menu select File -> New -> File… -> General -> Code Analysis Rule Set. Uncheck all the rules. There are many rules we don't care about right now – and some we probably won't ever care about.

How do I find unused methods in C#?

Right click on your solution and selection "Find Code Issues". One of the results is "Unused Symbols". This will show you classes, methods, etc., that aren't used.

How do I see unused methods in IntelliJ?

In newer versions of IntelliJ it's under Code -> Analyze Code -> Run Inspection by Name. Show activity on this post.


2 Answers

Xcode allows you to (un)check settings for specific compiler warnings that can warn you of some types of unused code. (Select the project in the source list and File > Get Info, then select the Build tab.) Here are a few (which show up for Clang and GCC 4.2 for me) which may be of interest:

  • Unused Functions
  • Unused Parameters
  • Unused Values

I don't see any options for detecting unused imports, but that is a bit simpler — the low-tech approach is just to comment out import statements until you get a compile error/warning.

Unused Objective-C methods are much more difficult to detect than unused C functions because messages are dispatched dynamically. A warning or error can tell you that you have a potential problem, but the lack of one doesn't guarantee you won't have runtime errors.


Edit: Another good way to detect (potentially) unused methods is to examine code coverage from actual executions. This is usually done in tandem with automated unit testing, but doesn't have to be.

This blog post is a decent introduction to unit testing and code coverage using Xcode. The section on gcov (which only works with code generated by GCC, by the way) explains how to get Xcode to build instrumented code that can record how often it has been executed. If you take an instrumented build of your app for a spin in the simulator, then run gcov on it, you can see what code was executed by using a tool like CoverStory (a fairly simplistic GUI) or lcov (Perl scripts to create HTML reports).

I use gcov and lcov for CHDataStructures.framework and auto-generate coverage reports after each SVN commit. Again, remember that it's unwise to treat executed coverage as a definitive measure of what code is "dead", but it can certainly help identify methods that you can investigate further.

Lastly, since you're trying to remove dead code, I think you'll find this SO question interesting as well:

  • Profiling Objective-C binary image size
like image 106
Quinn Taylor Avatar answered Oct 21 '22 14:10

Quinn Taylor


Appcode has a code inspection feature which finds unused imports and code.

like image 39
patrick-fitzgerald Avatar answered Oct 21 '22 16:10

patrick-fitzgerald