Consistent code bases using Rider code analysis

In our series about Rider‘s code analysis, we’ve seen what it is and how it works, and looked at various examples of inspections that can spot warnings and errors in our codebase. Rider helps us learn common practices and language features, too, using over 2400 inspections coming from ReSharper and from IntelliJ-based IDEs.

Code style is a broad topic, which includes everything from project structure to tabs vs. spaces (bring in the pitchforks!), to documenting public APIs, to internationalization and localization of our libraries and applications. All of these are important in making our codebase easier to consume – both for ourselves and for new developers joining the team. In this post, we’ll look at how Rider’s code analysis can help us maintain a consistent code style throughout our project.

In this series: (table of contents will be updated as we progress)

Consistent project structure

One of the first things I personally do when opening a codebase is exploring where everything is and how things fit together. A clean and consistent project structure following some conventions helps us become familiar with the project and improves maintainability later on.

A good convention is that namespace declarations should match with the file location. In other words, when a class is located in a folder Extensions, we may want to have the namespace reflect this. Rider’s code analysis will show a warning when this is the case – and by now it should be clear there’s a quick fix to resolve this, too (Alt+Enter). The quick fix will update the namespace for the current file and update any using statements in other files when needed. Keep in mind this inspection can run solution- or project-wide, too.

Namespace does not match folder structure

Similarly, we may want to ensure that the type name matches the filename and vice-versa. Nothing more frustrating than finally finding that Address.cs file only to discover it holds a completely different type! (of course, Rider’s navigation – Ctrl+T – should easily find the actual type itself). There’s a quick fix to ensure our file and type name matches:

Rename file to match type name

Another convention is that one file typically contains just one class. Rider comes with a quick fix to make that an easy task: simply Alt+Enter and move the type into a separate file:

Move type to file

Code style consistency

Rider helps to keep a consistent code style throughout our code base, applying style settings that are based on widely used and accepted conventions and best practices. Based on those conventions and our personal or team’s code style preferences, we can have Rider offer suggestions, warnings or even errors when we’re violating code styles.

A simple example: do you prefer using var or do you like to specify the full type name everywhere? We can configure Rider to have a strong opinion on this matter! From Rider’s settings (Ctrl+Alt+S), under Editor | Code Style we can specify the style rules developers, as well as our IDE, should follow when writing, generating or refactoring code in various languages. Let’s enforce var by making it an error:

Configure CSharp code style to always use var

In the editor, code analysis will now clearly show us when we’re violating code styles. A quick fix will help us correct:

Use var instead of explicity type

For each code style setting, we can see a description (what is it), a preference (which variation of the style do we want to use) and severity. We can also configure braces layout, blank lines, line breaks and wrapping, spaces (or tabs), indenting, … Code style settings are available for pretty much all languages and frameworks Rider supports, such as C#, VB.NET, JavaScript, TypeScript, CSS, SQL, XML, ASP.NET, Razor, XAML, and so on.

Code analysis and code style settings can be shared within the development team, enabling/disabling code inspections and setting their severity when the solution is loaded on their machine. Since Rider makes use of ReSharper settings layers to share settings, our configuration can also be shared between team members using different IDEs!

Back to business with one more example: naming. Do we use lowerCamelCase? UpperCamelCase? Prefix our private fields with an underscore or not? Should constants be uppercase? We can decide in the settings!

CSharp code style settings - decide on casing and property prefixing

Rider helps us adhere to the settings. Also note that using code cleanup, we can update our entire project or solution in one go, making sure the configured code style is applied.

Consistent variable, property and constant casing throughout our codebase

Internationalization

Depending on where we live in this world or who we are building software for, chances are we need to internationalize our libraries and applications and make sure they work as expected in all cases.

One project I’ve worked on was used all around the globe, running on various operating systems. Somewhere in the codebase, it converted user input to lowercase and then did a string comparison. That went great most of the time, until a customer in Turkey ran our application and used a “Turkish i” – our comparison failed.

When running this code in the USA, the comparison would work fine:

Now let’s set the current thread culture to tr-TR (for Turkey):

(tip: zoom in the browser font to see the differences in the character i)

That’s the same code, yielding a different result! We could fix this using string.Compare with ordinal string comparison, which checks the underlying Unicode values instead of culture-specific character classes, and returns the same result no matter where we run it.

Code analysis in Rider warns us about issues that are similar to the “Turkish i”. It knows which comparison methods are culture-specific and suggests specifying a comparison method that may be more suitable:

Inspection warns us for using culture-specific comparison that may lead to bugs

What else is there?

In this post, we’ve looked at some code analysis rules related to Code Style. With over 2400 automated code inspections borrowed from ReSharper and IntelliJ-based IDEs, Rider has many more to offer.

Make sure to explore the various other rules available in the settings under Editor | Inspection Settings | Inspection Severity! And if you haven’t done so yet, have a look at the entire blog series on code analysis in Rider.

Among other things, the ReSharper Cookbook also demonstrates how Rider – which is built on ReSharper – can help in different scenarios.

Download Rider and give it a try! We’d love to hear your feedback.

This entry was posted in How-To's and tagged , , , , , . Bookmark the permalink.

4 Responses to Consistent code bases using Rider code analysis

  1. Mikael Koskinen says:

    The code consistency tools in Rider are good. One thing which seems to be missing though is C# file layout configuration.

    With Resharper it’s possible to configure the order of things: Using-statements should go to top, constructors are after public properties, private methods are after public methods and so on.

    Even though Rider’s clean up tools mention file layout, there doesn’t seem to be a way to actually configure the settings.

    • Jura Gorohovsky says:

      This is true, the UI for file layout configuration is not yet implemented. Please upvote this feature request if you want this to happen. There’s also a recommended workaround under the request that involves configuring file layout settings in ReSharper and saving changes in a way that allows Rider to make use of them.

  2. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #2592

  3. Pingback: Dew Drop - May 25, 2018 (#2733) - Morning Dew

Leave a Reply

Your email address will not be published. Required fields are marked *