Friday, July 01, 2005

Usage Rules for NULL

A useful article Four rules on Nulls. Was a good read.

The article starts with explaining what NULL actually is and puts forth four rules on NULL usage.

Rule #1: Use NULLs to indicate unknown/missing information only. Do not use NULLs in place of zeroes, zero-length strings or other "known" blank values. Update your NULLs with proper information as soon as possible.

Rule #2: In ANSI SQL, NULL is not equal to anything, even other NULLs! Comparisons with NULL always result in UNKNOWN.

Rule #3: Use SET ANSI_NULLS ON, and always use ANSI Standard SQL Syntax for NULLs. Straying from the standard can cause problems including portability issues, incompatibility with existing code and databases and returning incorrect results.

Rule #4: The ANSI Standard COALESCE() and CASE syntaxes are preferred over ISNULL() or other proprietary syntax.

Via: Anand

Recommendations for using Strings in 2.0

I read an article in MSDN by Dave New Recommendations for Using Strings in Microsoft .NET 2.0. Extract below:

Code owners previously using the InvariantCulture for string comparison, casing, and sorting should strongly consider using a new set of String overloads in Microsoft .NET 2.0. Specifically, data that is designed to be culture-agnostic and linguistically irrelevant should begin specifying overloads using either the StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase members of the new StringComparison enumeration. These enforce a byte-by-byte comparison similar to strcmp that not only avoids bugs from linguistic interpretation of essentially symbolic strings, but provides better performance.

Recommendations for String Use
When developing with the 2.0 version of the .NET Framework, keeping a few very simple recommendations in mind will suffice to solve confusion about using strings.

Do's:

  1. Use StringComparison.Ordinal or OrdinalIgnoreCase for comparisons as your safe default for culture-agnostic string matching.
  2. Use StringComparison.Ordinal and OrdinalIgnoreCase comparisons for increased speed.
  3. Use StringComparison.CurrentCulture-based string operations when displaying the output to the user.
  4. Switch current use of string operations based on the invariant culture to use the non-linguistic StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase when the comparison is linguistically irrelevant (symbolic, for example).
  5. Use ToUpperInvariant rather than ToLowerInvariant when normalizing strings for comparison.

Dont's:

  1. Use overloads for string operations that don't explicitly or implicitly specify the string comparison mechanism.
  2. Use StringComparison.InvariantCulture-based string operations in most cases; one of the few exceptions would be persisting linguistically meaningful but culturally-agnostic data.
A read of the article would surely give an insight towards best practices for string comparison.... excellent article...

A No Mouse-Click WebSite!!!

This site is cool.... visit http://www.dontclick.it you will know it!!!

Via:Sudhakar