Enforce Coding Standards with GitHub Actions and .NET: A MUST for Your Project's Success
Enforce Coding Standards with GitHub Actions and .NET
In this short post, we'll explore how to enforce coding standards in your project using .NET and GitHub Actions.
Coding standards are crucial for every project as they ensure consistency, readability, and maintainability of the codebase, making it easier for teams to collaborate effectively. They also play a vital role in reducing errors and improving code quality, ultimately leading to more reliable and efficient software development processes.
The exciting part is that you can enforce these standards through GitHub Actions within your CI/CD pipeline.
Create a New Console Application
If you're using .NET 8, your app should contain only a single Program.cs file, which will include the following C# code:
Console.WriteLine("Hello, World!");
Create a Repository
Start by creating a local Git repository, which we will soon push to GitHub.
Create an .editorconfig
File
Navigate to Options > Text Editor > C# > General, and set the Avoid unused parameters severity to Error.
Then, click on Generate .editorconfig file from settings and save it into your project. This action helps ensure that your project adheres to specific coding styles and practices, enhancing code quality and consistency.
These steps generate an .editorconfig file that encapsulates all the formatting rules Visual Studio will enforce in our project. This ensures uniformity across the board for every team member.
We've also customized our first rule to return an error if there's a parameter that hasn't been used inside a method.
Now, proceed with pushing the project to GitHub.
You now have a main/master branch that encapsulates your predefined coding standards and rules. The next step is to integrate these standards into our pipeline.
Add Code Formatting into GitHub Actions
To do this, navigate to the Actions tab in GitHub.
Under the Continuous Integration section, locate the .NET option and click on Configure
In the YAML editor, add the specified configuration under dotnet restore.
- name: Format
run: dotnet format --verify-no-changes --verbosity diagnostic
Here’s the full YAML file.
# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net
name: .NET
on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Format
run: dotnet format --verify-no-changes --verbosity diagnostic
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
Commit the changes
This setup enforces the rules we've established every time a developer attempts to merge their code into the main/master branch. We'll see this in action shortly.
Imagine a developer is assigned to work on a new feature.
Developer comes in to work on a feature…
Create a new feature branch from master/main
Replace Program.cs with the following code:
class Person
{
public void SayHello(string name)
{
Console.WriteLine("Hello!");
}
}
In this hypothetical scenario, the developer intends to enhance the application by introducing a new feature.
The plan is to create a Person
class with a SayHello method designed to greet the individual. However, the developer skips using the name parameter inside the method’s logic which violates the coding standards and rules we've established for our project.
Since the feature branch is based on the main branch, it inherits all the rules, including the .editorconfig file. Consequently, the code snippet above would be highlighted in red in Visual Studio, indicating the unused parameter name
.
Here Visual Studio serves as our first line of defense against non-compliant code. However, suppose the developer overlooks this issue and attempts to merge the changes into the main branch.
Create a pull request to Merge into the main branch
Now let’s check the status of this pull request on GitHub by navigating to the Pull Requests section on GitHub.
The pull request fails, and clicking on Details on the right reveals the cause: Error: error IDE0060: Remove unused parameter 'name'.
This example illustrates the simplicity and efficiency of enforcing coding standards in your project using .NET and GitHub Actions.
By integrating these practices into your workflow, you ensure that all code adheres to predefined standards before it merges, maintaining code quality and consistency across the project.