Tuesday, October 10, 2006

Today I have realized that often when I'm writing code I'm using some skeleton methods that will encapsulate some part of the functionality that I don't really want to write right now. So for example I have a main code with a call to a method that does nothing, because its internals have not been defined yet as in the following example:

public int Calculate()
   int x = 1;
int y = DoSomething(x);
return y + 1;
private int DoSomething(int x)
throw new NotImplementedException();

The important thing here is the use of the "throw new NotImplementedException();" since it will ensure that in the future, at runtime I will not forget to implement the "DoSomething" method. This is the way I really recommend you to do instead of returning 0 or null value, because then you WILL forget about it.

Fortunately Visual Studio 2005 comes with some refactoring tools that enable you to generate the "DoSomething" method when you wrtite the line "int y = DoSomething(x);" via smart tag. It will even have a default implementation which throws an exception although of type Exception - which I don't understand. There are however situations when you write the method yourself and for this I have created a simpliest code snippet ever. It outputs the "throw new NotImplementedException();" frament inside your method. To use it just type nimpl from "Not Implemented".

The snippet is available in the package with all my other snippets that I use on daily basis. You can read more on the code snippets in my other article: Visual Studio Code Snippets.

kick it on DotNetKicks.com