Exploring Unfamiliar Code
Lately I’ve been working on some projects written by other developers. I thought it would be useful to quickly summarize some techniques I use to get familiar with a code base I did not write or wrote a long time ago.
For this example I'm going to use source from the facebook three20 project http://github.com/facebook/three20 simply because it is popular and easily available.
Some of the techniques I use include:
1) Xcode Class Model
The first technique is to get an interactive class diagram using Xcode’s quick model feature under the Design > Class Model > Quick Model menu.
The diagram instantly tells you there there are a number of subclasses of TTViewController.

Not only can your drag classes around you can select the classes in the top left panel and get information on the variables and methods.
You can even double click the class or method in the panels and Xcode will open the implementation or header file.
2) Class Browser
A similar and less graphical technique is Xcode’s Class Browser under the Project > Class Browser menu or cmd-shift-C

This lets you select a flat list or hierarchical display of all the classes and protocols or just the ones in your project.
Again selecting a class on the left shows you the methods up top and selecting one of those will show the implementation in the main window.
3) Debugger
Having too many break points can be cumbersome but stepping through a couple of key methods can be very informative. For example, a break point in the viewDidLoad method of a view controller will let you step through it when that view controller is loaded and study how it is initialized.
4) Find in documentation and find implementation
When you find a symbol (variable or class) name that you want to learn more about, remember that cmd double click will open the declaration of the symbol while option double click will find the symbol in the installed documentation.
5) Open Quickly
When you want to open a file it can sometimes be hard to find it in the left hand side browser. That is when File > Open Quickly comes in really handy.

6) Find in Project
We’ve all searched for strings in current file and Edit > Find > Find in Project... is a useful extension to that. It will search for the given string in any file in your project.

7) Grep on the command line
The command line tool grep may seem redundant when you have Find in Project but sometimes it is useful to search for more advances searches. For example when you are looking for a common pattern that does not appear with another pattern. In the example I search for occurrences of TTTabItem that don't also have the word "init" on the same line.

8) Bookmarks
When you are bouncing around between different files trying to figure out their relationships Xcode’s bookmarks can come in really handy. You can create one with the Edit > Add To Bookmarks menu or cmd-D then when you want to go back to that particular place in that file you can find the book mark in the book marks group in the left panel, the View > Smart Groups > Bookmarks menu or by the cmd-shift-M key combination.

9) Write sample and test programs
And finally, once you have an idea of what the code does and have put the original project under source control it can be very beneficial to branch it and start modifying it. If you create little programs to test theories about how the code works you can very quickly find out how well you understand the code base.
These were some tricks I use. If you know how to improve them or have others let me know.



Post new comment