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

Supporting IE6 – How much does this cost?

June 30th, 2009 Rusty

While it may astound many developers that IE6 is still a popular browser, when you segment your audience with an industry heavy in compliance bureaucracy and political overhead, you find users who are not able to upgrade their office computers.  Their IT department is understaffed and conditioned to resist any innovation that might introduce more work for them.  Thus, you’re stuck supporting an ancient, inferior browser and you are forced to continue to code everything twice – once for modern, standards compliant browsers; once for IE6.  While this is an improvement over times past, its still a legacy behavior that costs real money to business.  How much?  That’s hard to quantify. 

Some deductive cost analysis

Let’s tally up the time it takes me to test a new feature against IE6.  This is an isolated development activity that does not take into account the cost of configuring and maintaining a virtual machine that hosts IE6.  It takes for granted the cost of software licensing necessary to keep an IE6 OS around and to run it.  I am using VMWare so hardware is negligible.  However, you could also include the cost of hardware for a machine that hosts IE6.  There are images available for virtual pc but these are getting harder to find and they expire regularly.  Microsoft wants us to move forward and they make it hard to hold back on upgrading IE6.  Nonetheless, businesses allow their IT departments to shackle their employees because anything that involves IT carries IT costs.  Its a viscous cycle of lock down followed by immobility but that’s outside the scope of my current point. 

I waited for 20 minutes while my IE6 Virtual Machine booted up.  In order to test against my development server, I have to run two Windows virtual machines simultaneously and that pretty much slows the system to a crawl.

To access my server from my IE6 machine, I had to reconfigure my network settings to use bridged networking.  This took another ten minutes before it would connect.  Though my settings were correct, I still couldn’t access the website on my dev server VM.  I had to close down my browsers and try again.  35 minutes into my activity, I am still trying to bring up the page to see how it displays!  Finally, the page loaded correctly. At first, I was very excited to see a perfect page.  Then I realized it was firefox.  I try to limit my risk variables when troubleshooting.  On to IE6, then.

I’ve gotten very good at fixing IE6 issues.  When I brought up the page, it was a disaster.  I quickly identified that my “overflow” was not working and Googled the solution: a fixed width on the overflow container.  jQuery then took care of the rest.  I love that library!  Finally, I had a png file that was being manipulated by the iepngfix behavior so I dumped that.  If I am going to support IE6, its buh bye to png transparency for now.  The pngfix was f’ing up the size of the img.  Sure, I could have specified a fixed height and width on the img tag but that’s so old school. 

About an hour per IE6 page

Sure, this is an isolated and unique event but I think its probably pretty close.  Today I was held up getting the environment running but the fixes were easy.  Tomorrow I won’t have to jump through hoops but the fixes may take longer.  I would be willing to bet that for each page that needs to support IE6, add an hour of work.  Have 100 pages to work through?  100 hours.  At a hip shot rate of $100/hr for average salaried developers and you see that IE6 gets expensive (that is a $10,000 non feature).  It also holds up progress and innovation and lowers moral but focus on what’s important: its expensive.  Again, I am just shooting from the hip on that cost, its most likely much, much higher.

I look forward to the day when I have a systems admin working for me who resists doing his job to keep software current and useful while at the same time refusing to let user serve themselves.  I will have years of pent up frustration to present him or her with.  I hope I am in a pleasant enough mood to find the levity in it.

Curl beeps at me and displays binary on Mac Terminal

June 5th, 2009 Rusty

I went through the very arduous, yet somehow rewarding, experience of building MySQL from scratch on my Mac.
This article explains how and why you might want to install mysql from source on mac os x.
I then finished, went to bed, and this morning decided to use my own Mac (rather than the family mac, ie: my wife’s), and found I had no MySQL. Drat!. Alright, second time is easier, right?

Beep, beep, beep, beep… FAIL

One of the steps required is to use curl to bring down the source.  The first time through, I entered the command as instructed

curl -O http://mysql.he.net/Downloads/MySQL-5.1/mysql-5.1.33.tar.gz

Strange, alien looking glyphs flash past the green themed terminal screen while the computer beeps repeatedly.  I had a hard time finding any reference as to why this might be happening.  I finally gave up, downloaded using Safari, copied manually, and skipped curl.  Today, I was determined to discover the cause.

In reviewing the options, I found an argument to send output to a file rather than the terminal window or STOUT.  That did the trick!  In addition, I added the “progress bar” argument so I had something to watch.

curl -0 http://mysql.he.net/Downloads/MySQL-5.1/mysql-5.1.35.tar.gz -o mysql-5.1.35.tar.gz -#

curl -0 http://mysql.he.net/Downloads/MySQL-5.1/mysql-5.1.35.tar.gz -o mysql-5.1.35.tar.gz -# 

I am now dangerous with curl.  Hope this helps some other Windows defector

Getting Bent Out of Shape Over Inversion of Control (IoC)

December 30th, 2008 Rusty

I find I often spend more time trying to choose between alternative frameworks then I do either identifying the need for one, or designing and implementing the solution.  For example, I spent quite a bit of time evaluating competing blog engines.  When I finally chose WordPress, the install took a fraction of the time that I spent researching various alternatives.  Anyone who has used WordPress can attest that there really is no learning curve to using it and it installs ready to blog.  The funny thing is, I knew before I had spent any time researching that WordPress was the most popular blog engine out there.   Why?  who cares?  Move on already.

Most Popular IoC Framework

It wasn’t nearly so cut and dry for IoC in dotnet.  I found quite a few resources evaluating and comparing various frameworks but nothing really conclusive.  Then I realized that most of the tech bloggers I follow tend to use the same tools that I find most valuable.  Much of the time its because they turn me on to them. 

I decided to narrow my search to my most respected bloggers.  I started out with Scott Hanselman but he’s off wandering about Africa and has just started looking into IoC himself (or hasn’t updated his captive audience on his progres in a while).  His blog, however, did lead me in the right direction.  He was kind enough to post a list of the current dotnet IoC projects that look attractive/viable.  Sometimes a list helps to get the Googling going.  A short while later, I was reading posts by Ayende Rahien.  He’s a somebody I’d like to buy a few rounds of shots for.  He’s helped me more than probably any other Microsoft technologist.  He got straight to the meat and stated that he prefers Windsor Container (because that is what he has used).  Good enough for me!  Ironically, that is what I was going to use when I started my research but didn’t want to "make a regrettable choice".  What lead me astray is that Microsoft has recently promoted their framework called Unity.  If you are the kind of developer or development shop that prefers supported software or you just lean toward Microsoft code, use Unity.  It will get the job done.  From my perspective, Windsor is mature, stable, simple and natively supports AOP. 

Generally Trust in People but Still Lock Your Doors

I trust people in general but I never leave valuables in my car.  With code, that strategy can be applied in this way: use what is most convenient to solve your immediate need but loosely couple.  The whole point of IoC is loose coupling.  I wish I had just committed to keeping my registration code abstracted from any consumer access jumped into Windsor rather than spent all day obsessing about which framework was better.  Blogging about it tonight took about an hour.  I should have my code finished in about fifteen minutes.  At least I feel good about my decision.

IE 7+ and extra space around elements

November 10th, 2008 Rusty

Used to be we developers had to develop at least two sites: 1 for Mozilla and 1 for Internet Explorer.  Netscape was the biggest PITA and I am happy that browser was retired.  Now with Firefox, Safari and Chrome all committing to supporting W3C standards, more browsers is a very good thing.  Internet Explorer has come a long way to supporting the same standards although Microsoft likes to keep their browser special

Where is that extra margin coming from?

Sometimes, when floating an element or affecting its layout in some other seemingly unrelated way, you might introduce a little strip of space in your layout that you certainly hadn’t intended.  The best way to zero in on the element that is causing the issue is to set background colors of participating elements, using css, to various random colors.  This will make your elements solid so you can identify whether the space is inside the "blue" div or actually its child "red" div. 

Use Internet Explorers "IE Developers Toolbar" to inspect the browser’s interpretation of your document while its running.  Its similar to what you get with FireFox’s FireBug extension except that FireBug is to IE Dev TB what a heat seeking, laser guided missile is to a 5 pump pellet gun.  Nonetheless, its very useful.  If you aren’t using firebug with firefox yet, go get it immediately …and then come back and finish reading this post. 

Using the toolbar, or background color manipulation, to find the element that has the margin or padding added, you are very close to where you need to be to fix it.  Try to avoid adding negative margins to fix a problem caused by IE weirdness.  Sometimes you have little else and as a last resort can get you on to more important problems.

Look Around a Bit

Once you found your offending element (or pair of elements) don’t become too sure of yourselfIf the problem isn’t present in Firefox, its probably something unusual.  More often than not, I find that extra margin between two elements, where margins are explicitly set to zero, can be the result of a bordering child in one of the elements setting margin and pushing the parent’s sibling awayIt is NOT supposed to do that but we’re talking about some pretty complicated rendering based on competing scripted rules and behaviors and sometimes one rule causes a little hiccup.  I almost always find that I can modify how I am spacing child elements so that FF and IE behave the same. 

I am proud to say that I don’t believe I have any browser specific "hacks" in my css at this time.  I have certainly had to make some adjustments to accommodate browser weirdnesses but these I can live with.

If you just downloaded FireBug, go mess with the Edit Html feature and the css real time modifications.  Soooo cool.  If you don’t have it yet, choose a career that does not involve web development.  You are only causing yourself unneeded pain :)

Objective C memory exception EXC_BAD_ACCESS likely over-zealous releasing

September 14th, 2008 Rusty

On this one, I’ll be short and sweet.  Read Apples memory management paper.

Objective C memory management, simplified - or - head, meet wall.  repeat.

Here are the golden rules that I’ve discovered to overcome the EXC_BAD_ACCESS monster.

  1. When you create a pointer, its just a pointer.  for example: NSString *someString;  There’s nothing there yet.  When you assign value, you have a pointer to some allocated memory space.  This is true in all programming languages, we just tend to forget as many modern languages abstract this away from us.
  2. If you reference something that you received from a method, don’t release it.  for example:
    NSArray* resultSet = [entropyRepository queryWithClass: classType];
    I didn’t allocate or create that array, its not mine to release.  When my pointer goes out of scope, it will decrement the reference count.  Whoever created it is ultimately responsible for its release.  It may be auto-released or the creator may have a dealloc method.  However, if you release it, somewhere down the line, the originator may try to access it and you’ll get "EXC_BAD_ACCESS".  Unfortunately, this error will occur sometime in the future, long after you’ve made your mistake.  This was happening to me when the autorelease pool was being processed and I’d already released that array when I was done with it.  Ooops.  There went a week of "head, meet wall.  repeat". 
  3. If you use "alloc", you need to release the referenced memory.  Basically, "alloc" is, "initialize this memory space for me."  If you don’t release it, it’ll just sit there, forever and ever…   You can opt to use autorelease or you can explicitly send a release message. 
  4. In addition to alloc, methods used to get a reference that "create" an object using a method whose name begins with “alloc” or “new” or contains “copy” are yours to release.  This rubs me as hack-a-logical as it leaves so much up to naming conventions.  But, whatever…

Ftp on Windows

September 12th, 2008 Rusty

This morning I needed to copy a 300 MB file from my dev server to my local machine.  I was lazy and Windows Explorer was already open.  So I called up my ftp:// url and copied the file to my local folder.  It showed 1 hour and some odd minutes to complete.  That’s an amazing 50k per second!  Wow…  broadband has brought us so far. 

2 hours later, the dialog stated 39 minutes remaining.  Hmmm, perhaps that initial prediction was a little fuzzy.  Vista must be applying some fractal logic to determine how long things should take.  Then, KABOOM.  "Windows was unable to copy the file"

Now I remember why I started using FileZilla and why I stopped hating ftp.

So I launched old FileZilla, connected to my same server, and copied the same file.  Now I can see that it, indeed, is achieving around 40k per second.  FileZilla handles communication issues much better.   I failed to time it but I was able to leave the transfer window minimized and continue on with other work.

I still don’t like ftp but no-one has really come up with a great alternative.  Although I am now checking out Drop Box.  Looks promising…

Subversion (SVN) Integration with XCode

September 2nd, 2008 Rusty

This is specifically for the developer who wants to get source control going quickly as well as to instruct a colleague on how to jump onto a project without excessive ceremony.  I’m going to skip the part about associating the project with SVN in the first place, importing the project initially, etc.  The project has to be imported into SVN before you can then check it back out and work subversively.  I’ll assume you can figure out how to add a directory to Svn using the command line.  The basics (and then some) are covered on Subversion with XCode on Apple

 

Open XCODE and Configure SCM with Subversion

a picture says a thousand words…

SCM-Configure-Repos

Add a Repository

Click the "+" to add a repository

Svn-Repos-Screen

Give it a name so you can tell it apart from the Skynet alpha Svn repository

New-Repos

Configure your Svn

Svn-Repos_details

Click "OK"

Checkout your XCode Project Structure

Svn-choose-repos

Navigate to the root of the Code project and "Check Out"

SVN-Checkout-1

Navigate to an appropriate project location on your file system.

Svn-Checkout

An alert will indicate that it is complete
Choose "Open xxx.xcodeproj"

 

Use integrated SCM with SVN

If the project was previously associated with SCM, you can skip this step.  In other words, if you cange a file and an "M" shows up next to it, you’re done.

Otherwise, Choose Project > Edit Project Settings

Edit-Project-Settings

Near the bottom, choose your repository for SCM

SVN-Associate-Project

Browser Tools, Blogging and Simplifying Workflow

July 12th, 2008 Rusty

I tend to be a creature of habit. For example, I LOVE the Coby hamburger at Food 101 in The Virginia Highlands (1397 North Highland Ave, Atlanta, GA 30306). Whenever I eat there, I get the hamburger. They have awesome crab cakes and just about everything is fantastic but I always get the burger.
I do the same thing with other aspects of my life. Every now and then, however, I discover something that makes me regretful that I hadn’t tried it earlier (more on that later). I use Windows LiveWriter to blog. Its awesome, really. However, I have to fire it up. If my VMWare Vista isn’t running, I have to launch that. If it was suspended, that takes a minute.
I’m writing this post using ScribeFire. ScribeFire is a FireFox extension that is tightly integrated into your active browser window. You get alot of context at your fingertips that will save time when creating links to resources outside your blob (or even in your blog). It appears to have social bookmarks built in as well. I also added the del.icio.us bookmark add-in from Yahoo because it looks pretty feature-rich. Navigating to Del.icio.us turns out to be the barricade between me and my social network savvy self. With all the machine refreshes I’ve had lately, and the sheer number of computers I use every day, I am sharply beginning to realize the value of centralized, shared bookmarks.
While I still expect to use Windows LiveWriter to blog in general, providing quick access and integration with browsing is a crucial efficiency enhacement that I wish Safari had. I’ve been a Safari fan for a while now but I may start using FireFox on my mac now that FireBug is compatible with FF 3.0.
I couldn’t program as efficiently as I do without FireBug. Period. I expect my blogging will increase due to ScribeFire. While I sometimes get too busy to see what tools are emerging, failing to do so costs me and everyone around me time and money. Being diligent in purposefully discovering new options for old behaviors is a very valuable exercise. For example, I found that adding Food 101 Truffled Honey Mustard to their Coby burger is terminally delicious.