I recently decided to clean up an open source project, and the best tool I found for the job was Telerik JustCode’s Code Clean option. If you’ve never tried it, open the JustCode Options, go to Code Cleaning, then use the drop down to select Full (built-in) and press the “Set as Default” button.
If you wish, you can use the “New” or “Clone” button to set up your own profile that matches your coding style. Afterwards, return to a piece of code file and press Ctrl+Shift+Alt+F and watch your (co-worker’s) code be processed into a thing of beauty.
Opening a thousand C# files to press a button combo isn’t likely to be high on anyone’s list. Luckily, you don’t need to do this. Highlight what you want to clean in the Solution Explorer, right-click to bring up the context menu, and select Just Clean All.
I can’t guarantee this level of success every time, but a change I made in a project broke a ton of code due to invalid references, generating over 2000 errors. Instead of rolling back, I used Just Clean All on the solution; it fixed every last one.
However, there were some things I wish I could clean up automatically. The JustCode team prioritizes based on the impact each item will have to the community at large, and I am somewhat pickier than others. So, I asked Deyan Varchev for an example of creating a Code Cleaning extension, and he delivered one to me. The information you’ll find here isn’t even in the JustCode API templates! This means it may be unsupported or subject to change.
Some people swear by regions, other people consider it a code smell. If you are a fan of regions, you can still follow this example, but you may want to uncheck this cleaning step in your code cleaning profile.
As with any JustCode extension, it is best to create it using the JustCode Extension Template. This will include the appropriate references and debug options. Now, add a new class. I called mine RemoveRegionCleaning. This class will inherit from CodeCleaningStepProviderModuleBase found in Telerik.JustCode.CommonLanguageModel. Use the Visual Aid to Create Stubs for Required Members. This will create overrides for the property CodeCleaningSteps and method ExecuteCodeCleaningStep.
Before this code is implemented, add the following two attributes to the class so it is properly exported by MEF.
The CodeCleaningSteps property returns an IEnumerable<CodeCleanStep> which adds the cleaning for the languages specified. Since regions are present in both C# and VB, there are three constants to define for the class.
Order = 100;
The MarkerId is used to identify this cleaning, and the CleanOptionText defines the description provided in the Code Cleaning options. Finally, Order determines the order in which these steps run in regards to other cleaning steps. This is useful when one step will affect another.
Implementing the CodeCleaningSteps property is straightforward: create a getter that yield returns each step. Here it is:
The ExecuteCodeCleaningStep method performs the actions necessary to clean the code. Basically, it is the meat of this class.
public override void ExecuteCodeCleaningStep(CodeCleaningStep step, FileModel fileModel, CodeSpan span)
Regions are preprocessor directives, and we can use the fileModel parameter to retrieve every line that is one of these by calling fileModel.All<IPreProcessorDirectiveLine>(). However, we need to filter these out specifically for regions. We can analyze the text of each line to do so, and I may want to use this in the future. Therefore, I created an extension method for IPreProcessorDirectiveLine.
I will also need a way to delete these lines. Each line has a TextualCodeSpan property, which can then be replaced with String.Empty. The default formatting rules will remove the empty line, so this isn’t something this cleaning step needs to do. I want IPreProcessorDirectiveLines to have the ability to delete themselves, so I added another extension method.
I’m going to run Delete on every processor directive. To keep the code concise, I implemented the ForEach extension method.
IEnumerable<T> sequence, Action<T> action)
With all these steps in place, implementing the ExecuteCodeCleaningStep method is straightforward.
ExecuteCodeCleaningStep(CodeCleaningStep step, FileModel fileModel,
.Where(line => line.IsRegion())
.ForEach(region => region.Delete());
public class RemoveRegionsCleaning : CodeCleaningStepProviderModuleBase
private const string MarkerId = "RemoveRegions";
private const string CleanOptionText = "Remove Regions";
private const int Order = 100;
public override IEnumerable<
yield return new CodeCleaningStep(LanguageNames.CSharp, Order, CleanOptionText, MarkerId);
yield return new CodeCleaningStep(LanguageNames.VisualBasic, Order, CleanOptionText, MarkerId);
.Where(line => line.IsRegion())
public static class EnumerableExtensions
public static void ForEach<
> sequence, Action<
foreach (T item in sequence)
public static class PreProcessorDirectiveLineExtensions
public static bool IsRegion(this IPreProcessorDirectiveLine line)
return line.ExistsTextuallyInFile &&
(line.Text.ToLower().Trim().StartsWith("#region ") ||
public static void Delete(this IPreProcessorDirectiveLine line)
That’s really all there is to it. After implementing this code, you can see it in action by debugging your JustCode extension library. Visual Studio will appear with the extensions installed, and you can try it out on any project. Just make sure you have selected the cleaning step for your code cleaning profile.
Are there other extensions you would like to see implemented? Let us know what’s on your mind in the forums!
Chris Eargle is a Microsoft C# MVP with over a decade of experience designing and developing enterprise applications, and he runs the local .NET User Group: the Columbia Enterprise Developers Guild. He is a frequent guest of conferences and community events promoting best practices and new technologies. Chris is a native Carolinian; his family settled the Dutch Form region of South Carolina in 1752. He currently resides in Columbia with his wife, Binyue, his dog, Laika, and his three cats: Meeko, Tigger, and Sookie. Amazingly, they all get along... except for Meeko, who is by no means meek.
Copyright © 2002-2016 Telerik. All rights reserved.
Powered by Telerik