Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make all our code comply to coding conventions/coding styles?

I'm trying to set up SonarQube to have our coding conventions/rules in it. I'm looking for ideas on how to do this best. We're using C# as our dev language and Visual Studio as our IDE. We have dozens of projects that should all follow the same coding conventions.

Our tools:

  • C#
  • Visual Studio
  • Azure DevOps
  • SonarQube

Our goals:

  • Have all our code comply to our custom coding conventions/styling rules.
  • We have dozens of projects. All projects should comply to these rules
  • Rules should be configured in a centralized place so that all devs will automatically be using the same rules at all times.
  • A bonus would be if we can automatically do all this in a centralized place. Maybe when pushing code to origin?

Any tips or ideas on how we can achieve our goals?

What I tried

As a test, I installed SonarLint and set it up in Connected Mode. This doesn't seem to check all SonarQube rules. I found, after some searching, that SonarLint only checks for Sonars own rules. Not for 3rd party rules.

I've also looked for what I can use to 'beautify' our code by making it match or coding style rules. The suggestions I found mentioning Roslyn analyzers and editorconfigs.

From my understanding, usage of Roslyn analyzers would mean adding the analyzer dlls to each and every project, which I want to avoid.

And editorconfigs are stored locally which means, if we decide to change some rules, everyone would had to manually update their editorconfig files OR we would have to add editorconfig files to each project. Also at first glance, it looks like editorconfig files aren't very flexible.

like image 280
MrSoundless Avatar asked Nov 18 '25 17:11

MrSoundless


1 Answers

We are using Directory.Build.props to customize all project files at once. For example to add analysers:

<Project>
  <PropertyGroup>
    <Version>93.0.0</Version>
    <AssemblyVersion>93.0.0</AssemblyVersion>
    <FileVersion>93.0.0</FileVersion>
    <Authors>xxxx</Authors>
    <Company>xxxx B.V.</Company>
    <Product>xxxx Demo</Product>
    <LangVersion>12.0</LangVersion>
  </PropertyGroup>

  <PropertyGroup>
    <!-- https://github.com/dotnet/reproducible-builds/blob/main/Documentation/Reproducible-MSBuild/Techniques/DisableImplicitNuGetFallbackFolder.md -->
    <DisableImplicitNuGetFallbackFolder>true</DisableImplicitNuGetFallbackFolder>
  </PropertyGroup>

  <Choose>
    <When Condition="$(MSBuildProjectName.Contains('Test'))">
      <PropertyGroup>
        <Nullable>enable</Nullable>
        <WarningsAsErrors>NU1605;CS8603</WarningsAsErrors>
        <NoWarn>S6562</NoWarn>
      </PropertyGroup>
    </When>
  </Choose>


  <ItemGroup>
    <PackageReference Include="Custom.CodeAnalyzer" Version="2.0.1" />
    <PackageReference Include="SonarAnalyzer.CSharp" Version="8.14.0.22654" />
  </ItemGroup>
</Project>

See Microsoft Learn: https://learn.microsoft.com/en-us/visualstudio/msbuild/customize-by-directory?view=vs-2022

like image 167
Willem Jan Gerritsen Avatar answered Nov 20 '25 06:11

Willem Jan Gerritsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!