MVPs Should Not Publish Bad Code Samples
314 words.
Some programming blogs are informative and trustworthy. Unfortunately, most aren’t. For example, in an otherwise good post explaining new features in C# 3.0, I found this shocking sample C# 3.0 extension method among the blogs at the popular site ASP.NET Weblogs:
public static string RemoveNonNumeric( this string s )
{
MatchCollection col = Regex.Matches( s, "[0-9]" );
StringBuilder sb = new StringBuilder();
foreach( Match m in col )
sb.Append( m.Value );
return sb.ToString();
}
The trouble is: That sample method should not be emulated under any circumstances. I can’t think of any reason to use a Regex match for something as simple as stripping characters from a string. Regex matching is relatively expensive, not to mention very cryptic for most junior-level programmers to understand.
Out of curiousity, I looked up this guy’s bio. “Dan Wahlin (Microsoft Most Valuable Professional for ASP.NET and XML Web Services) is a .NET development instructor at Interface Technical Training.” Not only is he a Microsoft MVP but he’s an instructor! And he’s written books! Now I don’t know if he wrote that sample himself or not, but even if he copied and pasted it, his credentials indicate that he should have the wisdom and experience to remove that Regex.
I realize his point was to demonstrate extension methods and not the best way to strip characters from a string, but still, I don’t think people in positions of authority should ever publish bad code samples. He should revise his blog post before too many impressionable young coders see it — or revise his credentials.
P.S. I was going to leave a comment about it, but someone already did. Mr. Wahlin did add an addendum to his post, albeit somewhat indignantly. At any rate, the fact that he went with a Regex as his first choice for stripping characters indicates a certain lack of experience, in my mind. Buyer beware.
Sorry, new comments are disabled on older posts. This helps reduce spam. Active commenting almost always occurs within a day or two of new posts.