Closing A Cursor in SQL Catch June 29, 2009
Problem: In a T-SQL script, an exception occurs while a cursor is open, resulting in the cursor never being closed. But, the exception handling wraps the entire script, not just the cursor, so there is no guarantee that the cursor will be open if/when the CATCH statement is reached.
Solution: query the sys.syscursors view to see if the cursor(s) in question is still open:
BEGIN CATCH
...
IF EXISTS (SELECT 1 FROM sys.syscursors WHERE cursor_name = 'MyCursor')
BEGIN
DEALLOCATE MyCursor
END
...
END CATCH
Posted by Stephen at 10:42 AM | Comments (0)
Think For One ... Second June 20, 2009
What’s wrong with this code? There are unnecessary lines. So? Why care about unnecessary lines? Because it shows that the programmer was not really thinking about what he was doing.
MyObject obj = someList.Find(delegate(MyObject test)
{
if (test.Id.Equals(packageId))
{
return true;
}
else
{
return false;
}
});
Continue reading "Think For One ... Second"
Posted by Stephen at 7:58 AM | Comments (0) | TrackBack (0)
Unit Testing Functions That Call Microsoft Enterprise Logging May 27, 2009
Problem: you have a method that logs a message using the Microsoft Patterns &
Practices Enterprise Library Logging Block, and you would like to write an automated unit
test for it in NUnit (or Team System – solution is easily adapted). Logging to a flat file
doesn't work; the file is still open when you try to read it to verify that the message has
been logged. Logging to the database doesn't work; it seems the log isn't written to the
database immediately. And so forth. Is there an in-memory way of reading the logged message?
Continue reading "Unit Testing Functions That Call Microsoft Enterprise Logging"
Posted by Stephen at 9:53 PM | Comments (0) | TrackBack (0)
Sub classing for automated testing April 19, 2009
A few months after I first purchased it, I am still reading xUnit Test Patterns. Been reading a few pages every day – now on page 590 with a few hundred to go!
I have finally arrived at the point where the author describes the pattern Test Specific Subclass (TSS). This is a pattern we have used extensively in our testing at the office, so that we can access protected methods in our classes. However, we stumbled upon it on our own, well before reading anyone else's suggestions on how to apply it. That's the nature of patterns for you.
Continue reading "Sub classing for automated testing"
Posted by Stephen at 3:55 PM | Comments (0)
Automatic Properties in C# 3.0 April 2, 2009
We just upgraded our servers to support .Net 3.x, so at last I'm able to start migrating some of my code. I haven't taken a close look at all the features available yet, but one that caught my eye and initially excited me is automatic properties. However, I had two conflicting reactions:
- This is great, I don’t have to create a private field and write getter/setter in a public Property anymore.
- But then what’s the point of not just creating a public field and using it directly?
Well, this article addresses a primary benefit: this facilitates refactoring. If, for instance, we find later on that we need something more advanced than a simple get or set statement, then we can add it without breaking the interface. I'm sold.
Posted by Stephen at 10:39 PM | Comments (0) | TrackBack (0)
Nice technique for modifying a subset of a List March 4, 2009
One of my team members sent in the following piece of code, which is clearly
intended to update the OrderNumber field for all objects in a
List<T> of
objects that match a particular productId. I took one look at it and thought
"you can't do that!". But then I let the automated test run to see what
happens... lo and behold, it worked. And well it should, once I thought about
it.
Continue reading "Nice technique for modifying a subset of a List"
Posted by Stephen at 4:27 PM | Comments (0) | TrackBack (0)
Curly's Law and Questions for the Team February 16, 2009
Coding Horror, almost two years back, christened a name for the principle variously known as "don't repeat yourself," "once and only once," etc.: Curly's Law. Excellent formulation of the principle. In sending this to my development team, I thought it would be useful to pull this from the abstract to the specific through a few questions:
Continue reading "Curly's Law and Questions for the Team"
Posted by Stephen at 12:54 PM | Comments (0) | TrackBack (0)
Unit Testing - Code Coverage and Separation of Layers February 5, 2009
Lately I've been working with my team to understand and utilize good
automated unit testing strategies with NUnit.
A code release I was inspecting revealed a couple of good points that seem worth
expanding on: the importance of testing each layer, and the need to pay
attention to code coverage. This is a rather facile treatment; for more in-depth
reasoning and details, I recommend xUnit
Test Patterns.
Continue reading "Unit Testing - Code Coverage and Separation of Layers"
Posted by Stephen at 12:13 PM | Comments (0) | TrackBack (0)