Friday, May 19, 2006

Question mark: The ternary operator ?

You want to write short blocks of code and you want to write readable code. Usually, there's a fine line, as you don't want your code looking like the works of a l33t h4X0r script kid or a python minimalist.

A fine way of both shortening and simplifying is if you use the question mark. It's called a ternary operator. With this, you can do conditional assignments on one line.

Consider the following example (taken from Bresenhams line algorithm) :



int ystep;
if(y0<y1)
{
ystep = 1;
}
else
{
ystep = -1;
}



The code above could be shortened to one line:


int ystep = y0 < y1 ? 1 : -1;


Tuesday, May 16, 2006

Custom Exceptions

Custom Exceptions

Exceptions are your friends. So is garbage collection :D Well, you shouldn't really ignore Exceptions, in stead, if you just learn yourself a little about them, your code could be easier to understand and easier to debug.

Custom Exceptions allows you to make more narrow, specific Exceptions with customized messages. This often makes it easier to pinpoint where your application fails from a higher persepective. By using InnerExceptions, you can also track back the original error message.

In the following example, I throw my own custom Exception object using the caught DataAdapterException as an InnerException.



try
{
myConnection.Open();
myDataAdapter.Fill(ds_records);
}

catch(Exception err)
{
throw new MyCustomException(
"Database error: Unable to fill dataset", err);
}

finally
{
connection.Close();
}



Now lets make the MyCustomException. First off, lets make a base class _ErrorException which inherits from the Exception class and write the following methods. We'll make the class Serializable, so that .NET is able to serialize the Exception. If the exception is ever used in a remoting context, exceptions on the server are serialized and remoted back to the client proxy. These base constructors are called each time we create a new Exception.



[Serializable]
public class _ErrorException : Exception
{
public string ErrorMessage
{
get
{
return base.Message.ToString();
}
}

public _ErrorException(string errorMessage)
: base(errorMessage) {}

public _ErrorException(string errorMessage, Exception innerEx)
: base(errorMessage, innerEx) {}
}

Now lets create a class MyCustomException which inherits from _ErrorException:

public class MyCustomException : _ErrorException
{
public InternalErrorException(string errorMessage)
: base(errorMessage) {}

public InternalErrorException(string errorMessage, Exception innerEx)
: base(errorMessage, innerEx) {}
}


That's about it. Now, from the business layer of my application, I can track back to the original DataAdapterException and its original error message, but also find out other places in the code affected by the error.

Test-Driven Development (TDD)

TheCodeProject has a great article on Test-Driven Development in .NET. The article explains the basics of TDD, the most common attributes, class setup and the NUnit GUI.

TheCodeProject:Test-Driven Developmen in .NET

Managed Direct X tutorials

The massive amount of articles, downloads, webcasts and product sites on the MSDN network sometimes makes it a daunting task to find usefull information about a specific topic.

Looking for resources on the latest managed Direct X, I didn't manage to find anything really usefull off the Direct X MSDN home page, but half an hour later, Google had directed me to these great tutorials:

Microsoft: Code4fun

Microsoft: Video Game Development (webcasts)

Other resources:

Direct3 D Tutorial Index: Craig Andera's DirectX Wiki

Welcome to C# Tutorial

Welcome to C# Tutorial. This is the workblog for Anders Nygaard, and hopefully a usefull resource blog for both you, the reader and myself. I'll start off with a low level of ambition, this is not going to be your complete resource for C#, but rather a collection of usefull tips and tricks that I stumble upon.

Nuff said, back to work :D