she swears <i>geek</i> is a term of endearment

Duck Typing in Objective C / Cocoa

July 15th, 2009 Rusty

I discovered, quite by accident, that Objective C supports a feature I have been yearning for in .net / C#.  While dynamic languages such as Ruby and Python (I’m no expert and could be wrong but I believe this to be true) allow you to pass very loosely typed objects around and swap one type for another, strongly typed languages usually do not allow this.  DotNet takes it a step further and won’t even allow you to inherit and override unless the author of the subclass explicitly marked the property or method as virtual.  This rears its ugly head when you try to mock for unit testing, a very valuable practice, and it won’t let you because the class you intend to mock was not littered with virtual decoration.  What this means to me is that I will, from now on, always mark my props and methods virtual.  That’s just ugly and unnecessary.  I would have preferred sealed to have been the exception and virtual to be the default.  Oh well.

If it quacks like a duck…

In objective c, take the following class

@interface Lead : NSObject {

    NSString *nameFirst;

}

 

@property (retain, nonatomic) NSString *nameFirst;

 

@end

The implementation is irrelevant .  If I have a consumer of this class like so:

@class Lead;

 

@interface StateManager : NSObject {

    Lead *currentLead;

}

 

@property (retain, nonatomic) Lead *currentLead;

…and let’s say I did something with the nameFirst method in my StateManager.

I could do this

@interface Giraffe : NSObject {

    NSString *nameFirst;

}

@property (retain, nonatomic) NSString *nameFirst;

@end

and pass it in to the StateManager

Giraffe *giraffe = [Giraffe new];

[giraffe setNameFirst:[textFieldName text]];

 

StateManager *stateManger = [StateManager getSingleton];

[stateManger setCurrentLead:giraffe];

Other than a compiler warning, all is well.  No errors will occur.  Iff you want to get rid of the compiler warning, cast the giraffe

[stateManger setCurrentLead:(Lead *)giraffe];

Which, apparently, in Objective C is more of a, “yes, I know that’s supposed to be a Lead instance,” rather than any real cast.

So, in review, you have object oriented features but not strongly typed restrictions.  This makes the opportunities endless and probably scares the hell out of some paranoid security software engineers but I’d rather have open frameworks and defensive programming than defensive frameworks and less power and flexibility.

JQuery Error troubleshooting

July 9th, 2009 Rusty

As is usually the case with javascript errors, a recent error I encountered was difficult to track down to its source.  Firebug was kind enough to indicate that there was a bug, but the message and line at which it occurred was not very helpful.  The exception was thrown from deep inside the minified jQuery library.

uncaught exception: [Exception… "Could not convert JavaScript argument arg 0 [nsIDOMViewCSS.getComputedStyle]" nsresult: "0×80570009 (NS_ERROR_XPC_BAD_CONVERT_JS)" location: "JS frame :: …/jquery-1.3.2.min.js :: anonymous :: line 12" data: no]

unhelpful-error

Not very helpful!

I’ve seen this exception before and Googling it brought me a number of exceptions related to dimensions manipulations.  However, nothing concrete was evident. 

I’m curious to know if there is some way to bring the stack trace forward from firebug as I see several indications that this is possible.

Another opportunity is to set FireBug to “break on all Errors” under Script > Options.  This can be tricky because you’ll find it breaking on quite a number of calls inside jQuery.  I found that this did, indeed, break on the line that was causing the error in MY code but it was not easy to click through the other errors to find it. 

More Info please Javascript Detail in FireBug

strict-warningsA useful option is to increase the noise level for FireBug.  A big warning indicates that you shouldn’t leave this option on all the time.  However, its very useful when you need it.

Under Console, choose Options > Strict Warnings.

This will increase the information that FireBug reports on.

You can now look at the messages preceding the non-helpful message to see if, perhaps, something is leading up to the problem.

more-firebug-error-info

That’s more like it!  Now I can see that, right before the JSFrame Exception, I had another warning in my own lobrary: reference to undefined property this.items[this.selectedIdx]

I added one short line that checked the length of a selected object so that it could exit if it was not there and my error went away.

FireBug is certainly the tool of the decade for web programming and I have no idea what this world would be like without itl