top of page
Search

DX - Code Formatting

  • Writer: Andrew Boyd
    Andrew Boyd
  • Aug 13
  • 2 min read

I’m a big fan of the Developer Experience (DX). Too often, developers are told to “just do it”, and we’re not really given time to make the refactors we *know* would pay off.


If you’ve written any JavaScript, you’ve probably had the privilege of experiencing Prettier. This post is about getting as close as possible to that same experience in C#—reducing noise in your `.cs` files and making formatting consistent across a team.


1) Excessive namespaces

Remove unused usings

First up: unused namespaces.


Have you ever noticed a `using` is greyed out in your IDE? That normally means it isn’t used, so it can be removed. It’s a small change, but it adds up fast—especially in older codebases.


Enable implicit usings (.NET 6+)

There’s also a feature you can turn on in your `.csproj` (or `Directory.Build.props`) called:

<ImplicitUsings>enable</ImplicitUsings>

This was introduced in .NET 6. Scott Hanselman covers what this gives you (and which namespaces you no longer need to explicitly include) here:


The gotcha

This can break your build.


Once implicit usings are enabled, every C# file effectively gets common namespaces (like `System`) “for free”. That’s great—until you have types with the same name in different namespaces.


When that happens, you’ll need to disambiguate by:

- fully qualifying the type name, or

- using an alias.


2) Coding standards with `.editorconfig`

Most teams have coding standards, but did you know you can enforce them with a `.editorconfig` file?


There are a lot of options available, so going through them all is beyond this post, but Microsoft’s docs are a good starting point:


I also have a sample `.editorconfig` in my open-source project here:


A couple of my favourites


File-scoped namespaces

Prefer:

namespace YourApp;

over:


namespace YourApp
{
    // ...
}

Multi-line blocks for readability

Prefer:

if (someComparison)
{
    return someResult;
}

over:

if (someComparison) return someResult;

3) Bringing it all together with `dotnet format`

As of .NET 6, Microsoft introduced `dotnet format`:


Once you’ve got an `.editorconfig` in place, this becomes the “make it all consistent” button.


At a high level, the workflow looks like:


  1. Enable Implicit Usings (if you haven't already)

  2. Commit the changes

  3. Add/adjust `.editorconfig`

  4. Commit the changes (yes, I like to commit regularly, but you'll thank me if the next step blows out)

  5. Run: dotnet format

  6. Commit the changes (ideally as a dedicated formatting PR. A word of warning: it will be huge because it will tidy up every cs file in your repo)

Final thoughts

None of this is glamorous work, but it’s the kind of thing that improves DX immediately:

- less noise in diffs

- fewer style debates in PRs

- more consistent code across the team


If you’ve ever wished C# had a “Prettier moment”, this is the closest I’ve found.


Visual Studio extension

There is also a feature in Visual Studio that allows you to format your files on save by Mads Kristensen called CodeCleanupOnSave ( https://marketplace.visualstudio.com/items?itemName=MadsKristensen.CodeCleanupOnSave).


 
 
 

Recent Posts

See All
Don't use ILogger

In a project recently, I noticed while ILogger was being passed into API controllers, it wasn't being used as much as it should, so I...

 
 
 
Don't use Configuration!

One of the most used classes in .NET Core would have to be IConfiguration, found in the Microsoft.Extensions.Configuration Nuget package....

 
 
 

Comments


Post: Blog2_Post
  • LinkedIn
  • Twitter

©2022 by Andrew Boyd

bottom of page