Error: Twitter did not respond. Please wait a few minutes and refresh this page.
Stephen Ritchie's offerings of ruthlessly helpful .NET practices.
So you’re getting ready to start a .NET Best Practices initiative at your organization and you’re looking to find a lot of specific best practices tips. You want to know: What are the .NET Framework best practices?
You can be assured that I’ve been down this road. In fact, a few readers of my book, Pro .NET Best Practices, expressed some disappointment that the book is not a collection of specific .NET best practices. And this is exactly why I decided to address this subject in today’s post.
For those that want to dig right in, follow this link to part 1, MSDN: .NET Framework Best Practices.
If you want some background, let me start with the question: Who wants to follow best practices, anyway?
The adoption of new and different practices is a central theme of Pro .NET Best Practices. I work with enough individuals, teams, and organizations to understand the issues involved with adopting best practices. Consider the four levels at which best practices are embraced:
In an ideal world, best practices are quickly adopted at all four levels. However, in the real world, they can be slowly adopted by the group, resisted by the organization, embraced by one individual, not by another, or ignored altogether by everyone but you. It can be a mixed bag.
There are two key readers of this blog post that I want to identify with and help:
These readers are adopting at either the individual or group level.
If you are a reader who wants to bring best practices to the organization or the software development profession then I assert that you are probably not interested in the content of this compendium. Yes, you might refer a developer or team leader to the compendium, but I doubt you will find it directly relevant.
So, given this introduction, let’s look at how a collection (I like the term compendium) of specific .NET best practices might be organized.
Since this is a blog, tags can help others find and navigate the content. Here is a quick list of tags that come to mind:
Clearly, there are a lot of ways to slice and dice the topic of best practices; however, I will try to bring things back to the topic of the Microsoft .NET Framework.
You can find the entire Best Practices category here: https://ruthlesslyhelpful.net/category/development/best-practices/
I mostly wrote Pro .NET Best Practices based on my professional experience trying to get teams and organizations to adopt .NET Framework best practices. Over the years, I have read many books, I experimented, I tried and persevered with one approach, and I tried totally new approaches. Many times I learned from others. Many times I learned by my mistakes.
Over the years and as I researched my book, I found many free, on-line sources of .NET best practices. Many are professionally written and easy to follow. In my book I was reluctant to paraphrase or repeat material, but I should have done a better job of showing people how to access the material. (The one thing I really kick myself over is that I did not use Bitly.)
So, let me start the Compendium of .NET Best Practices with some great material already available on the Internet.
For years now, Microsoft Developer Network (MSDN) has provided free online documentation to .NET developers. There is a lot of individual .NET best practices topics, which are described at the high level at this MSDN link:
MSDN: .NET Framework Best Practices
This is a great MSDN article to read and link to bookmark if you’re interested in.NET best practices.
Just take a look at all the information within the MSDN topic of Best Practices for Using Strings in the .NET Framework. I am not going to be able to duplicate all of that. If you are developing an application that has to deal with culture, globalization, and localization issues then you need to know much of this material.
Before I go any further, let me introduce you to Jon Skeet. He wrote an awesome book, C# In Depth. I think you might enjoy reading his online article on .NET Strings: http://csharpindepth.com/Articles/General/strings.aspx
Okay, let’s get back to the MSDN article. Below I have highlighted a few of the Strings best practices that I’d like to discuss.
1. Use the String.ToUpperInvariant method instead of the String.ToLowerInvariant method when you normalize strings for comparison.
In the .NET Framework, ToUpperInvariant is the standard way to normalize case. In fact, the Visual Studio Code Analysis has rule CA1308 in the Globalization category that can monitor this.
This is a really easy practice to follow once you know it.
Here is the key point I picked up from rule CA1308:
It is safe to suppress [this] warning message [CA1308] when you are not making security decision based on the result (for example, when you are displaying it in the UI).
In other words, take care to uppercase strings when the code is making a security decision based on normalized string comparison.
2. Use an overload of the String.Equals method to test whether two strings are equal.
Some of these overloads require a parameter that specifies the culture, case, and sort rules that are to be used in the comparison method. This just makes the string comparison you are using explicit.
3. Do not use an overload of the String.Compare or CompareTo method and test for a return value of zero to determine whether two strings are equal.
In the MSDN documentation for comparing Strings the guidance is quite clear:
The Compare method is primarily intended for use when ordering or sorting strings.
If you have not had a chance to take a look at the All-In-One Code Framework then please take a few minutes to look it over.
The Microsoft All-In-One Code Framework is a free, centralized code sample library driven by developers’ needs.
It is Microsoft Public License (Ms-PL), which is the least restrictive of the Microsoft open source licenses.
What’s relevant to this article is the All-In-One Code Framework Coding Standards document. You can find the download link at the top of this page: http://1code.codeplex.com/documentation
In that document, they list a very relevant and useful list of String best practices.
This post is part of my Compendium .NET Best-Practices series.
In November I traveled to Upstate New York to present at four .NET Users Group. Here’s the overview:
I realize it is belated, but I’d like to extend a very big and heartfelt thank you to the organizers of these users groups for putting together a great series of meetings.
Thank you to Stephanie Carino from Apress for connecting me with the organizers. I really appreciate all the help with all the public relations, the swag, the promotion codes, the raffle copies of my book, and for the tweets and re-tweets.
My presentations are available on SlideShare under my RuthlessHelp account, but if you are looking for something specific then here are the four presentations:
All the code samples can be found on GitHub under my RuthlessHelp account: https://github.com/ruthlesshelp/Presentations
If you attended one of these presentations, please rate me at SpeakerRate:
You can also rate me at INETA: http://ineta.org/Speakers/SearchCommunitySpeakers.aspx?SpeakerId=b7b92f6b-ac28-413f-9baf-9764ff95be79