dgundersen
dgundersen

Reputation: 447

Resharper naming convention rule for void methods

Does anyone know how (or if it's possible) to create a Resharper naming convention rule that doesn't allow void methods to start with "Get"?

So this would be fine:

public string GetFoo();

But the rule would complain about this:

public void GetFoo();

Upvotes: 2

Views: 399

Answers (1)

Igal Tabachnik
Igal Tabachnik

Reputation: 31548

You can't really add this as a naming convention, but you could create a search pattern that will look for void methods starting with Get, and flag them with a Warning or Error.

For this, go to ReSharper's Options, then Code Inspection → Custom Patterns:

  • Click Add Pattern

  • In the new dialog, select Find

  • Type in the following pattern: void $method$($args$){ $stmt$ }, and press Add Placeholder → Extract from pattern. This should create 3 placeholders in the list.

  • Double click the method placehoder, and add the following RegEx: ^Get.*

  • In the Pattern Severity combobox select Show as Warning or Show as Error, depending on your preference.

  • In the description, write something like Void methods should not begin with Get.

  • Click Add, then Save (or Save To → Team Shared, to have this pattern stored in the team-shared settings, available to all your teammates).

ReSharper will now flag all void methods that start with Get:

Upvotes: 4

Related Questions