Monday, July 23, 2007

Theory of Software Usability

As software matures software designers are putting more and more thought into "usability" or "user experience." This is clearly a good thing. But I've noticed that this strange concept of usability isn't really that well defined.

There are a few distinct factors that all contribute to how usable a given piece of software is overall. I refer to these as:
  • Ease of Learning - how easy is it for a user to learn to use the software
  • Ease of Use - how easy is it for a user to use the software to achieve their goals
  • Familiarity - how familiar is the user with the software or patterns used by the software
  • Functionality - how many of a user's goals can be accomplished with the software
Of these 4 factors, Familiarity is the only one which changes over time (for a given software release). However, all of these factors depend on a combination of the user and the software. That is, different users will not only have different "values" for Familiarity but for all three as well.

In the case of Ease of Learning this is simply because some people learn faster than others or will take to certain concepts easier than others. Ease of Use will vary because different people may want or need to accomplish their goals in different ways. Functionality will vary for a similar reason, different people may have completely different goals.

So clearly, there is no way to look at a piece of software and assign these 4 factors static values. However I think they are still helpful as a guide to understanding what makes something usable. Its also interesting to consider how these factors relate to each other.

Probably the most important relationship is that between Ease of Learning and Ease of Use. Firstly, they are not the same thing. Take the third generation iPod for example. The iPod is frequently used as an example of an extremely usable interface. Its very simple in that it has only a few UI concepts and a few controlling buttons. One of my main goals when using an iPod is to find a certain artist, album, or song that I want to listen to. To do this I need to navigate through my music collection, reading the names until I find what I'm looking for. The third generation iPod has everything sorted alphabetically which makes scrolling around fairly easy. But wouldn't it be easier if it had a search feature? It didn't. It also didn't scroll the names of songs and albums when you had them selected (it only scrolled while it was playing), which made it very hard to tell if you were looking at the song you wanted. So while the third generation iPod had the functionality I was looking for, it wasn't terribly easy to get there.

Easy to learn, but not as Easy to Use as I'd have liked. These deficiencies have been corrected in later versions of the iPod of course, but it still serves as a great example.

This demonstrates that Ease of Learning and Ease of Use can very easily be in opposition to each other. By making an interface easy to learn you may be crippling how effectively it can be used.

Another interesting point here is that Ease of Learning can only contribute so much to the overall Usability of the software. You can imagine a text editor that is very easy to learn because it only consists of a text box that you can type in. But if the functionality of the text editor is so crippled such that it doesn't support cut and paste, return characters, capital letters, etc... Clearly, this will never be a usable text editor no matter how easy it is to learn.

Also, Ease of Learning becomes less important the more familiar the user becomes with the software. Consider the Vim text editor. This is a very powerful editor which is far more usable to an experienced user than an editor like Notepad. So while Notepad is exceedingly easy to learn, it can't compare to Vim overall. This is because Vim is both easier to use to accomplish certain goals (ex: delete all text to the end of the line) and because it has much more functionality.

So why is so much emphasis placed on Ease of Learning these days when it can clearly only take you so far?

Consider Vim again. Vim is not easy to learn at all. It requires a lot of memorization and completely new UI/control patterns to be learned. In fact a user who knows nothing about it may not even be able to figure out how to type any text into it.

Obviously some kind of balance must be struck. The next logic question then is where do you strike the balance. The only answer (as usual in Computer Science) is it depends.

If your user base contains skilled users with a lot of familiarity, you want the Ease of Use. If your user base is using your software to accomplish very specific goals in a controlled environment where they will be supplied with training, you probably want the Ease of Use. But if your user base is mixed with many different skill levels, many different goals to accomplish, and no structured environment, Ease of Learning is going to be very key.

Not surprisingly, we're right back to where we started with a not very rigidly defined concept of Usability. But at least recognizing these 4 factors as distinct factors can help in the design process as you size up your user base and your goals for the software.

Tuesday, July 17, 2007

Shared Assemblies, Components, and Applications

Here's the situation:
You have a shared assembly which contains a lot of useful utilities called SharedAssembly.dll

You create a customized component that uses SharedAssembly.dll called ComponentA.dll

You create a full application which uses SharedAssembly.dll and ComponentA.dll called App1.exe

In VS2005 you setup your solution to include all the projects: App1, ComponentA, and SharedAssembly. You setup the references as Project references. Now, when you build the solution, VS figures out it needs to build SharedAssembly first, then ComponentA, then App1. The end result is App1 and ComponentA both use the same version of SharedAssembly. This is wonderful.

But what if you wanted to change it so that ComponentA wasn't included as a project in the solution file? Instead you want to release ComponentA as dll files that can simply be referenced by App1. The problem is that you now have two different versions of SharedAssembly.dll. If you set it up this way you'll get a runtime error because the framework detects that the version of SharedAssembly.dll is not what was expected by ComponentA.dll. Is there any way to get a setup like this working?

I want ComponentA to be like its own little mini-application, with its own versions of dlls, completely independent from App1.

My mind is immediately drawn to Java's jar files, but I don't really know how they work.

I've tried a little utility from MS Research called ILMerge which takes a set of dlls and combines them into a single dll. However, this didn't work. An exception was thrown at run time when the second SharedAssembly namespace was encountered.

I also tried installing the dlls in the GAC. However, it turns out you can't reference a dll in the GAC from Visual Studio: "This is because the GAC does not have full support for all design-time pieces of an assembly -- while you have access to the DLL itself, you will not have access to PDB symbols nor XML documentation files." (link)

Any ideas?

Wednesday, May 9, 2007

Blob performance concerns

Maybe someone can help me answer this question. I originally wrote about this on 3/2/2006 but never really found a good answer.

Suppose you are writing a program in C# that uses SQL Server 2005 and you'd like to store some files as Blobs (Binary Large OBjectS) in the database.

This is a very simple thing to do. Take the bytes, send them to SQL Server 2005 where you store them in a varbinary(max) column.

The code for this in C# looks like:

FileStream fs = new FileStream( "file" );
byte[] buff = new byte[fs.Length];
fs.Read( buff, 0, fs.Length );

SqlCommand sc = new SqlCommand(...);
sc.Parameters.Add( ...buff... );


My concern is that this code is going to read the entire file into memory. Then its going to send all that memory to SQL server in one shot. If this is a sufficiently large file I'd expect there to be problems on both the client and the server because of this.

If the client is somewhat lacking for memory, or if the file is really large, then I'd expect this code to cause a lot of disk paging. Possibly even paging out the beginning of the file you just read from disk to make room for the end of the file, just to turn around and page out the end of the file to make room for the beginning so it can send those bytes to SQL. I'm not clear such a thing could ever actually happen, but I do suspect some paging will occur.

What I'd really like to do is specify a stream as the parameter instead of a byte array. That way I could trust the driver was only loading a portion of the full file into a buffer at a time. It seems the JDBC driver supports this, but the SqlClient driver for ADO.NET doesn't.

So my questions are, am I blowing this way out of proportion? Is there a better way to insert a Blob in SQL from C#? Why does no one else on the internet address this concern?

Partly because of this problem, along with some other performance concerns, I ended up storing the files on a shared file server instead of as Blobs in SQL. This allowed to me to handle the transfer myself using the typical buffered approach.

Wednesday, April 11, 2007

Memory Leaks, Garbage Collection, and .NET

I have frequently read and been told that .NET (and Java) do not have memory leaks. Of course, this depends on the definition of a memory leak.

One definition says a memory leak occurs when a program allocates memory from the operating system but never gives it back. This is pretty clear, so I'm going to stick with it.

Why does .NET not have memory leaks? It has a garbage collector. The garbage collector checks your memory for you every so often to determine if it is still referenced anywhere. If it is, its assumed you're using it. If it isn't, you can't possibly be using it, and the memory is freed and given back to the system.

Therefore, languages with garbage collectors clearly can't have memory leaks, right? I'm going to show you why I disagree with that statement.

Lets look at a simple example in C#. Suppose you've written a class which acts as a DataTable cache. This will be a static class. The purpose of this class is to store DataTables which will be used by various controls throughout the application. If a DataTable gets updated, this class will update all the controls that use that DataTable.
public static class DataTableCache
{
Dictionary dtCache = new Dictionary();
Dictionary controlStore = new Dictionary();

public static void AddDataTable( string key, DataTable table, Control ctl )
{
if ( dtCache.ContainsKey( key ) )
{
dtCache[key] = table;
// update controls using key, if any exist yet
}
else
{
dtCache.Add( key, table );
// update controls using key, if any exist yet
}
AddControl( ctl, key );
}

public static void AddControl( Control ctl, string key )
{
controlStore.Add( ctl, key );
}

public static DataTable LookupDataTable( string key )
{
if ( dtCache.ContainsKey( key ) )
return dtCache[key];
else
return null;
}
}

I'm keeping this example as simple as possible, but it is modeled on real code. There is a "memory leak" problem here. Suppose you create a Form, f. On that form you add a ComboBox, cb. You obtain a DataTable which you use as cb's DataSource and you add them both to the DataTableCache. When f is closed it will be disposed along with all of its controls, including cb. However, because cb is still in the controlStore dictionary it will never be freed by the garbage collector. It has been disposed, but it is still referenced by our static DataTableCache.

No, don't worry, I'm not claiming this qualifies as a memory leak. This is simply programmer error. The programmer should have accounted for this problem. There are two obvious ways to fix this. The first might be to add a RemoveControl method and call that when the form closes. The second, and better approach, is to register the control's Disposed event in DataTableCache and remove it from the dictionary when that event handler is fired. And while we're at it, we'll also make the event handler check to see if any other controls are still using the disposed control's key after it is removed. If none are, we'll remove the DataTable too.

AddControl would now look like:

public static void AddControl( Control ctl, string key )
{
controlStore.Add( ctl, key );
ctl.Disposed += new EventHandler( ctl_Disposed );
}

And the event handler would look like:
private void ctl_Disposed( object sender, EventArgs e )
{
string key = controlStore[(Control)sender];
controlStore.Remove( (Control)sender );
if ( !controlStore.ContainsValue( key ) )
dtCache.Remove( key );
}

Problem solved. No more memory leak.
Actually, wrong. We still have a memory leak. The DataTable will be disposed and freed by the garbage collector, but the Control wont be. If you don't believe me, try it out yourself. You can use a memory profiler to see that the combo box remains in memory no matter how many times you run the garbage collector.

This is the memory leak! No where in our code do we have a reference to the combo box, so why isn't the garbage collector working? Well, it turns out its because we registered the Disposed event. Any time you register an event you get this little intermediate object behind the scenes which holds a reference to the object you registered the event on and the object you registered the event from. So if you have ever registered an event between two objects, where one of them had a longer lifetime than the other, then you have a memory leak. And if one of those objects is static, as in this example, then the memory wont be given back to the system until the application is closed.

How serious is this? It depends on the circumstances, but most of the time it will be pretty serious. In our example its very likely that the Form, f, had registered the ValueChanged event of the ComboBox, cb. That's a very common need. Because of this, both cb and f will never be freed. And imagine how many controls could potentially be on f. Now none of them can be freed either.

How do we solve this? Its very easy, we just modify the Disposed event handler as follows:
private void ctl_Disposed( object sender, EventArgs e )
{
string key = controlStore[(Control)sender];
controlStore.Remove( (Control)sender );
((Control)key).Disposed -= new EventHandler( ctl_Disposed );
if ( !controlStore.ContainsValue( key ) )
dtCache.Remove( key );
}

I consider this a memory leak because 1) its hard to consider this programmer error since its actually the .NET framework which contains the references and 2) this problem is very very hard to remember to avoid. This means you really do need to use a memory profiler on your applications before releasing them. And therefore I claim that C#, at least, does suffer from memory leaks.

I haven't had a chance to test this on any other languages. I'd be very interested to hear if the same is true in Java, Python, and Ruby for example.

Thursday, April 5, 2007

Fair Witness

In the book Stranger In a Strange Land, Robert Heinlein introduces the concept of a Fair Witness. In the book a Fair Witness is someone with complete memory recall (they remember EVERYTHING!) who makes no assumptions about anything. They're used for various legal things because they are infallible observers.

There's a quote from the book which I always liked. I can't remember it exactly but it goes something like:
"Anne, function as a Fair Witness! What color is that house over there?"
"This side is blue."
Obviously the other sides could be any color at all. But it wouldn't occur to you to think that way, and in most cases it wouldn't make sense to.

However, attempting to assume this kind of mind set can be enormously helpful for a software developer. How many times have you been tracking down a bug for hours and hours and hours, unable to figure out what's causing it. "Everything looks right!" you keep saying to yourself. Finally you discover the problem: you had made an assumption about some part of your code that turned out not to be true.

Sometimes its an assumption that you programmed into the code that turns out to be false. These are usually easier to find. Other times its an assumption you made in your mind about the behavior of the code. Could be, "I know this method is working fine" or "It will never do xyz because of abc." Then when you find the bug you realize that not only was your code wrong but you were wrong too. It was two bugs in one.

Of course, being human we can't avoid making assumptions. Assumptions are powerful for the same reason abstraction in Object Oriented Programming is powerful. It lets you forget about the details and focus on a higher level idea. But in my limited experience I've noticed that being human also comes with a tendency to want to rush. And nothing helps you rush more than making assumptions. So now when I set out to find a bug I always remind myself to function as a Fair Witness. I'll still make incorrect assumptions from time to time, but I'll avoid making a countless number of rush-assumptions. And ultimately, I'll end up saving myself time and frustration.

That being said, making assumptions in debugging can be a good thing too. You just have to make your assumptions in a very conscious manor. As in, this side of the house is blue, so I'm going to assume the other sides are as well. Then if you run into an inconsistency later on you'll remember you made the assumption and you can go back and examine if it was the right one. The author of The Old New Thing blog calls this Psychic debugging. I call it functioning as a more flexible Fair Witness.

Wednesday, April 4, 2007

Why?!

There are a few reasons for this blag.
  1. I like to write my opinions about software and technology like things.
  2. There is a wide community of people on the interweb who are writing similar things. This blog might allow me to join them.
  3. I have a website which includes a forum that I wrote in ASP over 5 years ago (as of this posting). Its been great fun, but it doesn't allow me to blog, as it were. Its too open and public. Its also hosted on a computer in my basement, which isn't terribly reliable.
  4. I thought I should learn how this blogging software worked.
I may port things I wrote on my original website to this blog if I feel like I'm getting something out of this. If I do, I'll indicate that's where it came from along with the original date in the interest of full disclosure.

And with that...