December 2, 2008

Visual Studio vs. NUnit unit tests

I've used NUnit over the last few years at a few jobs to do unit testing in C# code. At my current job we started trying to use the unit test features of Visual Studio 2008. We had been using NUnit but we needed a solution that would work with our new C# code, as well as testing our existing legacy C++ code. The C++ code we have is primarily unmanaged cross platform code that is used in both Windows and Linux applications.

Everything we had read indicated that the Visual Studio unit testing didn't work very well with unmanaged code. At first this seemed true, but we have been able to work around this well enough by just simply setting project settings to allow executing mixed mode code.

One of the biggest issues has been trying to debug into unit tests that are throwing exceptions. Because the unit tests are managed code, and most of the code being tested is unmanged, almost all of the exceptions come across as SEHExceptions. This can make it more difficult to determine what is going on, but can be overcome by going to Debug->Exceptions and checking some of the exceptions to break the code when the exceptions are thrown.

Other than that, the biggest concerns I've had with the Visual Studio unit test system is that they seem to have been over-engineered (Microsoft would never do that, right?). Pretty much everything seems to have been copied from NUnit, except it is harder to use. Just trying to determine how to run a specific test or tests is a pain. NUnit is just very simple and easy to use.

So far everything is working OK, and at least we have one solution that works for both C# and unmanaged c++ unit tests, but I still miss the ease and simplicity of NUnit.

[Update: 2/6/2009]
We decided to use Visual Studio unit test for C++, but after more debate decided that NUnit is a far superior solution. It just works and it works well. Visual Studio works, but not as well and not as easily.

November 12, 2008

Stack unrolling for exceptions in managed/unmanaged C++ code

Trying to mix managed and unmanged C++ is not pleasant. At my current job we have been re-architecting some of our core business logic. One of the focuses has been to make sure the code is set up to behave well with our MySQL database using transactions.

To facilitate this we have set up an object to help manage and maintain the transactions. If the code throws an exception, the transaction object automatically gets destructed as the stack unrolls, causing a rollback. This had been tested and was working great in our unmanaged code.

One of the GUI applications we maintain runs in managed C++ mode, but still uses our underlying unmanaged C++ business logic. While testing this application we found that when the business logic would throw an exception, the code was not properly rolling back transactions in the database. After hours and hours of debugging and research we determined that the Microsoft compiler doesn't seem to unroll the stack properly when crossing the managed/unmanged code boundary. We found posts indicating other bugs with this across this boundary, some of which had been fixed in Visual Studio. The only way we were able to get the code to behave properly, was to wrap every function that uses database transactions and is called directly from the managed code in a try-catch block, that just rethrows the exception. Like this:
void DatabaseTransactionFunction()
{
try
{
TransactionHelper t();
t.Start();
//Code that may throw an exception
t.Commit();
}
catch (std::exception&)
{
//This forces the destructor on t to be called
//which rolls back the database transaction.
throw;
}
}
This is the only way the code would work. Seems unnecessary, and the code works fine if it is called by unmanaged code. So that seems to leave something with the Visual Studio compiler. Everything I've seen and read indicates that the compiler should behave like we think, The destructor on object t should be called when an exception is thrown, which would roll back the database transaction.

I would make the argument that Microsoft should fix these things, but all of the other issues I've seen with using managed and unmanged c++ code together would just make me recommend avoiding it. Use C++ for unmanged applications and stick with C# for unmanged code, and don;t mix them.

July 31, 2008

Amazon web services

I wanted an excuse to write some code. Seem like I never get any chances to write fun stuff anymore. So I decided to look into the Amazon associates program and put together a simple application for browsing books. It's pretty basic and just shows the amazon book categories and sub categories, with the 10 most popular books in each category.

Hopefully I'll get some more time at some point to flesh it out a little bit, but who knows. At least I think I have all of the bugs worked out.

May 30, 2008

eBooks and eReaders

For a few months now I've been thinking about getting an eReader. The Sony Reader seems nice, and it has been around for a while, but Amazon Kindle has been getting a lot of press since it came out. Seems like it has a few too many features for my tastes (I just want a basic device that I can load books onto.)

Take a look as these two and let me know if any of you have any preference, or any other good ideas.

May 29, 2008

Technorati

Signed up for Technorati to see what I can do to increase visibility to the blog.

Technorati Profile

Developer Efficiency (C# vs. C++)

Ever have one of those nights where you can't sleep. Sometimes me brain will not stop thinking so I can fall asleep.

Lately at work I have been working on adding new features to our companies web site. We recently rewrote the site in C# and ASP .NET which has been wonderful. I've been able to use C# for the past 6 years or so, and overall find it better than Java, C/C++, and any other language I know for general purpose programming. Other languages may have some better features than C#, but overall I prefer C#.

Unfortunately most of our code base is in C++. Every time I have to go back and work in our old code base I feel like I hit a brick wall. I just feel so much more productive and effective when I use C#. Before everyone starts yelling about the merits of C++ let me clarify.
  1. Our C++ code base is not very well organized or designed
  2. I have much more experience with C# (Though I'm not a C++ novice either)
  3. The added features/power of C++ are not worth the extra complexity most of the time
C# has most of the important features from C++ and many that C++ doesn't have. There are very few cases where I wish I had something extra in C# that is in C++, but the opposite happens quite frequently. With modern computers today, what little performance hit you get from using a memory managed language that complies to byte code, is far outweighed by the many benefits (such as programmer efficiency) gained.

Also I find it much easier to move from one project to another and one company to another when using the .NET framework. The .NET framework includes most of the standard features/classes/functions I need on a daily basis. It doesn't have everything, but close enough for most things. With C++ there is the STL, but beyond that different companies use different libraries for everything else, and often write some of their own libraries. If we hire a developer familiar with C# I know that they should be right at home in our new C# code base, because most of what we use is already in the .NET framework. If we need someone for C++ then we either make sure that already know all of the different 3rd party libraries we use, or we assume it will take them a while to get up to speed. Regardless they will have to get up to speed on our in house C++ libraries.

From both a technical and managerial standpoint I am glad we are moving more and more towards C# and away from C++.

May 20, 2008

Changing Passwords for local windows accounts

At work today some of the IT guys were talking about needing to change the local Administrator accounts on a bunch of the computers. They were planning on updating each password manually by going to each computer and logging in. The computers are all part of a domain, but apparently Microsoft hasn't made an easy way to update local machine information like this.

I remembered seeing an article in Microsoft TechNet magazine about how to automate this. I thought it had been windows powershell script, but it wasn't. Once I found the article, it ended up being a visual basic script for Excel. All you need to do is provide a list of computer names, and give it a new password to set. It's not as simple or easy to use as a standalone application, but all of the apps I found didn't look very good, or cost more than they should. If someone knows of a good free application or even better an open source application (maybe C#), let me know.

May 19, 2008

The Beginning

I finally decided to break down and start a blog. I've been thinking about it for a while, I was just trying to decide what platform to use. Since I am a software engineer I had considered writing some sort of basic blog/CMS system, but decided against it for now. Coding is fun, but it just seems like I don't ever have enough time to do what I need to, let alone everything else. Who knows what will really end up here (assuming I keep posting).