Wiki

Clone wiki

Greenkeeper / SortingDeclarations

Sort your code

Description

Code is easier to read if it is arranged according to a certain pattern. For example fields before constructor and constructor before methods etc. Greenkeeper provides a ContextAction sorting code to your pattern and a Highlighting with a QuickFix if your code is not sorted correctly.

Greenkeeper includes the possibility to sort your code by newspapermode. There are quite more possibilities to sort your code. There is already a default, but you can define your own pattern in configuration. You can combine the sortertypes or include conditions.

Sortertypes

  • Usage (newspapermode)

    Sorting methods in newspapermode is a technique from the book "Clean Code" by Robert C. Martin alias "Uncle Bob". A class should be readable like a newspaper, with the most important methods being at the beginning of the class, followed by methods which are invoked later in the execution flow.

  • Modifiers (static, readonly etc.)

  • AccessRights (private, public etc.)
  • Alphanumeric
  • Attribute ([Test] etc.)
  • BaseElement (interface, baseclass)

    Allows you to sort overriden properties and methods by definitionorder in their class or interface.

  • ParameterCount

  • Region
  • Type (ctor, method, property etc.)

##Example sort by newspaper## Before sort:

#!csharp
void MethodD()
{ 
}

void MethodB()
{ 
}

void MethodA()
{
  MethodB()
  MethodC()
}

void MethodC()
{ 
   MethodD()
}

After sort by newspaper:

#!csharp
void MethodA()
{
  MethodB()
  MethodC()
}

void MethodB()
{ 
}

void MethodC()
{ 
   MethodD()
}

void MethodD()
{ 
}

Configure

The sort definition can be configured via xml in the ReSharper-Options. Details

Updated