Software Development

Spring cleaning for your code, thanks to CodeMaid

CodeMaid is a Visual Studio add-on that cleans up your code, allows you to rearrange it for better navigation, and, ultimately, makes you more productive.

cleaning1698x1131032814.jpg
 Image: iStock/gvictoria

The last thing that comes to mind when you're in the middle of churning out code is proper formatting and organization -- like documentation, this is something that usually comes up at the end when everything (hopefully) is working as designed. The CodeMaid Visual Studio add-on lends a hand with this cleanup and provides useful features to use when coding.

Getting started

CodeMaid is available from the Visual Studio extensions page; there is a link to the download page on the CodeMaid site as well. Click the download link and run the downloaded file once it's available. It supports Visual Studio versions 2010, 2012, and 2013. Once installed, the CodeMaid menu shown in Figure A is available in Visual Studio.

Figure A

codemaidfiga032814.jpg

CodeMaid menu available in Visual Studio IDE.

Where does the maid start?

CodeMaid's slogan says that it promises to do the dirty work via two basic features: cleanup and navigation. Cleanup is achieved by removing unnecessary whitespace, organizing code, and formatting in a consistent manner. Navigation is provided by searching, as well as organization options that may help you better work with the code. It works on C#, C++, F#, VB, XAML, XML, ASP.NET, HTML, CSS, LESS, JavaScript, and TypeScript.

When CodeMaid cleans a file or a solution, its goal is to remove all blank lines and whitespace (end of line, consecutive blank lines, and so forth) and unused using statements. Also, it will organize using statements, add unspecified access modifiers, update endregion tags, and run Visual Studio formatting.

The Visual Studio built-in formatting is accessed via Tools | Options, select Text Editor, and then select the development language and formatting. Figure B shows C# options for my environment.

Figure B

codemaidfigb032814.jpg

Visual Studio built-in code formatting options.

A code sample is used to show the basic CodeMaid cleaning feature. Figure C shows a basic code sample before it is cleaned, and Figure D shows the results of cleaning: unused using statements and whitespace are removed.

Figure C

codemaidfigc032814.jpg

C# code sample before it's cleaned by CodeMaid.

Figure D

codemaidfigd032814.jpg

C# code sample after it has been cleaned by CodeMaid.

The cleaning features are good, but the real power of CodeMaid is provided by the navigation or digging features as the product site calls them. These features allow you to rearrange code for better navigation.

One of the interesting features is called the spade within the Visual Studio interface. Basically, it digs into the currently open file and presents all of its methods and properties in an easy to navigate list that can be sorted a few ways. Figure E shows it opened for code from the VirusTotal.NET project. It shows the popup window displayed when you mouse over a method or a property.

Figure E

codemaidfige032814.jpg

The CodeMaid Spade provides easy to use interface for code.

The following list provides details on some of the other features available.

  • Formatting: You can consistently format comments.
  • Joining: You can right-click and join two adjacent lines or highlighted code onto one line.
  • Read-only state: I love this feature, as it allows you to toggle a file between read-only, so no more switching to explorer to change file attributes.
  • Switching: This allows you to switch between related files. A good example is switching between web page and code-behind.
  • Collapsing: You can collapse sections or nodes of code as well as the entire solution.

The search features of CodeMaid are some of my favorite features, because they allow you to quickly find references or where code is used via a few clicks. The context menu shown in Figure A shows this feature in the menu list -- it is called Find In Solution Explorer. You click this when in source code, and it shows you where the item is defined within the Solution Explorer. A large code base can be confusing, so it lets you quickly find things.

You decide how CodeMaid operates

While CodeMaid provides great features out of the box, its configuration options allow you to choose how these features often behave and appear. Configuration options are accessed by selecting Configuration at the bottom of the CodeMaid menu in Figure A. Figure F shows the configuration window opened; it allows you to specify general settings like the color scheme used.

Figure G shows the remove feature options for the cleaning feature; it allows you to specify when blank files and spaces are removed. The left portion of Figure G shows the other CodeMaid features that can be configured. You can play with the configuration items to see what works best for your projects.

Figure F

codemaidfigf032814.jpg

CodeMaid allows you to configure its many features.

Figure G

codemaidfigg032814.jpg

Configure what is removed when CodeMaid cleans code.

A more productive coder

I am skeptical when something promises to help me work better or smarter, but CodeMaid provides nice simple features within the sometimes overwhelming Visual Studio IDE. I used it on a new project as well as an existing codebase, and it was useful. CodeMaid provides shortcuts for working with code that can only help developer productivity, which means less time hunting for something and more time coding. It did not work as well within my Node.js project, but it worked without problems on C# and VB projects.

I've only covered a few of CodeMaid's features, so take it for a test drive and see if it deserves a spot in your toolbox. If you've used CodeMaid, do you like it? Let us know in the discussion.

About

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

1 comments
RobJGood
RobJGood

Thanks for the write up, love reviews that simply tell me what the author likes and uses!

Editor's Picks