Sample 2 - Explore Existing Methods in Workspaces

Frequently during development you encounter existing methods that you don't understand and you are tasked with either fixing it, reusing it, enhancing it or explaining it. Faced with this, you proceed to study the method, study uses of the method, perhaps using debugging aids such as NSlog.

ActiveDeveloper Workspaces provides another (more productive) approach.

We show how to use a Workspace to help understand a piece of existing Objective-C.

Example:

- (void)splitLines:(NSString *)string;
{
unsigned stringLength = [string length];
NSRange lineRange = NSMakeRange( 0, 0);
NSRange searchRange = NSMakeRange( 0, 0);

   while (searchRange.location<stringLength) {
     [string getLineStart:nil end:&searchRange.location contentsEnd:nil forRange: searchRange];
     lineRange.length = searchRange.location - lineRange.location;
     [arrayOfLines addObject:[string substringWithRange:lineRange]];
     lineRange.location = searchRange.location;
   }
}

Don't approach a Workspace like a C level debugger where you set breakpoints and step through the methods line by line. In Workspaces, the approach is to dissect the code into small parts and execute them to see what the various methods return. To leverage the capability for incremental development and to use the inspector to traverse around and drill down into the resulting object.

This lets you cut out the pieces that you don't understand, observe them in isolation and when you understand the separate pieces, recombine them and if necessary execute them and observe their combined behavior. You wouldn't be able to do that in a traditional C level debugger.

Start ActiveDeveloper

Start ActiveDeveloper, select ActiveAppKitExplorer, Press Start, dismiss the info panel and open a new Workspace (menu:"Source Editors:New Workspace"). (Please read Manual1 first if you are not yet familiar with using an ActiveDeveloper Workspace).

Prepare the Method for a Workspace

Since we are isolating a method from its class we may need to provide input, output and instance variables. In our example we need to provide an input variable to represent the string parameter and a variable to represent the instance variable arrayOfLines. If your code refer to classes outside of the Foundation or AppKit, you need to add #import statements for these classes in the top pane of the Workspace.

The following snippet illustrates how we can rearrange the code. Note that we are not changing the core code only the manner in which it is called. Here string is used to represent the string parameter and array represents the arrayOfLines instance variable. We need no import statements in this example because it does not reference any classes outside of the Cocoa Framework.

//the lack of indentation is to allow copy/paste from HTML to a Workspace

id string = @"Bruce\nBill\nBeate\nBenny"; // workspace variables
id array = [NSMutableArray array];

unsigned stringLength = [string length];
NSRange lineRange = NSMakeRange( 0, 0);
NSRange searchRange = NSMakeRange( 0, 0);

while (searchRange.location<stringLength) {
[string getLineStart:nil end:&searchRange.location contentsEnd:nil forRange: searchRange];
lineRange.length = searchRange.location - lineRange.location;
[array addObject:[string substringWithRange:lineRange]];
lineRange.location = searchRange.location;
}

return array; // inspect - and make sure this is last line of selection

Continuous testing

Given the dynamic nature of the Workspace you can repeatedly evaluate your code, gone is the need to quit the debugger and restart when we step over the mark. In effect you have quick ad hoc testing at your finger tips. It is easy to test methods with various data, you can either edit directly in the Workspace or read data from a file e.g.

string = [NSString stringWithContentsOfFile: @"myfile"];

Spot Checking

You can insert return statements to do spot checks on values, for example after

NSRange searchRange = NSMakeRange( 0, 0);

insert

return array;

Highlight all statements from the top and down until return array; and Press Inspect.
The runtime Object Browser will appear displaying the array - as it looks half way down the method.

Remember that ActiveDeveloper is optimized to work at the object level - with Objective-C objects.
Try to replace return array; with return stringLength; make the same selection again and Press Evaluate.

The inspector doesn't appear and compiler error messages are written to the Transcript. This behavior is related to the fact that Cocoa entities such as NSRange, NSSize and NSPoint are in fact defined as C structs of floats, they are NOT objects. If you want to display or inspect these entities, you need to present them as objects by using the following macros

return NSStringFromRange (searchRange);
return NSStringFromSize (aSize);
return NSStringFromPoint (aPoint);

In the case of int, unsigned etc. use

return [NSNumber numberWithInt: anInt];

Closing Comments

The size of this Sample is testimony to how simple it is to prepare code for workspace investigation. Provide any required substitute variables for the core code and any import statements. You are now free to isolate statements, rearrange them to have the various variables correctly assigned and use return statements to present the result for inspection.



Home News Jobs Products Download Purchase Consulting Contact Us