4

Is there a general coding convention in ordering namespaces? Is it always order of importance, or alphabetical? Currently I order by main important ones first. I know it does not affect program, just curious about coding convention.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
4

How would you determine the importance? The simplest way of ordering the usings is the to apply an alphabetical order; However, it is often useful the make the difference between usings pointing to the .NET Library and your own namespaces:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using MyCompany.MySolution.MyModule1;
using MyCompany.MySolution.MyModule2;
using MyCompany.MySolution.MyModule3;

Within these two groups I would still apply an alphabetical order. I keep my own namespaces below the system namespaces, because like this, they are closer to the rest of my code and more likely to be visible.

Keep it simple!

2
5

When dealing with style, it is always more useful to take either an official style if one exists, or to use the one commonly adopted by the community of developers writing in a specific language.

You're lucky, for C#, there is an official style created by Microsoft. Another good thing is that Microsoft also developed a tool called StyleCop which checks if your code is valid.

Two of the rules are relevant to your question:

  • SA1210: Using directives must be ordered alphabetically by namespace.
  • SA1208: System using directives must be placed before other using directives.

In other words, namespaces starting by System. always come first, ordered alphabetically, and all other namespaces, also ordered alphabetically, come second. There is no empty line between the two. The code you quoted in your question is a perfect illustration of this style.

Compared to your approach:

The one I suggest has a few benefits:

  1. It can be applied automatically instead of a manual choice of which of the usings is more important. Actually, this is the one which is enforced automatically in Visual Studio when you ask to reorder the usings. Having one thing less to think about during development allows you to focus on things that really matter, such as the quality of your code.

  2. It can be checked (for instance during a commit).

  3. There would be no pointless discussions among your colleagues about the importance of the respective namespaces.

There is, however, a major drawback:

  1. If you rename a namespace, it would often happen that it would create havoc in your well-ordered lists of usings in other files. While Visual Studio makes it very easy to reorder namespaces in a single file, it doesn't help at all when it comes to applying the order in all the files of a project (and if I'm not mistaken, Visual Studio won't bother fixing the order of the namespaces when renaming one).

Compared to Olivier's approach:

The one I suggest has the following benefits:

  1. Here again, Visual Studio support comes up handy. It can be difficult if I have to waste my time reordering the namespaces by hand (or slow my IDE by installing CodeRush or Resharper).

  2. A newcomer won't have to learn a different style from the one commonly used by the industry. This, alone, is enough to avoid this style.

  3. StyleCop checks this style automatically. If Olivier's style has to be used, one should develop a custom rule for StyleCop. Nothing impossible, but still additional work to do.

The drawback is:

  1. The relative mix between in-house namespaces and third-party namespaces. This could be an issue if in-house namespaces are named inconsistently; if they are all prefixed by the name of your company, they are necessarily together when ordered alphabetically anyway.
3
  • Thanks, doesn't Stylecop slowdown IDE like CodeRush? – CarSpeed87 Aug 19 '18 at 22:34
  • StyleCop doesn't continuously run in background, not does it change code while you're writing it. It runs either when you explicitly call it, or during a build if you configured it this way. Therefore, there is no performance impact on Visual Studio. Unlike Code Analysis, StyleCop is also very fast. I have it in a pre-commit hook and I don't notice it (unlike, once again, things such as Code Analysis which tend to take a few seconds on growing up projects). – Arseni Mourzenko Aug 20 '18 at 12:13
  • In order to have VS put the System namespaces first when you sort and remove (the way StyleCop wants it) you need to change the dedault setting: dailydotnettips.com/… – Martin Maat Aug 21 '18 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.