Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cleaning up Objective-C code

When working on complex problems, I find myself trying all sorts of solutions and, while doing my best to stay organized, the code can get quite messy. Objects may be changed and no longer be of use, while other times I may add snippets of code that do not end up being used by the program but are taking up space and possibly memory.

Aside from carefully reading through the program, are there ways of finding chunks of code that are not used by the program?

What are some tips you have found for cleaning up your program?

One small trick I found for checking that objects from the .h file are still used in the app and to check that they are properly deallocated/released is to use the "search all" function (cmd-shift-F) and search by the name of the object

like image 731
Jonah Avatar asked Aug 25 '09 15:08

Jonah


1 Answers

Here's an article about a few approaches to reporting code coverage in your application:

http://seriot.ch/blog.php?article=20080728

It's oriented toward Mac applications but mostly applies to iPhone stuff too (DTrace you can only use in the simulator)

As the article notes, this is a harder problem in objective-C than other languages since it's so easy to have a method that is called by performSelector, that static analysis would report as dead code even though it gets called (yes you can also do something similar in Javabut it's done far more rarely).

Probably the gcc warning flag is the best idea, along with careful examination of what it thinks are uncalled methods. Actually running every possible code path in an application is actually pretty hard, but if you have a smaller set of possibly functions to remove at least you can make choices more quickly so you wouldn't have to test every path...

EDIT: I should probably make clear that code coverage is a technique you can use to find "dead" code, which is what you were after

EDIT2 : Link is dead! I cannot find a cached version, and I can't remember it well enough to summarize more about what it contained.

like image 195
Kendall Helmstetter Gelner Avatar answered Oct 17 '22 09:10

Kendall Helmstetter Gelner