C code review with visual studio

Developers should follow Microsoft's C# Coding Conventions and, where applicable, Microsoft's Secure Coding Guidelines.

Code Analysis / Linting

We strongly believe that consistent style increases readability and maintainability of a code base. Hence, we are recommending analyzers / linters to enforce consistency and style rules.

Project Setup

We recommend using a common setup for your solution that you can refer to in all the projects that are part of the solution. Create a common.props file that contains the defaults for all of your projects:

...


    
      all
      runtime; build; native; contentfiles; analyzers; buildtransitive
    
    
      all
      runtime; build; native; contentfiles; analyzers; buildtransitive
    


    true


    

...

You can then reference the common.props in your other project files to ensure a consistent setup.

The .editorconfig allows for configuration and overrides of rules. You can have an .editorconfig file at project level to customize rules for different projects [test projects for example].

Details about the configuration of different rules.

.NET analyzers

Microsoft's .NET analyzers has code quality rules and .NET API usage rules implemented as analyzers using the .NET Compiler Platform [Roslyn]. This is the replacement for Microsoft's legacy FxCop analyzers.

Enable or install first-party .NET analyzers.

If you are currently using the legacy FxCop analyzers, migrate from FxCop analyzers to .NET analyzers.

StyleCop analyzer

The StyleCop analyzer is a nuget package [StyleCop.Analyzers] that can be installed in any of your projects. It's mainly around code style rules and makes sure the team is following the same rules without having subjective discussions about braces and spaces. Detailed information can be found here: StyleCop Analyzers for the .NET Compiler Platform.

The minimum rules set teams should adopt is the Managed Recommended Rules rule set.

Automatic Code Formatting

Use .editorconfig to configure code formatting rules in your project.

Build validation

It's important that you enforce your code style and rules in the CI to avoid any team member merging code that does not comply with your standards into your git repo.

If you are using FxCop analyzers and StyleCop analyzer, it's very simple to enable those in the CI. You have to make sure you are setting up the project using nuget and .editorconfig []. Once you have this setup, you will have to configure the pipeline to build your code. That's pretty much it. The FxCop analyzers will run and report the result in your build pipeline. If there are rules that are violated, your build will be red.

- task: DotNetCoreCLI@2

  displayName: 'Style Check & Build'
  inputs:
    command: 'build'
    projects: '**/*.csproj'

Enable Roslyn Support in Visual Studio Code

The above steps also work in VS Code provided you enable Roslyn support for Omnisharp. The setting is omnisharp.enableRoslynAnalyzers and must be set to true. After enabling this setting you must "Restart Omnisharp" [this can be done from the Command Palette in VS Code or by restarting VS Code].

Code Review Checklist

In addition to the Code Review Checklist you should also look for these C# specific code review items

How to do code review in Visual Studio Code?

Simply right click somewhere in the opened file and choose the option "Code Review: Add Note". You will be prompted for your note you want to add. A file code-review. csv will be created containing your comments and the file and line references.

What is the code review plugin in Visual Studio?

Our code review plugin helps you to create review requests and respond to them without leaving Visual Studio. Review Assistant supports Azure DevOps, TFS, Subversion, Git, Mercurial, and Perforce. Simple setup: up and running in 5 minutes. Review Assistant is free of charge for 1 project with up to 3 participants.

How to use Visual Studio Code for C?

Simply open VS Code/VS Code Insiders, open any folder, and create any file with the extension .c for the C file and .cpp for the C++ file. After writing your code, you can run the code directly using the play button you'll find in the upper right corner.

How to review C# code?

C# Code Review Checklist.

Set code review goals..

Identify the code to be reviewed..

Familiarize self with the code context..

Run static code analysis tools..

Inspect manual code..

Check code style conformity..

Ensure adherence to coding standards..

Check for correct variable declarations..

Chủ Đề