Archive for February, 2007

February 26th 2007

Nil Object Pattern

Or, as it’s called more widely, the null object pattern. Of course, REALbasic calls “null” nil, so I think it’s better to call it the Nil Object Pattern when dealing with REALbasic code.

The Nil Object Pattern is a design pattern that simplifies code by using a default value as the “nil” state. This simplifies code because instead of checking for Nil everywhere, this object sits in place and responds appropriately to any requests (generally ignores them, or returns sensible values).

I was writing code this weekend that I refactored to use this approach. I found myself typing “if foo <> nil then” a little too much, and realized that this pattern would simplify the solution quite well. Another usage situation that I’ve seen is the REALbasic compiler. In the compiler, we use this approach because errors can propagate upstream, but we still want to be able to continue on. By using this approach, we can guarantee we can continue in the compiler and not worry about a null pointer, which in C++ crashes instead of giving you a nice exception :)

So, how can we use this in REALbasic? I honestly can’t think of any good built-in examples in our framework that could benefit from this sort of approach, so we’re going to have to jump into hypothetical land. Consider the following code:

Interface House
Function HowManyStories() As Integer
Sub AddThing( o As Object )
Function RemoveLastThing() As Object
End Interface

Class SimpleHouse
Implements House
Private mThings() As Object

Function HowManyStories() As Integer
Return 1
End Function

Sub AddThing( o As Object )
mThings.Append o
End Sub
Function RemoveLastThing() As Object
Return mThings.Pop
End Function

// More useful stuff
End Class

Function LocateHouse( someparameters ) As House
// Find the house based on the parameters&#8230;
End Function

Now, pretend that LocateHouse was guaranteed to always find the house. All of your code in your program relied upon this basic fact, that LocateHouse simply could not fail. Then the inevitable happened: LocateHouse would make more sense if it [i]could[/i] fail.

So now you’re faced with the dreading realization that all of your code doesn’t check for nil. You have two options: check for nil everywhere you call LocateHouse and figure out how to handle it properly. [i]Or[/i] you could create a NilHouse implementation to return in the failure case.

Class NilHouse
Implements House
Function HowManyStories() As Integer
Return 0
End Function
Sub AddThing( o As Object )
// Do nothing
End Sub
Function RemoveLastThing() As Object
// Do nothing
End Function
End Class

And viola, your code works as always, and you still don’t need to check for nil because sensible implementations are kept in place that keep your other methods simply working as always.

This pattern is a powerful pattern that certainly can be abused. However, in certain circumstances it is the most elegant way to reduce complexity in code.

4 Comments »

February 26th 2007

Welcome

I successfully moved this blog to a new host. If you’re seeing this, the DNS has propagated to your area. I didn’t post this until it propagated to my area :) If you’re not seeing this, your DNS cache somewhere along the line hasn’t been updated.

I’ve been making subtle improvements to the backend of this website, such as updating to the latest wordpress, adding live preview to comments, etc. Enjoy, and expect a bit more regular fun updates. I’m finishing up a post I started this weekend that I hope to post today or tomorrow.

Comments Off

February 23rd 2007

REAL World 2007

Just a friendly reminder to all of you reading the blog. REAL World 2007 is taking place starting May 9 and ending on May 11. I’m doing two sessions. The first one is Mastering Declares, which is a repeat from last year but with coverage of new features we’ve introduced since then. If you have always been curious about declares and haven’t had the courage to tackle them, this session is for you. Or, if you’re pretty good at them but haven’t messed with using structures and enums to simplify life, this session is also for you.

The second one is a new session, but I’m even more excited about presenting it. I’ll be covering how to use Subversion with REALbasic to manage team development and version control. While Subversion is what I’ll be showing, all the tips and tricks should work with any of your favorite version control systems. Additionally, I’ll be talking about what we’re doing to make version control development easier for you (and us).

Early registration ends on March 9 at which time the price increases by $100. The sessions list is nearly complete, and as more information is available, it’s being put on the website.

As with every year, I’m looking forward to being able to meet up with everyone here. It’s always a fun three days, and I hope everyone can make it!

2 Comments »

February 20th 2007

Correct Selection Behavior for Fields on a Window

So I set out to fix jicfdote today as it was bugging me quite a bit today. The overall problem is that users expect tabbing between fields on a window to select appropriately, and for single-line EditFields, this means selecting the entire field.

To do this, you can simply write “me.SelectAll” in the GotFocus event. However, that causes the problem the bug report was talking about: clicking on the field now selects everything rather than putting the insertion point in the proper location.

How can we get the best of both worlds? Simple answer: The MouseDown event. In the IDE we happen to have most of our EditFields coming from a common superclass, so I added the code there. However, you can do it yourself with an EditField subclass like this:

Class FocusSelectEditField
Inherits EditField

Private Dim mPreventAutoSelection As Boolean
Dim AutoSelectOnFocus As Boolean

Event GotFocus()
Event MouseDown( X As Integer, Y As Integer ) As Boolean

Function MouseDown( X As Integer, Y As Integer ) As Boolean
mPreventAutoSelection = True
Return MouseDown( x, y )
End Function

Sub GotFocus()
If mPreventAutoSelection Then
mPreventAutoSelection = False
Else
SelectAll
End If
GotFocus
End Sub
End Class

There we have it. By setting a flag in MouseDown we can ignore attempting to select ourselves and use the default editfield behavior. A simple fix to a very annoying problem.

8 Comments »

February 18th 2007

Ok, I’m back

Sorry for no updates. Been a busy holiday season, and I’ve been holding off posting for the last month because I didn’t want to come up the obligatory “here’s what’s been going on” post. So, I’m going to skip that post, and just offer this as the welcome back post, and relieve the pressure of posting such a message when I really do have something to say.

That’s all ;)

3 Comments »