Reader small image

You're reading from  Refactoring with C#

Product typeBook
Published inNov 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781835089989
Edition1st Edition
Languages
Right arrow
Author (1)
Matt Eland
Matt Eland
author image
Matt Eland

Matt Eland is a Microsoft MVP in Artificial Intelligence (AI) who has been working with .NET since 2001. Matt has served as a senior engineer, software engineering manager, and .NET programming instructor. He is currently an AI specialist and senior consultant at Leading EDJE near Columbus, Ohio, where he helps companies with their software engineering and data science needs using C# and related technologies. Matt speaks and writes in his community and co-organizes the Central Ohio .NET Developers Group while pursuing a master's degree in data analytics.
Read more about Matt Eland

Right arrow

Adopting Code Standards

In this chapter, we’ll talk about the importance of establishing clear code standards with the right degree of flexibility. We’ll also cover some built-in tooling in Visual Studio that will help your team adopt a consistent set of coding standards. This in turn helps you focus on the right things during code review.

This chapter covers the following topics:

  • Understanding code standards
  • Establishing code standards
  • Formatting and code cleanup in Visual Studio
  • Applying code standards with EditorConfig

Technical requirements

The starting code for this chapter is available from GitHub at https://github.com/PacktPublishing/Refactoring-with-CSharp in the Chapter16/Ch16BeginningCode folder.

Understanding code standards

In this chapter, we’re going to explore the idea of code standards.

Code standards are an agreed-upon set of standards your team decides should be applied to any new code created by the team.

These standards have an important role in resolving disputes, focusing attention on the areas that truly matter, reducing the amount of technical debt teams naturally accumulate, and helping pay down existing technical debt.

The importance of code standards

One of the most frustrating experiences I’ve ever had as a developer is when I’ve sent a carefully thought-out change to another developer for review and I’ve heard back remarks such as the following:

  • I don’t like your curly brace formatting
  • Your indentation doesn’t match mine. I use spaces instead of tabs
  • I’d like it if you’d use var instead of the Type

In these scenarios, the developer in question ignores the substance of...

Establishing code standards

So, now that I’ve convinced you how code standards can reduce conflict in your team, focus code reviews, and guide refactoring efforts, let’s talk about where these standards come from and how we adopt them in our teams.

Collective code standards

Every software development team already has code standards.

I say this because each software development team already has, by definition, at least one developer. Every developer, whether they’re aware of it or not, has their own set of internalized code standards.

They may not have thought about their preferences or be able to list them out, but if you look at each developer on your team and the code they write in isolation, there will be a certain amount of consistency to it.

The problem teams encounter is not that they don’t have standards, but rather that they have too many standards. Each developer operates from their own internal set of standards and preferences and...

Formatting and code cleanup in Visual Studio

It turns out that Visual Studio can automatically arrange and even clean up your code in a consistent manner through built-in features.

Formatting documents

One of the easiest ways to do this is with the Format Document feature, either by pressing Ctrl + K and then Ctrl + D or by opening the Edit menu, then going to Advanced and selecting Format Document, as shown in Figure 16.1:

Figure 16.1 – Formatting the active editor document

Figure 16.1 – Formatting the active editor document

This will change the code in your current file to match the preferences you’ve configured in Visual Studio.

These settings can be configured by opening the Tools menu and then selecting Options…. From there, expand the Text Editor, C#, Code Style, and Formatting nodes until you see the various preferences for indentation, new lines, spacing, and wrapping.

These settings blades allow you to configure the formatting preferences of Visual Studio and preview...

Applying code standards with EditorConfig

Let’s take a look at how you can take the same code style settings found in the options dialog and attach them to a project through an .editorconfig file.

The EditorConfig feature uses .editorconfig files that contain style and language usage rules that apply to your project. Any violation of your EditorConfig rules will result in compiler warnings and suggestions in the Visual Studio editor.

EditorConfig files outside of Visual Studio

At the time of this writing, .editorconfig files work in Visual Studio and JetBrains Rider natively. In VS Code, EditorConfig files are supported as long as you install the C# Dev Kit and the EditorConfig for VS Code extension. See the Further reading section for instructions on enabling these features in VS Code and JetBrains Rider.

The key benefit of EditorConfig files is that they allow all developers working on a project to work with a consistent set of formatting and styling preferences...

Summary

Code standards are important for helping your team focus on productive things and ensuring source code can be easily maintained by all developers on your team.

While code standards don’t need to include everything, it can be helpful to codify stances on commonly contested items or best practices your team wants to make sure absolutely every change follows.

Visual Studio provides a number of features that help ensure a consistent and high-quality codebase including code formatting, code cleanup profiles, formatting on save, code analysis warning profiles, code styles at the editor level, and EditorConfigs to configure code styles inside of your editor.

In the final chapter of this book, we’ll discuss refactoring code as part of a larger organization and as part of an agile software development team.

Questions

  1. How do you determine what code standards your team should adopt?
  2. What are some ways you can handle disagreements on style rules?
  3. What are some options for configuring how Visual Studio formats code?
  4. What does the New EditorConfig (IntelliCode) option do?

Further reading

You can find more information about materials from this chapter at these URLs:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Refactoring with C#
Published in: Nov 2023Publisher: PacktISBN-13: 9781835089989
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Matt Eland

Matt Eland is a Microsoft MVP in Artificial Intelligence (AI) who has been working with .NET since 2001. Matt has served as a senior engineer, software engineering manager, and .NET programming instructor. He is currently an AI specialist and senior consultant at Leading EDJE near Columbus, Ohio, where he helps companies with their software engineering and data science needs using C# and related technologies. Matt speaks and writes in his community and co-organizes the Central Ohio .NET Developers Group while pursuing a master's degree in data analytics.
Read more about Matt Eland