I should have committed this lesson to memory years ago after some kind of time wasting mix-up between a test script called “test” and the UNIX test command (which, in hindsight, may have been more of a path issue, but the other lesson was to not work on assignments at 4 am the night before they’re due), but here we go again:
Never ever ever use throwaway variable names.
Case in point: inspired by my earlier thoughts on desktop application development, I decided to try Cocoa programming. I’m just at the poking-around stage right now (I miss MSDN’s level of documentation already, even when it’s wrong), but I lost an hour or two on this gem:
- (void) loadPicture:(NSString *) filename
{
img = [[NSImage alloc] initWithContentsOfFile: filename];
if(img)
{
[pic setImageScaling: NSScaleProportionally];
[pic setImage: img];
[pic setNeedsDisplay:YES];
}
else
{
NSLog(@”img failed”);
}
}
“pic” was an NSImageView control, and there weren’t any errors, but the damned thing wouldn’t ever change its picture. Further debugging showed that messages weren’t going to it at all:
if([pic isEditable]) NSLog(@”YES”);
[pic setEditable: NO];
if([pic isEditable]) NSLog(@”YES”);
That yielded two consecutive YES entries in the log.
In the end, renaming “pic” to something more meaningful solved the problem. I’ve yet to find a Cocoa or Objective-C reserved words list, but I suspect pic is on it. Of course, meaningful names will avoid a good chunk of the need for such a list anyway.
Technorati Tags: Cocoa