Archive for November 2005

Lost email password? Easy fix.

So I am selling my laptop this week and I needed to set up a new work PC with email. However, for the life of me I could not remember my email password!

Eventually, it dawned on me that since my email password is sent in clear text, all I need to do is run Ethereal (free network packet sniffer) with an appropriate filter (tcp port pop3), and I would have my answer in moments!

Sure enough, moments later I had my password. Of course, it does not instill much confidence in the privacy of my email.

Also, it is a reminder that it is best to use different passwords for all your needs. I have started using KeePass recently, and have been very happy with it. Amongst its nice features are the ability to generate strong passwords, and a cute copy-paste feature whereby you copy your password from KeePass, but it only stays in the clipboard for 10 seconds.

Exceptions and Globalization

It is commonly accepted that it is best not to use exceptions to control the flow of an application. I have found that an interesting side-affect of this approach is that I feel I do not need to provide facilties to support translation of my exception messages.

It serves little purpose to translate the message since it is probably only meaningful to a programmer anyway. The only reason I will display the message on the user interface is to make it simpler to report it back to me – I am the primary consumer of the message, and I prefer it to be in English, so that I can more easily find the cause.

If a Tree falls in the Woods, is there a Sound?

Consider this quote from Bob Martin, recently found in an old newsgroup post:

Not all functionality relevant to a domain object should be a method of
a domain object. For example, consider a modem class. A behavior that
is relevant to a modem is “ConfigureForUnix”. However this behavior should
not be a method of Modem since there is no end to them. You will be adding
ConfigureForXXX methods to the modem class for every new operating system
that comes along. Rather, we would like to see the ConfigureForXXX behaviors
placed into command objects decoupled from the Modem class.

It is a mistake I have been guilty of in the past – I have a domain class and I’ve been told that I should encapsulate logic inside of my objects, so I create methods on my class. For example, I may create a “Save” method that knows how to save the object, and a “Load” method that knows how to retrieve it from the database.

But the simple fact is that logic needs context. Sometimes the context is all inside of the class itself (variables and properties), and that is well and good. But not always. For domain objects it is in fact common to need access to some sort of external context (how was this object created) or environment (what is the value of setting X).

Hard won experience has taught me that finding that context from inside of a domain object method can create complexity (events, obscure flag variables, etc.) and introduce coupling. I definitely don’t want that!

The solution is to put the code outside of the domain objects. Some might say that this throws away encapsulation, breaking one of the primary rules of OO. Put that way, it definitely sounds like a bad idea! The first time someone brought that point up, I was at a loss for words. They were 100% correct – my finely thought through design was breaking encapsulation! That could not be good. Or could it? Read on…

The ideal of a domain object with properties and methods, magically encapsulating all data and behavior is ultimately just too simplistic. Objects do not live in isolation. Behavior is defined only in terms of an environment. Let me explain that using a simple example…

I can shout loudly, and if you are standing near me in my office, you will hear me. But alter the environment, say by placing us floating in space, and I can shout all I like without you hearing a thing. In OO terms, I can add a “Shout” method to my Person class, but the environment is never static, so eventually my method is doomed to failure, and my class is doomed to become legacy code, forced to be re-written because it is coupled too tightly to its environment.

If I really really want to have a Shout method to my Person class (and that is by no means certain), then I have to provide the environment (or context) as a parameter to that method, and have the actual shouting be implemented inside of the environment. That is the Visitor pattern.

Another alternative is to provide a singleton-style environment, implemented by a Registry pattern. This approach can free you from the need to pass the context around via parameters all of the time.

Incidentally, Test Driven Design will lead you naturally to one of these approaches, because you will need to test the Shout method, and the only way to do that is to provide a mockup of the environment/context that can verify that the Shout method actually tried to do something.

A last thought on encapsulation, and the title of this post. The environment (the woods) encapsulates behavior. If a tree falls in the woods:

Tree.Fall(woods)

then the sound is communicated to all within hearing distance. Perhaps only a log hears the sound:

Public Sub HandleSound(sound) Handles Woods.Sound  Trace.WriteLine("A sound was heard")End Sub

Or perhaps nothing hears it and the question remains unanswered.

Nullable Types in .NET 2.0 are a hack

I’m starting to get into .NET 2.0 now that it is officially released.

I’m sorry, but nullable types (MSDN link) are a complete hack, created without understanding of the real needs of developers that deal with nullable fields in the database. It most certainly is not “deeply integrated” into the CLR or the C# language, as some would claim.

If it were, then it would be simply part of the value types, i.e. Int32 would have a hidden magic value that equated to NULL. Cannot get much simpler than that. Then (myInt == null) works fine, as does (myInt = null), as does (myInt = DBNull.Value). No wierd casting, boxing, new operators (?? – you gotto be kidding me), .Value or .HasValue etc.

Add to that the spotty support on the WinForms UI and lack of support at the DataReader level. Grrr. What a waste. I’m glad the VB team did not get in on the act.

By failing to think outside the box, the .NET Framework team have simply created a clumsy hack that adds complexity to the language instead of simplifying it. I score them an F (thats a fail) on nullable types. The C# team gets an F- for adding even more obscure operators to the language.

GIMP – installed

I installed GIMP at home yesterday on my Windows XP box. The GIMP is a free, open source, well-featured graphics application in the style of Paint Shop Pro. My mom had asked for a collage of recent photos of the kids for her PC desktop, so I thought I’d give it a try.

I’ve always had difficulty with the layered-paradigm of professional image editing applications, but I seem to have got the hang of it, and had the collage done in less than an hour (saved to a jpeg and shipped off via email).

I think I’ll keep it.

VistaDB 2.1 database for .NET has been released

I don’t usually make product announcements, but this is for a good cause, so…

This 2.1 update includes over 60 improvements, including new support for .NET 2.0 and Visual Studio .NET 2005. VistaDB is a small-footprint, embedded SQL database alternative to Jet/Access, MSDE and SQL Server Express 2005 that enables developers to build .NET 1.1 and .NET 2.0 applications. Features SQL-92 support, small 500KB embedded footprint, free 2-User VistaDB Server for remote TCP/IP data access, royalty free distribution for both embedded and server, Copy ‘n Go! deployment, managed ADO.NET Provider, data management and data migration tools. Free trial is available for download.

- Learn more about VistaDB
- Repost this to your blog and receive a FREE copy of VistaDB 2.1!