Posts

Showing posts from 2009

Programming tidbit - be careful with the null coalescing operator on strings

One of my favorite shortcuts in programming with C# is the null coalescing operator, better known as '??'. var something = aString ?? "somethingelse"; Is a shortcut to... var something = aString == null ? "somethingelse" : aString; However, it is important to note that it does not take care of the empty string case, which is also vital to check for... var something = string.IsNullOrEmpty(aString) ? "somethingelse" : aString; Unless you can absolutely guarantee that a string will either be null or have a value, the '??' operator then becomes useless (sadly) for strings. EDIT:  As of .NET 3.5, you cannot overload this operator, so I would not recommend using this operator with strings, unless an empty string is acceptable.

10 Best Practices for Converting a Datasets-based App to LINQ to SQL

While experiencing the major headache of converting our existing web app to use LINQ to SQL instead of datasets (b/c we'll be switching to a Silverlight front-end using WCF services), I started making a list in my head of all the things I found really helped me get this conversion done as quickly as possible. Although the pain was unavoidable, it was greatly lessened by following some simple guidelines. The guidelines I have below are mix of LINQ specific guidelines, and generic data access layer guidelines. First, understand that we are a Microsoft shop, completely reliant on stored procs. With this in mind, it should be needless to say (but I'll say it anyway) that I really didn't have to/get to do too much LINQ, as in LINQ queries. The guidelines I will outline below are centered around this idea. 1. Put your entire database into one dbml. As far as I was able to determine, it is not possible to do cross-context LINQ queries. Even if you are using nothing but store

How discoverable and usable is your PERL module?

I am working on using the IMAPI2 COM component in one of our products for an improved data burning experience. After exhaustively searching for anyone's C# port of the COM error codes laid out by Microsoft, I came up dry. There is a C++ header file included with the Windows SDK, but that's doesn't help me in C# where I wanted to use an enum (uint). There are 85+ error codes and I also wanted to easily link them to resource file descriptions of the error for localization purposes and it would mean a very tedious import of each enum as the lookup for the string, coupled with 85+ copy/pastes. Sound like a job for PERL? Well, I agree. I haven't used PERL for several months and saw this as an opportunity to pull out my Swiss Army Knife of programming. I am posting this to show how little code there is, and yet how long it took me to get it working (took my about 3 1/2 hours to get the script to run perfectly). Does 3 1/2 hours sound like an incredibly long amount o

35x Improved T-SQL LevenShtein Distance Algorithm...at a cost

At work, we noticed a considerable performance hit using a T-SQL implementation of the Levenshtein algorithm created by Michael Gilleland found here . It seems to me that his approach was to replicate the C-code algorithm found in Wikipedia in T-SQL rather than taking a step back and re-conceptualizing the algorithm from a T-SQL standpoint. Feel free to correct me, but it see ms that the algorithm focues on three things to find the shortest distance between two strings (what it takes to make them the same): How many character insertions are necessary How many character deletions are necessary How many character substitutions are necessary These three traits are necessary to accommodate mis-aligned strings because of typos. If the algorithm is not designed with these constraints, one missing letter will result in inaccurate reporting because it throws the indexing off. The C-code example in Wikipedia uses a 2-D matrix approach, which is a natural, compact, and performant fit for

MSSQL Express, MySQL, or db4o?

In designing my first scalable, flashy, performant (yes, I'll use this word I guess) internet app, I spent quite a bit of time in the database exploration realm, which really comes down to one question on a no-budget venture like I'm beginning with: open source or free? Deciding on a database for a web app is like deciding on the materials for the building you're designing - mui importante. When I took a database class in graduate school (nope, didn't know much about them until then), we exclusively used MySQL with a C API, while developing (supposedly) on a Linux platform। We weren't even allowed to use C++ and I still think my professor believes that C is better than C++ in every way...but I digress। Knowing of the ridiculous (yes, ridiculous because I have used them extensively) debuggers available on Linux (clunky, buggy, slow, incompatible with various g++ compilation types, etc.), I decided to develop my programs on Windows using VS2003 (with a free academic