How to automatically keep your code clean

How to automatically keep your code clean

11 January 2021

General

Share:

This is my first article, which I dedicate to setting tools that can help to keep the code clean and prevent you from publishing unexpected files to the repository.

First, let me ask you 2 questions, which will help you to better understand the reason for writing the article.

  1. How many times did you publish a confidential file to the repository, so you needed to revert your changes?
  2. How many people are working with you on the same code?

As for the first question, we all know that people make mistakes, but we can prevent ourselves from them.

As for the second question, if you are working in a team, you probably see that your team member write code in a bit different style, which you usually do (extra spaces, additional scopes, or brackets). As for me, I prefer a consistent code style with predefined rules, which each team member follows. But even if you defined some config files, it doesn't matter that all will follow them. So we need additional steps which will check all our configuration before publishing the code.

So, let's start.

//Note: some steps are applied for a git repository only.//

Step 1.

First, let's create a new empty library project GitConfiguration.

Now open csproj file and replace its content with:

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<TargetFramework>netstandard2.0</TargetFramework>
		<HooksPath>hooks</HooksPath>
	</PropertyGroup>

	<ItemGroup>
		<PackageReference Include="DotnetHooks" Version="1.0.0">
			<PrivateAssets>all</PrivateAssets>
			<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
		</PackageReference>
	</ItemGroup>

	<Target Name="PreBuild" BeforeTargets="PreBuildEvent">
	  <Exec Command="dotnet restore" />
	</Target>
</Project>

Create a new folder hooks in your repository root folder. Build your solution. Now git is configured to our hooks folder.

Here we use Nuget package DotnetHooks which set up git to use actions from the specific folder.

Step 2.

Git has hooks - actions that are executed depends on your action. We will use the pre-commit hook.

So, open the hooks folder and create a new file pre-commit with the next content:

#!/bin/sh
echo "error"
exit 1

Now try to commit your changes. You should receive the error

Git Commit Error

Now replace the content with exit 0 and try to commit again. All your changes are committed.

Step 3.

Now let's add our code inspectors to the pre-commit file.

I will describe my 3 favorites which I used many times.

  1. dotnet format. This utility goes through all your files in solutions and formats the code according to your style rules.

Add the next code to your pre-commit file:

$solution="YourSln.sln"
dotnet tool install -g dotnet-format
dotnet format $solution --check
status=$?
[ $status -eq 0 ] && echo "No errors found" || dotnet format $solution
exit $status

So first we install the tool. Then we check if our solution has any errors. If any we run formatting and check the exit code of the operation.

  1. Jetbrains.Resharper.CommandTools - powerful tools that can help you not only inspect the code but also clean up it and find duplicates.

Add the next code to the beginning of your pre-commit file:

dotnet tool install -g JetBrains.ReSharper.GlobalTools
jb inspectcode $solution --properties:Configuration=Release --output="result.xml"
jb cleanupcode $solution

Summary

So using git hooks you can control your actions with a repository.

Code inspectors should help you automatically analyze and format your code to follow your preferences.

You can find more examples on my GitHub: XamarinAndroidFloatingWindow

Related:

Secrets of successful interviews. How to get to any project in any IT company

Secrets of successful interviews. How to get to any project in any IT company

Some base recommendations which might help you to successfully pass the interview

An error has occurred. This application may no longer respond until reloaded. Reload 🗙