views:

42

answers:

2

According to the guidance published in New Recommendations for Using Strings in Microsoft .NET 2.0, the data in a string may exhibit one of the following types of behavior:

  1. A non-linguistic identifier, where bytes match exactly.
  2. A non-linguistic identifier, where case is irrelevant, especially a piece of data stored in most Microsoft Windows system services.
  3. Culturally-agnostic data, which still is linguistically relevant.
  4. Data that requires local linguistic customs.

Given that, I'd like to know the best way to communicate which behavior is expected of a string parameter in a public API. I wasn't able to find an answer in the Framework Design Guidelines.

Consider the following methods:

   f(string this_is_a_linguistic_string)
   g(string this_is_a_symbolic_identifier_so_use_ordinal_compares)

Is variable naming and XML documentation the best I can do? Could I use attributes in some way to mark the requirements of the string?

Now consider the following case:

   h(Dictionary<string, object> dictionary)

Note that the dictionary instance is created by the caller. How do I communicate that the callee expects the IEqualityComparer<string> object held by the dictionary to perform, for example, a case-insensitive ordinal comparison?

+5  A: 

Use the documentation syntax:

/// <param name="dictionary">
/// ... string is case sensitive ordinal ...
/// </param>
GvS
Yes, that is an answer, but is that the best I can do?
Dave
It's the best I can think of. Waiting for more answers....
GvS
@Dave: Since that documentation is displayed by Intellisense, it should be fine.
OregonGhost
+1  A: 

You could always use a modified Hungarian convention (and I mean the Joel-approved kind):

  • Prefix cs for case-sensitive (non-linguistic)
  • Prefix ci for case-insensitive (non-linguistic)
  • Prefix cil for culture-invariant linguistic
  • Prefix csl for culture-specific linguistic or culture-sensitive linguistic

The "i" and "s" have consistent implications here, even though they can mean two different things depending on the context, which is a helpful attribute. "i" means "don't care" (about case/culture) and "s" means "do care".

Of course, as a disclaimer, I never do this, because for the vast majority of strings I deal with, the distinction between these types of strings is blurry at best. But if they have semantic meaning to you, this would be a reasonable alternative to relying on XML docs. Especially when you're using them as arguments to private methods, which most people don't write XML docs for.

Aaronaught
I read Joel's post; I like his thoughts on Apps Hungarian. I think I'll suggest this strategy unless someone else comes up with a better one. You're right on the mark about private methods, and I won't have to worry as much about lost .xml files.
Dave
I want to add that the real solution is a combination of Apps Hungarian and XML documentation, but since I mentioned XML documentation in my original question I'm going to give the checkmark to Aaronaught.
Dave