Limegarden.net Personal site of Wouter Lindenhof

19Aug/100

Extending exceptions

Sometimes you have a function which needs to communicate back on failure through special exceptions as you want to program have the option to fix certain functions. For example a formula whose input parameter is bad (example: begin date is after the end date) or when the database throws up or anything.

Now you could do the following:


public class EndDateBeforeBeginDateException : Exception 	{ }

class example
{
	void SomethingWithTime(DateTime begin, DateTime end)
	{
		if (begin > end) throw new EndDateBeforeBeginDateException();
		// ... do something with time ....
	}

	void DemonstrateFunctionTriesToCorrectInsteadOfFail()
	{
		DateTime input_begin = new DateTime();
		DateTime input_end = input_begin.AddDays(-1); // WRONG DATE ON PURPOSE!
		do
		{
			try
			{
				SomethingWithTime(input_begin, input_end);
			}
			catch (EndDateBeforeBeginDateException)
			{
				// Date was most likely wrong ask user
				var r = MessageBox.Show("The end date was before the begin date."
					+ "Do you want to switch them and try again?",
					"Switch dates?",
					MessageBoxButtons.YesNo);

				if (r != DialogResult.No) { return; } // quit function
				var temp = input_begin;
				input_begin = input_end;
				input_end = temp;
				continue; // Try again now
			}
		} while (false);
	}
}

But what if you have a lot of parameters. You don't want to create thousands of exceptions. Well, you can try using tagged exceptions which looks like this.


public class TaggedException<T> : Exception { }

struct _EndDateBeforeBeginDateTag{}
struct _SameDateTag {}
struct _TimeDoesNotExistTag {}
struct _ThinkOfATagOnYourOwn {}

class example
{
	void SomethingWithTime(DateTime begin, DateTime end)
	{
		if (begin > end)  throw new TaggedException<_EndDateBeforeBeginDateTag>();
		if (begin == end) throw new TaggedException<_SameDateTag>();
		// ... do something with time ....
	}

	void DemonstrateFunctionTriesToCorrectInsteadOfFail()
	{
		DateTime input_begin = new DateTime();
		DateTime input_end = input_begin.AddDays(-1); // WRONG DATE ON PURPOSE!
		do
		{
			try
			{
				SomethingWithTime(input_begin, input_end);
			}
			catch (TaggedException<_EndDateBeforeBeginDateTag>)
			{
				// Date was most likely wrong ask user
				var r = MessageBox.Show("The end date was before the begin date."
					+ "Do you want to switch them and try again?",
					"Switch dates?",
					MessageBoxButtons.YesNo);

				if (r != DialogResult.No) { return; } // quit function
				var temp = input_begin;
				input_begin = input_end;
				input_end = temp;
				continue; // Try again now
			}catch (TaggedException<_SameDateTag>)
			{
				// Force the user to change one date
			}catch (TaggedException<_ThinkOfATagOnYourOwn>)
			{
				// Tag seems obvious enough *wink*
			}
		} while (false);
	}
}

Now you only have one Exception in your code though you can quickly and without having to write much code.
(And if you are smart you store extra info the tag which is stored in the exception).

The same trick also works in C++ using templates.

24Jun/100

Splash screens

I always have my doubts about splash screens. As far as I know the majority of the splash screens have no function. A minority does have a function (they do background loading).

The main purpose of splash screen is, in my opinion, advertising. For a few second the user sees the name of the application and the company behind it. I admit that you can use it to load things in the background, but showing a splash screen also takes time. In that case you are better off loading the application and showing a progress bar. At that point the user sees it as the application is loading content. If you are using a splash screen for that (and I'm going to assume it makes no difference in performance or time that it requires) than the users perceives it as an annoyance.
This is a weird thing. Because showing a splash screen that does nothing but is only showed for a 2 seconds after which the application starts loading is seemed to be less intrusive than a splash screen that takes longer but actually causes the application to load faster (it doesn't require two useless seconds).

So how do you create a splash screen?
First of all start with a small image (take the dimensions of the office 2007 splash screens), add some abstract art (not something complex). Then create a form without borders in C# that closes after a few seconds and then modify the program.cs so that it looks like this.


using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Client
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new SplashScreen()); // load the splash screen
            Application.Run(new MainWindow()); // load the application
        }
    }
}

I know it's a cheap trick, but it looks nice if you do it well. And most customers care a lot about appearance.