The New String recommendations

by Michael S. Kaplan, published on 2005/06/02 03:00 -04:00, original URI: http://blogs.msdn.com/b/michkap/archive/2005/06/02/424128.aspx


Dave Fetterman reported yesterday on the Official Guidance: New Recommendations for Strings in .NET 2.0 (full paper here).

Now this is a paper whose recommendations I think are incredibly important (some of them were I daresay inspired by things I have been saying here about invariant versus ordinal and using uppercasing!). And I think at the core of those recommendations is a principle that applies to all code that is written, managed and unmanaged, in any version of any product. So I don't want people to think "I'm not using Whidbey, so this does not apply to me."

That core principle? Stated simply....

Use appropriate comparison methods.

It simply makes no sense to use the wrong method, ever.

Now as I have mentioned before, I am a bigger fan of what I call the 'vertical method' (different flag values in a single method or function) as opposed to the 'horizontal method' (different methods or functions). I find it easier to explain, easier to document, and easier for a user to know what to call. I explain this in part for unmanaged code in my post Similar descriptions does not mean similar methodologies and some day soon I will follow up with a managed version of that post.

But it is important no matter whether you are a 'vertical' person or a 'horizontal' one to choose appropriate methods to compare based on your actual scenario.

 

This post brought to you by "๚" (U+0e5a, a.k.a. THAI CHARACTER ANGKHANKHU)


# Maurits [MSFT] on 2 Jun 2005 10:28 AM:

"Case-insensitive identifiers in standards like XML and HTTP"

Whoa! HTTP is case-insensitive, but XML is case-sensitive. So is XHTML.

# Michael S. Kaplan on 2 Jun 2005 12:11 PM:

Well, yes and no -- a lot of the things that use XML are themselves case insensitive, or give the option to be. And even then there are the normalization issues -- which can still point to more appropriate sdtring comparison methods. :-)

# Ben on 2 Jun 2005 1:27 PM:

I love it! I wish someone would go ahead and write this exact article for using non-.NET APIs as well.

# Michael S. Kaplan on 2 Jun 2005 3:07 PM:

There is a brief mention towards the end of the paper:

----------------------------
Notes for Native Code

Native code is susceptible to similar types of errors, but they occur much less commonly. Default behaviors of string operations are not based on the locale, but are typically ordinal-based (strcmp or wcscmp, for example). Our recommendations for using managed code mirror this behavior. Finally, where linguistic flexibility is desirable, culture parameters can typically be passed in (see CompareString).
----------------------------

Beyond that, I have posted on the issue here from time to time and there is work to improve the documentation on the Windows side in future versions of the Platform SDK.

# kurakuraninja on 5 Jul 2005 12:51 PM:

Since these immortal words were spoken by the voice of Tim Blaney to Ally Sheedy, I think every...

Please consider a donation to keep this archive running, maintained and free of advertising.
Donate €20 or more to receive an offline copy of the whole archive including all images.

referenced by

2006/08/16 Something .NET does more intuitively than Windows

2006/08/12 Getting string comparisons of file names right

2005/07/05 'Need more input, Stephanie!'

2005/06/12 Browsing the shoals of managed string comparisons

2005/06/07 My first (bloggers) geek dinner and the sequelae, at Orlando TechEd 2005

2005/06/05 The dasBlog 'Turkish I' thing figured out

2005/06/05 What will I answer questions about?

go to newer or older post, or back to index or month or day