Friday, December 04, 2009

A What?!

Objective-C has a concept called "Categories" - I just learned about these things a couple of days ago listening to a podcast and wasn't quite sure when or where it would be appropriate to use something like this.

At a high level a "Category" is a way to change (add or modify) the behavior of an existing object. For example - if you needed to print a string backwards in your application; one option would be to subclass the string object. Another option would be to create a category to add a method to perform this operation.

The benefit of a category is that the new method is available to all of the Objects of that type. Another cool benefit is that you don't have to change any code at all to take advantage of the behavior change. All types of the categorized object get the new behavior.

The downside to this? I'm not sure it seems pretty powerful but as they say "with great power comes a great responsibility"

Here is the problem and the solution I solved by using categories:

(IMHO) The Apple API has a bug in the MapKit. If you add a MKPinAnnotationView the CallOut has a (seemingly) random z-index defined which causes the following to happen:

You can see the red pins are on top of the call out window. Very irritating - I was about to give up when I googled one last time and found this posting. Here is how I fixed it by using a category:

Here is the header:

#import <MapKit/MapKit.h>

@interface MKPinAnnotationView (ZIndexFix)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;

and here is the Implementation:

#import "AEBMapViewCategory.h"

@implementation MKPinAnnotationView (ZIndexFix)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
[self.superview bringSubviewToFront:self];
[super touchesBegan:touches withEvent:event];

So basically I overrode the default behavior of the [touchesBegan: withEvent:] method.

That is all the code I changed to make the picture above look like this:

Pretty sweet huh?

If anyone else solved this problem differently I'd love to hear about it.


Tuesday, December 01, 2009

Testing and xCode

Java developers are spoiled.

I was a late comer to the testing band wagon. I'm not a test nazi by any stretch but there is a certain amount of confidence that testing gives you.

I miss that when I'm coding in Objective-C.

I was so happy today when I found out today that xCode has built in support for unit testing. But it sucks horribly. Granted much of my problem may be that I have not fully grokked the underpinnings of this environment. But I have spent all day and most of the night trying to figure it out and this is the extent of what I have figured out.

Unit tests that exercise simple parts of the application (non-framework/Cocoa stuff) can be tested after a build is performed. However:

  • Debugging is not an option
  • Capturing output from sysout is not an option
  • Decent reports don't seem to be available
  • Decent error reporting doesn't seem to be available

It's hard to believe that unit testing is so far behind on this platform. Java developers don't know how good they have it.

Monday, November 09, 2009

ADC2 Results

<sigh> so the results for the ADC2 are out. Unfortunately, DungeonTrainer was not one of the winners. I was a little bummed - they haven't released the numbers in each category but I'm guessing that there was a lot of competition in the casual game arena.

One of my good friends submitted an app and made the first round of cuts (his app was in the education category). So, congratulations to him and "Mobi Professor".


Sunday, November 01, 2009

New Mac Blogging Software

I'm trying out some new blogging software for my mac called blogo. I've been using a windows vm with LiveWriter installed - which is a spectacular piece of software for my blog postings up until now. I tried blog about a year ago and got very frustrated with it and happily uninstalled it - I hear it's a lot better now - we'll see.

My last post I was lamenting about the use of of timestamps in the iPhone SDK specifically CoreLocation, and as usual it takes me proving my ignorance before I can figure something out. So, here is what was going on...

If you remember back in your first C programming class - you learned about the function called printf. I specifically remember my instructor explaining the function and that the %d symbol meant to format an integer value and %f meant to format a float value. This information was lost to the deep recesses of my memories. This is important as I was using the %d to for logging -basically to output the age of the calculation - trying to understand what was going on and getting crazy - seemingly random values for the age.

Anyway this is the correct way to get the age from a CLLocation object:

float age = fabs([[newLocation timestamp] timeIntervalSinceNow]);

Armed with the value of age you can proceed with confidence that you are calculating the age of your location correctly.


Monday, October 12, 2009

Who wrote this crap?

It never ceases to amaze me how code that is written 2 or 3 months ago almost always ends up being crap.  I am exaggerating of course, but a lot of time the code I write when I revisit later is just crazy. 

I'm sure part of that is because the code I write on my own is often done at 2 or 3 in the morning.  It is interesting though, I can clearly see where the coding sessions were broken up instead of flowing smoothly in one session.

So, CoreLocation is one of the frameworks in the iPhone SDK.  I used it quite a bit with the LifeAware iPhone app and am using it again for my current project.  In both project I ran into the cached location issue.  Now if you read the SDK and even look at the sample code for this framwork you will see things like this:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    [locationMeasurements addObject:newLocation];
    // test the age of the location measurement to determine i
f the measurement is cached
    // in most cases you will not want to rely on cached measurements
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    if (locationAge > 5.0) return;

This code does not seem to work.  I tried so many variations and it just doesn't work.  I'm sure it worked for Apple and maybe it works for you.  If so that's great, good for you.  But for me it didn't work so here is how I did it.

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"Found a new location %@",  newLocation);
    NSDate *now = [[NSDate alloc] init];
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"HH:mm EEEE MMMM d"];
    NSString *nowTimeString = [outputFormatter stringFromDate:now];
    NSString *locationTimeString = [outputFormatter stringFromDate:newLocation.timestamp];
    NSLog(@"The age of the location is: %@", nowTimeString);
    NSLog(@"The age of the location is: %@", locationTimeString);
    if ([nowTimeString compare:locationTimeString] == NSOrderedSame){

Basically I take the timestamp from the location object and create a new one for "now".  Convert the two to string representations and compare the strings for equality.  This gives me a location that is less than a minute old. It seems just a little bit hackish but I think it's a pretty decent solution to a flaw in the API, seriously why wouldn't you say something like:

[locationManager startUpdatingLocation useCached:NO];

Happy coding!


Sunday, October 11, 2009

Been a while...

So it's been a while since I last posted a blog entry.  A whole lot of things have been going on.  First of all I finished my entry to the Android Developer Challenge 2 it is a game called Dungeon Trainer

Dungeon Trainer is a simple RPG game that lets you create and train characters by battling other characters.  Once your character is created you can upload them to share with players around the world.  You level up, buy weapons and armor and then battle (train).  It is a simple game for a couple of reasons

  1. The time constraint of the challenge was very tight I started designing on June 16th and had to develop it all from concept to final product by Austst 28th.  Ruffly 2.5 months.
  2. Mobile games should be simple.  Having a game like Baler's Gate or Diablo on your phone would be cool but it would be hard to pick up and put down quickly.  Or casually play in a meeting that you were bored at.
  3. It was the first game I ever wrote so I wanted to start simple and build on it.

As of October 6th the first round of judging is complete.  I haven't heard any results yet, I'll let you know when the winners are announced though.

I'm also working on another iPhone app.  This one is pretty cool and much simpler than the LifeAware iPhone app.  I can't say much about it except that I am still doing iPhone stuff.

It is kind of a cool contrast between the 2 platforms though(android an iPhone).  My day job is Java so the Android platform was a super easy transition.  The iPhone was not easy in any way, even still I kind of enjoy developing for it.  It kind of reminds me of VB in a way although you have all the power of C to hang yourself...

I'll put some little code snippets of the iPhone out. I"m sure if I have struggled with these parts other people have as well. 

Until then ciao'.


Tuesday, June 02, 2009

Test Driven Development

So I posted an entry a little while ago... ok perhaps it was more of a rant about unit tests.

I don't disagree with the post, however I have learned a little bit more about testing.  Specifically what testing is and what it is not.  I recently was able to attend a JUnit class that was given by J.B. Rainsberger.  He also authored the JUnit Recipes book it really is a good book and worth the price.

Some of the things I have taken away from the class is that unit testing pretty much sucks.  However, behavior driven design is pretty cool.  Unit testing (IMO) is going through code (usually code that exists prior to the test being written) and writing a test to figure out how the code works.  This is a very effective way to trouble shoot existing code but is a very large effort, both in time and mental CPU cycles.

Behavior Driven Design is a technique that lets developers take an abstract concept (or a problem) and get it into code.  Testing frameworks like JUnit and JMock are helpful tools to do this with but don't get confused you aren't testing anything.  You can't test anything because the code doesn't exist yet - you are modeling the behavior you want your code to exhibit.  This is a powerful technique and I have done it many times on many projects but I never had a name for it.

I recently implemented a restful webservice using a new java framework called cxf.  I was itching to try out the techniques of Behavior Driven Design that J.B. showed us .  Unfortunately, I didn't have a good understanding of cxf so I was unable to use BDD effectively for quite a while.  I fell into the difficult place that java developers fall into of iterating over configuration changes and server restarts.  This is an important point not all of our efforts as developers can be boiled down into writing test cases or even BDD.  In fact, I did try to do some BDD work prior to getting the frameworks working but ended up throwing all the code away. The cxf framework changed how I thought the code would eventually work.

If anyone is interested I'll post some code examples of how the code ended up looking.  But the point I am trying to make is that doing Behavior Driven Design, although leaves a whole lot of unit tests lying around as artifacts of the process that is not the goal.  Unit testing on the other hand is an extreme effort that we do as either a last resort or as an ego trip to get our testing coverage up to 100%.

Don't confuse behavior modeling with unit testing - they are worlds apart.



Monday, June 01, 2009

It's been a little while

So with all of the fancy iPhone, AppEngine, GWT, Android development I've been involved with I was on the job the other day and someone asked me how to do something in Javascript.  Her problem was a tricky one but it basically built upon a very simple foundation.  When a user selected something she needed some text and a style to change.  I knew immediately how to do it but the syntax escaped me for a bit so I thought I'd stick it out here for the next time I needed it.

The problem is pretty simple click a button change some text and the color of the text.  So here is my very simple solution.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
   <title> Switch </title>
      <script type="text/javascript">
         function change()
            var styleClass = 'required';
            var textValue = 'Else'
            if (document.getElementById('sigDate').className == 'required')
      <style type="text/css">
            color: RED;
            color: BLUE;
      <input type="button" value="Switch" onclick="change()" />
      <div id="sigDate" class="standard">

Like I said a very simple solution to build a very complex solution upon.



Wednesday, March 18, 2009

St. Patricks Day run

I guess there were 3,100 people at the run.  It was 4 miles long.  My time was a stready 41.5 minutes - one of the guys I went with ran it in 26 minutes though!!!

Tuesday, March 10, 2009

A rainy Kansas City aternoon

Here is a pictue of Kansas City from my office building.


Sunday, March 08, 2009

Out of my comfort zone

So in a very strange twist of events I am finding myself completely out of my comfort zone writing Objective C code. 

I have an earlier blog post about Objective C and some of the exceptions you get.  There is another scenario that happens when things just don't happen.  Without a doubt I am a novice at Objective C and I'm not entirely certain how you would classify it.  It is a odd mix of a scripting language and C.   Because of that hybrid I often find myself  troubleshooting errors that don't make much of any sense to me.  No doubt my lack of experience.

For instance there is a magic object in objc  called "id" apparently there are a lot of rules around this object (that I'm just learning) you can do something like this:

NSLog (@" this is it's vaule %@", magicId);

but not this:

NSLog (magicId);

I'm sure there is a very sound reason and I can pretty much assume what it is at this point but stuff like that brings into light that Objective C is a blend of two types of languages.  And makes me miss my comfort zone...

Now, where is that manual?


Thursday, March 05, 2009

The reality of testing

This morning I watched an interesting you tube video on Django Success Stores.  If you aren't familiar Django is an open source web framework written in and for python.  As a side note AppEngine from Google also has implemented a version of Django for their systems.

In this video without exception every developer on the panel had various levels of apologies to offer for not testing their software.  Sometimes it seems that there is a whole religion devoted to testing, and just like other religions they have done a great job of making competent, productive developers feel guilty about not following their mantra.  Regardless of what that competent developer is working on or for who.

Don't get me wrong unit testing has its place - but there are situations where a rigorous, disciplined testing process is not appropriate.

If you are developing in an environment where you are not held accountable to your delivery dates ( I have had jobs like that ) or you are not delivering software versions frequently (for example if your production deploys happen less frequently than every other month).  Maybe dogmatic unit testing is for you.  However, if you are in a high stress work environment and you ship the code as fast as you can; then unit testing may not be the correct fit for your development process.  If you work in an environment where speed is the most important - and your business owners don't even know what they want but the want it delivered anyway - stop apologizing for not writing tests for every setter and getter in your code base!  I don't think I've ever heard someone who believed in Christianity apologize for not believing in Buddhism or vice versa.  Let's be done with the self recrimination, unit testing is not a silver bullet nor does if fit every software development scenario.

Having said that should we have great bodies of untested code running around?

Obviously not. I once worked with a testing zealot who loved to scoff at the lack tests in our applications.  It's not like these applications don't have any tests.  In fact some had quite a bit the difference was they tested functional paths.  And some of the only had tests that covered 5% of the code and you know what?  I'm ok with that.  Thanks to Java and Maven our applications have lots of libraries and some of these libraries are literally a bunch of simple POJO's basic setters and getters that are shared across the domain.  Should I write test cases to make sure that if you pass in a value you get one back?  No I should not, maybe a testing zealot should - but not me, that's not my religion.

I would suggest that if you are like me and unit testing is not your religion, then stop apologizing first of all!  Then read up on something called "Functional Testing."  Basically functional testing is taking a step back and looking at your application and understanding the functions that are performed - another way to say that is to test how your application is used (yeah we call those use cases).

This is how I love to test.  I not only enjoy writing these kind of tests but they are also critically important to understanding how the application really works.  And if you run into a problem in production if you wrote these kind of use case tests you should have the ability to recreate the scenario fairly easily.  That is where you get the bang for your buck.

I am making the decision write now - I am not apologizing anymore for not writing unit tests anymore.  My employer doesn't give me enough time to write the code without tests.  However I do write functional test that make EJB calls, query databases and do all kinds of things unit tests aren't supposed to do.  It's possible I may end up in a programmer's hell for writing functional tests instead of unit tests but I bet I'll have lots of great company.

What do you think?


Saturday, February 07, 2009

I'm starting to hate this error

So, I'm learning Objective - C now - Yeah so I can be like all of those other iPhone developers.... 

I am doing it for a very specific reason though - over at LifeAware we're trying to port or application to the iPhone platform.  We were recently accepted into Apple's iPhone Developer Network which is kind of cool - it cost $99 of course but cool nonetheless. 

Once we got accepted I immediately ran out and purchased an iPod Touch to do development on.  It is an impressive device - I think it's overpriced - but it is pretty damn cool. 

But - I digress. 

I've been working through a spectacular book on iPhone devleopment called "Beginning iPhone Development - Exploring the iPhone SDK".

It is a fabulous book that has links to an active community forum of a bunch of other people learning together.  If you're looking for a good technical book that is pretty easy to follow and teaches a lot of stuff this is a good one.

However, with all projects you eventually need to step away from the tutorials and start building the next greatest thing and that's when the crazy errors start rolling in.

This is my current nemesis:

2009-02-07 20:53:13.535 NavTest[830:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "MySpecialView" nib but the view outlet was not set.'

So I've finally figured it out (thanks to the book) the error is actually pretty decent.  It is saying - a view was requested and it was found but the view couldn't be rendered because the outlet for the view wasn't defined.  Here is a quote from the book that warns about the danger:

When we  changed the underlying class of the file’s owner, the existing outlet connections were broken.   As a result, we need to reestablish the connection from the controller to its view.  Control‑ drag from the File’s Owner icon to the View icon, and select the view outlet to do that.

Which is really the solution to most problems.... RTFM



Thursday, January 08, 2009

Android post

Coming at you from my Android phone!!!