Learning a new language? Write some tests…

The excellent Pragmatic Programmer book suggests that you should learn a new language every year – this is something which I strongly agree with. By learning a new language it does not mean C# 4.0 (when you already know 2.0 and 3.0), or how to create Silverlight applications. Instead, make the effort to learn a language with a different mindset and approach to what your used to and actively engage in that community. Coming from a JavaC# background, Ruby was an eye-opener for me and approach to software development. The priorities and principals are different, for example Ruby has much more emphasise on elegance, solving problems and testability where software is treated as an art form while still being pragmatic in their approach – it’s never prefect and can be improved at some point. This importance is also being effectively communicated throughout the community from the Ruby guru’s such as Dave Thomas and Chad Fowler to the developers writing applications on a day-to-day basis. As a result of learning more about the Ruby language,

I feel I can approach a solution with a more opened minded approach.

How should you learn a new language?

This is something which I have been thinking about for a while, what is the most effective way to learn a new programming language? There are a couple of approaches you could take, but this is my approach (if you have your own suggestion, please leave it as a comment).

1) Buy a book, but not just any book – the best book. Personally, online materials are great but I still find the most effective way to learn something is to have a physical book by my side.  However, be sure to pick your book wisely, I recommend you research the influences within the community and either buy their book, or the book they recommend.  The wrong book could take you down completely the wrong path.

2) Start writing tests

Once you have picked your language and book of choice, you need to start grokking the concepts. After trying a couple of different techniques, I’ve found the best way to learn a new language is to actually write tests. This isn’t as crazy as it might sound, the test will define your expected outcome from your sample and give you something to aim for. This will help focus your mind on the task in hand, while giving you a clear signal as to when you are complete – the test will pass.

For example, with C#, if I wanted to know how to write a line of text to a file I might write the following test with the implementation.  

public class IO_Examples
    public void Write_Hello_World_To_The_File_HelloWorldTxt()
        MyFileAccess.Write(“Hello World”, “HelloWorld.txt”);

        Assert.True(File.ReadAllText(“HelloWorld.txt”).Contains(“Hello World”));

public class MyFileAccess
    public static void Write(string s, string file)
        StreamWriter streamWriter = new StreamWriter(file);

I have also found tests to be a much more natural starting point for interacting with a language, with C# my starting point was a command line application, however this wasn’t the most effective way of learning as I was constantly commenting out calls to various methods to execute the correct block of code. By using a test framework and a test runner as your starting point, I would have been able to run the samples more quickly and effectively while still keeping everything readable.

However, if your anything like me, while learning you will end up going off on a tangent or being distracted mid-task, I can recall too many occasions where I have been deep in the middle of learning the inners the underlying code only to read a blog post which takes me in a different direction and then completely forgetting where I was. By having a test, or a series of tests, guiding me I am able to quickly get back on track by seeing which tests are currently failing.  Because the tests will describe my aim, I will have a much better chance of remembering what I was actually doing.

Finally, once you start moving onto real applications and solving real problems using the language, you will be able to look back and refer to the tests as a reference regarding everything you learned.  If you keep your tests in a source control repository, you will even be able to see how you adapted over time. This could be extremely useful a year down the line where you want a very quick refresher.

3) Solve a problem!

Once you have an understanding of the language with a series of tests describing how to do various different tasks, the next thing to do is solve a problem you, or someone else, is experiencing having. While this isn’t always possible, it’s a great motivator to finish the job.  I enjoy automation and improving productivity (it allows me to spend more time on twitter), I find it really interesting to see how problems can be solved in a more effective fashion using tools and technologies – if a new language can help me with that then all the better. Not only will I learn the language in a more ‘real world’ context, but it will be helping me in the future.

Technorati Tags: , ,

.Net Fault Injection – It’s not just about exceptions

I had a interesting comment on my previous post about .Net fault injection. ‘Losing Side’ asked if this would work for simulating other faults such as timeouts. It’s a good point and one I didn’t think about yesterday, but there are other faults which are interesting when testing the application. Performance is one of those areas, creating performance problems, such as slow disk IO or a slow server is difficult if you don’t have the setup in place, and even then not always possible. How can you effectively, repeatability test for a slow hard drive (and using a virtual machine doesn’t count). Tools such as ANTS Profiler will help tell you where the problems are, but only if you can reproduce the problem.

First demo of the day, I ask the question – how can you simulate a slow write process when using StreamWriter?  Based on my previous post, I’ve changed the method to this:

private static void MethodFails()
    Console.WriteLine(“Writing to a file @ ” + DateTime.Now);

    string path = Path.GetTempFileName();
    StreamWriter sw = new StreamWriter(path);
    sw.WriteLine(“This is a test @ ” + DateTime.Now);
    sw.WriteLine(“This is a test @ ” + DateTime.Now);
    sw.WriteLine(“This is a test @ ” + DateTime.Now);
    sw.WriteLine(“This is a test @ ” + DateTime.Now);

    foreach (var s in File.ReadAllLines(path))

Running my console application normally, I get the following output, notice no delays between each write:

Writing to a file @ 15/11/2008 13:23:48
This is a test @ 15/11/2008 13:23:48
This is a test @ 15/11/2008 13:23:48
This is a test @ 15/11/2008 13:23:48
This is a test @ 15/11/2008 13:23:48

Running this using my injector, I have a five second delay (which I set, easily could have been a random number) between each of my writes to the file.

Writing to a file @ 15/11/2008 13:19:45
This is a test @ 15/11/2008 13:19:51
This is a test @ 15/11/2008 13:19:56
This is a test @ 15/11/2008 13:20:01
This is a test @ 15/11/2008 13:20:06

I now get the same five second delay each time this happens, creating a scenario repeatable.

Disappointingly, I tried to use the SQLConnection object, but I couldn’t get this to work. I don’t know if its a limitation or a bug.  Still a lot more work to do until its even remotely useable, but I’m finding the concepts interesting.

Technorati Tags: , ,

ReSharper 4.0 EAP – First nightly build 729

image EAP for ReSharper 4.0 is open so I thought I would post my initial impressions. On their blog, the announcement adds this comment:

That is, not fully fledged tool, but rather our current development bits. Which could be very well broken, buggy, hanging, slow and useless at times. We will not take any responsibility if the tool wipes out all your source code!”

You have been warned.

Download it from here: http://www.jetbrains.net/confluence/display/ReSharper/ReSharper+4.0+Nightly+Builds

The release notes contain information on all the current features:


Linq and partial methods are not currently supported, but the other features still make this worth it.

I initially installed this as an upgraded from 3.1.  Sadly, my 3.1 License is not acceptable as I got it before 20/12/2007 so I have to use the 30 day free evaluation. Not sure what is going to happen after 30 days with the EAP? Addin loaded, everything seems to be working.

Running over the menu’s I notice two new features, Profile Unit Tests which I guess will link into dotTrace – I wonder if this will profile the code being unit tested, or the unit test themselves…. I don’t have dotTrace installed so this option was disabled 🙁

In the options, they now also have Code cleanup profiles which are executed via Ctrl+E, Ctrl+C.  This allows you to define rules based on how you want to clean up your code (remove this, reformat code, optimise using etc).  Nice to be able to do this via a single command.

Various improvements to Intellisense improvements, it now understands var and Extension methods are detected and work well.

Annoymous types support is included. Given this code:

var o = new {Values = new string[] {“One”, “Two”, “Three”}};

If I put my cursor just before new, I get the option to create a named type.  This creates internal class of the class of your method:

public class AnonymousClass
    public string[] Values { get; set; }

public void Test()
    var o = new AnonymousClass() {Values = new string[] {“One”, “Two”, “Three”}};

You can also view view recent edits (Ctrl+shift+,) which then tells you the classes and methods you have recently changed and allows you to go to them which is cool.  Not sure if this is new, but it’s cool and I only just found it.

However, it’s not prefect. Couldn’t find any help, lucky I did find the release notes which answered all my questions.

  1. One or two bugs – http://www.jetbrains.net/jira/browse/RSRP-58041 After a while I just told it to ignore this exception as it was annoying me. (NOTE: This has been fixed in build 730 – the joy!)
  2. prop shortcut is not there for creating automatic properties.
  3. Automatic Properties doesn’t appear to be great,  didn’t tell me at various points when I expected to such as the following code.  Would have expected it to offer me to convert them for me. 

public string name;
public string address;
public int Age { get { return age; } set { age = value; } }

I’m sure there is more, but this is a good first build.  Can’t wait for some more…

Technorati Tags: , ,

Process.Start and first time launching Firefox

I was going over the MbUnit issue list and came across Issue 97. This had a really interesting scenario.  Basically, Firefox was the default web browser but didn’t have a profile associated with it (it hadn’t been executed yet) and when you try and launch it via Process.Start it will load firefox correctly however also throw a Win32Exception.

If you did something like this:


The exception would be:

System.ComponentModel.Win32Exception was unhandled
  Message=”The system cannot find the file specified”
       at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)
       at System.Diagnostics.Process.Start()
       at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
       at System.Diagnostics.Process.Start(String fileName)
       at ConsoleApplication1.Program.Main(String[] args) in ……Program.cs:line 13
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Very strange, if anyone knows the extract reason why it decides to thrown an exception please leave a comment. Definitely something to be aware might happen.  Wonder how many applications would crash because of this…

When testing your applications, if you want to reproduce this without having to load a clean VM, go into your %AppData% and move the Mozilla directory to another location.  Next time you load Firefox, the dialog will be displayed and the exception will be thrown.

Technorati Tags:

How to: Break when an exception is thrown.

While helping me to solve a threading issue, Robert mentioned this little gem.  Within Visual Studio, you can set it so that it breaks on all Exceptions (not just the unhandled exceptions). No longer do I need to keep stepping over code to see when an exception was thrown, I can simply bring up the exceptions dialog (ctrl + alt + E, or Debug > Exceptions) and tell it to break when a CLR Exception is thrown!  Now, as soon as an exception is thrown it will break at the point the exception was thrown.  You can then enable editing and see what was actually happening.


I knew it could be done, but had completely forgotten how/where to set it, where to the point I had forgotten it could be done…

Happy debugging!

Technorati Tags: ,

Finding assembliesmodules currently in use

Today I was trying to debug a problem and needed to see if locking was the issue.  Quick search online and I came across the tasklist command line application.  This lists all of the applications currently open together with the modules they have loaded.

To execute the list, open a command line window and type

tasklist -m

This will return everything.  If you want to stop and see everything you will need to use the more pipe.

tasklist -m | more

If you want something a bit more useful, type in the module your interested in.  This will then return only the applications open which has the module loaded.

tasklist -m MbUnit.Framework.dll

Finnally, to see a particular application with all the modules loaded you need to use a filter based on the IMAGENAME.

tasklist /FI “IMAGENAME eq MbUnit.GUI.vshost.exe” -m

I thought it was cool! 🙂

Technorati Tags: ,

NotifyIcon and Always Hide setting

Yesterday, I had another one of those moments with the NotifyIcon class in C#.

During testing, if the icon was set to always hide under Windows XP then it would simply never display in the task bar.  This I thought was very strange as it should be in the systray, just not visible when its in the compact state.

The reason? Well on a balloon close event I would also remove the icon from the systray as it had served its purpose. However, when an icon is set to Always Hide, when a balloon tip is shown it is instantly closed (by XP Shell) which causes the balloon close event to be raised which in turn removes the icon from the systray. 

I should have realised sooner but simply didn’t link the two actions. Next time in situations like this, always check events which hook up to the close event.

Technorati Tags:

C# Null Coalescing Operator (??) and String.Empty

One of the features of C# 2.0 is the null coalescing operator (??) which says that if the value is not null, then use it otherwise use another value.

For example, if we wanted to use the myCust variable if it wasn’t null, or if it was use order.Cust then we would write.

Customer c = myCust ?? order.Cust;

Using 1.1, we would use the ternary operator (?) and it would have been

Customer c = if(myCust != null) ? myCust : order.Cust

We could have chained this together, so we could also check order.Cust to see if that was null and provide an alternative to that.

However, one problem I had was with String.Empty.  I wanted to test to see if a string was returned from a method and if it wasn’t, use the system default.

String importantStr = myObject.GetImportantString() ?? sysDefault;

The problem is that myObject.GetImportantString() returns string.empty if it cannot be found. The operator doesn’t treat string.empty as a null and uses it as the variable value. Not what I wanted at all! 

Sadly, I had to go back and use the ternary operator.

String importantStr = if(!string.IsNullOrEmpty(myObject.GetImportantString()) ? myObject.GetImportantString() : sysDefault;

Sadly, myObject.GetImportantString() is an expensive call so calling it twice is bad. So I had to resort to the old way of doing things

string temp = myObject.GetImportantString();
string importantStr = sysDefault;
   importantStr = temp;

Oh well, still a useful operator.  Just not for strings…

Technorati Tags:

Balloon Tips, NotifyIcon and Windows 2000

While i’m sure this isn’t relevant to most of my readers, again this has bugged me on and off for the last hour so thought I would blog for future reference for anyone else.

I’ve got a nice working SysTray application which displays a balloon tip message, that when a user clicks the tip it launches another separate dialog.  This worked great on Vista and XP however I decided just to do a little test on Windows 2000 (yes, its still used!).  I found that when you click the balloon tip on 2000, nothing happens.  Turns out this is because the shell_NotifyIcon is a different version on 2000 than XP/Vista and the event isn’t being handled correctly.

The only way I found to fix this is no to only have the balloon tip click (BalloonTipClicked) event wired up but also the Click event on the actual NotifyIcon class wired up as well.  This way, when the user clicks the icon is will display.  Sadly, if they click the Balloon tip nothing will happen but at least there is still some support for 2000 in place.

I then found that on Vista/XP where it worked correctly before, now when the systray icon is clicked both the BalloonTipClicked and the NotifyIcon.Click event are fired, hence opening the form twice!  Quick solution was to use a boolean member variable which knows if the form is currently visible or not.  Nothing fancy…

Technorati Tags:

NotifyIcon icon still visible after closing form

I”ve been playing around with the NotifyIcon class within C# recently but been having one major issue with it.  The class itself allows you to place an icon in the systray (down by the clock) and for it to display the standard balloon tip dialog.

However, the problem I was having is that when my calling parent closed my NotifyIcon would still be in my SysTray.  The only time it was removed was when I hovered over the icon with my mouse, then it would automatically disappear. Not great from the users point of view!

The MSDN page says nothing about this however I did find a useful forum post on the subject.  The solution is to manually call dispose and more importantly, set the variable to null.

The code is shown below, the notifyIcon variable is a member variable (private NotifyIcon m_notify;)

protected override void Dispose(bool disposing)
    if(m_notify != null)
        m_notify.Visible = false;
        m_notify = null;

    if (disposing && (components != null))

After inserting this code, when my parent form is closed the notify icon also disappears.  This took me a while to find out about, hopefully I have saved you some time.

Technorati Tags: