Monday, 02 February 2004

"Best Practices"?

Is there something I'm missing here? Why call Close() and Dispose()?

If there isn't any good reason, then I'm wondering just how much the authors of the book could really know about "best practices." Is it a best practice to include unnecessary code just because you've never bothered to look at how Dispose() works? [Rory Blyth]

I recently saw someone add something like the following in a demo:

SqlConnection connection = new SqlConnection(...);
connection.Open();
// code that uses the connection...
connection.Close();

He then said that in production code he should really protect that in a try...catch but it was easier and cleaner to skip the try catch for the demo. I always wonder why people don't just do this:

using(SqlConnection connection = new SqlConnection(...))
{
// code that uses the connection...
}

Of course true best practices dictate your code should only touch connections in a single location*. But I digress.

* I didn't say I follow this best practice all the time...but I digress even further. ;-)

02/02/2004 14:41:28 (Central Standard Time, UTC-06:00)  #    Trackback