Archive for February, 2006

February 6th 2006

Cocoa Update

Well, Phil M. asked for it. Here it is :)

It’s been a month with my puppy Cocoa. He’s doing really well. I’m going to upgrade his travel-style crate to a much bigger wire-crate that will give him more room during the days when I’m not around. However, he seems to be house-trained now, which is great — only two accidents during this period, and now he seems to go to the door every time he needs to go out.

Today he isn’t with me for another few hours though — he’s getting neutered. Which my coworker Dave reminded me of a funny far-side comic that I decided to post here:

He’s grown a total of 11 pounds over the last month, so he’s up to 41 pounds now. He still is growing too — probably will hit the 50 lb mark within a few months. I had to get him a new collar because he outgrew his old one. At the same time, I got him that “mesh” ball thing that he has in his mouth. He loves playing with that thing :)

Well, that’s about it. Oh, the other Cocoa stuff is coming along too ;)

Update: He seems like his old self already, a mere 4-5 hours after waking up after his surgery. He’s chowing down right now.

3 Comments »

February 3rd 2006

Assert! Assert!

I realized today that one of my favorite features in REALbasic 2006r1 is the ability to get the stack crawl to an exception. It’s more useful than you’d think. I’ve always wanted to be able to write an assertion routine that worked in such a way that when an assertion failed, I could figure out exactly where that assertion was at.

For those of you who aren’t familiar with assertion-based programming, it’s where you routinely insert “assert” statements for situations you think aren’t possible, or that the code assumes aren’t possible. This way, if a bad parameter is passed in, you get a noticable failure rather than exceptions, odd results, or potentially even crashes. Thus, it’s like saying, “I assert that MyObject is not nil, and because I assert that, I will use it without even checking for nil.” The assert is actually doing the check for nil, and if it fails, it will let you know that it happened.

Well, today, I’m going to show you how to write an assert routine in REALbasic very easily:

Sub assert(condition As Boolean, failureMessage As String)
// Check to see if the condition is false
If Not condition Then
// We failed. Raise an exception
Try
Raise New RuntimeException
Catch exc As RuntimeException
// We just caught our own exception.
Dim md As New MessageDialog
Dim trimmedStack() As String
trimmedStack = exc.Stack
// Because the "assert" method will show up in the stack
// we need to remove the top-most item.
If ubound( trimmedStack ) >= 0 Then
trimmedStack.Remove( ubound( trimmedStack ) )
End If
// Prepare the error dialog
md.Message = "Failed Assertion"
md.Explanation = failureMessage + EndOfLine + EndOfLine + _
"Please send this information to the developer." + EndOfLine
Join( trimmedStack, EndOfLine )
md.ActionButton.Caption = "Quit"
md.CancelButton.Caption = "&Continue"
md.CancelButton.Visible = True
// if the user decided to quit, rather than tempt fate,
// we should quit.
If md.ShowModal = md.ActionButton Then
Quit
End If
End Try
End If
End Sub

Example usage:

Sub Something( someParam As Dictionary )
assert( someParam <> Nil, "someParam was nil" )
// use someParam
End Sub

That’s it! By raising an exception, then catching it, we can get the current stack trace, and use it to display a dialog that would help any developer figure out where an assertion is failing, which might be enough to fix the problem. This is even better than some assert routines written in other languages because it provides a complete list of the call stack, which is useful if a very common routine fails.

Have fun, and happy asserting!

8 Comments »