Skip to main content

Closing A Cursor in SQL Catch

· One min read

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:

Think For One ... Second

· One min read

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;
}
});

Unit Testing Functions That Call Microsoft Enterprise Logging

· 2 min read

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?

A Plurality of "Marriage"s

· 2 min read

The op-ed, Is My Marriage Gay? in the New York Times (5/11/09), describes the unusual state of affairs surrounding marriages where one partner has legally changed gender after the marriage was recognized by the state. The country is patchworked with statutes and laws that make such a marriage anywhere from fully recognized to partially tolerated to anathema. And this makes no sense to me, except in recognizing that people are afraid of what they do not know. Well, go hug a transgendered person today (in person or virtually if that's what it takes). Get to know her or him.

Sub classing for automated testing

· 2 min read

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.

Automatic Properties in C# 3.0

· One min read

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:

  1. This is great, I don't have to create a private field and write getter/setter in a public Property anymore.
  2. 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.

Nice technique for modifying a subset of a List<T>

· 2 min read

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.

Curly's Law and Questions for the Team

· One min read

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:

Unit Testing - Code Coverage and Separation of Layers

· 3 min read

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.

safnet logo