How-To's

Working with TODO items in Rider

Usually, coding and review tasks are logged in an issue tracker like YouTrack, JIRA or Visual Studio Team Services, all systems that can be connected to from within JetBrains Rider.

But what with those typical TODO comments? Do they go into the issue tracker? Or should we simply annotate our code with short comments?  Instead of starting a religious discussion, let’s look at how we can work with TODO’s in our code and IDE to describe these small, code-specific tasks.

Tip: do read up on how to work with tasks and contexts and integrate with issue tracking systems. Rider has some excellent functionality there as well!

Creating and navigating TODO comments

Many codebases already contain comments similar to this one:

public static string Piratize(this string boringEnglishString)
{
    // TODO - turn this into a non-horrible regex ;-)
    return lowerSubstitutions.Aggregate(boringEnglishString,
        (current, substitution) => current.Replace(substitution.Key, substitution.Value));
}

The TODO comment above describes a small task, typically related to the code at hand, which should be fixed at some point in time. In Rider we can open the TODO tool window (Alt+6 or View | Tool Windows | TODO), which scans our codebase for TODO comments and displays all of them:

Rider overview of TODO comments in code

Whenever we write a comment that starts with the word “todo”, Rider will display it in the TODO tool window. This works for all languages supported by Rider, so we can do this in C#, VB.NET, Razor, ASP.NET, JavaScript, HTML, CSS, …

From the toolbar on the left, we have several options available. For example, we can toggle Preview Source to display some additional context for a TODO item after selecting it:

Preview TODO comments in editor

We can navigate to a TODO comment by using the Jump to Source context menu (F4). When Autoscroll to Source is enabled from the toolbar, Rider will let us navigate the list using the keyboard and auto-opens the containing file in the editor at the exact location in source code where our comment is located.

Autoscroll to source from TODO tool window

TODO patterns

Over the years, I’ve seen code bases that use one (or more) of these types of comments to describe tasks that should be carried out at some point, for example as part of the “boy scout rule” (leave the campground cleaner than you found it):

  • TODO – a simple “todo” which should be executed on at some point (but very often stays in the codebase for years)
  • REVIEW – something the code reviewer should note and comment on
  • NOTE – explains why a certain piece of code was written and what the underlying idea is
  • BUG – describes a bug (which should probably be logged in the issue tracker as well…)
  • HACK – explains a hack in code

By default, Rider recognizes TODO and BUG as TODO comments. To add support for other comments, we can define a new pattern. We can do this from the settings, under Editor | TODO. Pattern are created as regular expressions, and can be as simple or as complicated as we want. For example if we want to recognize REVIEW, we could add a regular expression that searches for that word, e.g. \bREVIEW\b , or a full-blown regular expression that matches more complex structures.

Create TODO pattern

We have to specify our regular expression and can define whether it should be treated case sensitive or not. Additionally, we can pick an icon and a color that will be used to display the matching comment in the TODO tool window and editor. Here’s what things would look like after creating a pattern which emphasizes HACK comments:

Custom TODO pattern

Filtering TODO comments

Depending on the size and age of the codebase, there may be lots of TODO comments. We can filter TODO comments, either by scope or by creating a filter. Let’s look at scopes first.

From the Current File tab, we’ll see just the TODO comments in the current file active in the editor. The Scope Based tab, we can pick the places Rider should search for TODO comments. We can select the current project, or limit the search to just the files that we’re about to commit to a version control system. We could also create a custom scope, for example one which only scans through a specific folder inside our solution.

Filter TODO list by scope

In the TODO tool window, we can also apply a filter to the list of todo comments. We can add a new filter through Rider’s settings (under Editor | TODO) or Add filter toolbar button. Filters are based on a pattern, so we can easily create a new filter which would only show us only a specific type of TODO’s:

Filter TODO comments matching a given pattern

A cool trick here would be to create a filter which only matches TODO’s that apply to ourselves, for example here’s a filter that would only show TODO comments that have maarten in the comment (regular expression: \bmaarten\b). If we apply this filter on our TODO list, we’ll see only the tasks that I should be working on:

Filter TODO comments

TODO comments are not meant to replace an issue tracker, but can be helpful in identifying code that needs some work. Rider helps navigating these types of tasks and lets us slice and dice them based on scopes or patterns.

Download the latest Rider EAP build and give it a try! We’d love to hear your feedback!

image description