Regionerate Helps Keep C# Code Clean

by Andrew Barber 13. October 2009 05:06

Regionerate is an open-source add-in for Visual Studio which enables developers to easily apply code layout rules in C# source. I've been using it for a few months now, and have found it to be incredibly useful, with only two issues; one not really even worth mentioning, but the other could be a problem for some.

First, though; what it does! From the Regionerate Introduction page:

Regionerate is an automated tool which helps you to preserve your code's layout over time. Regionerate lets you define regions in your code and determine the way members (fields, methods, properties etc.) should be placed inside them.

Regionerate is a zero-friction tool - when setting up, you can choose a Code Layout (the way you want your code to look) or just use the default Code Layout. From that moment on, Regionerate will make sure your code follows that Code Layout.

When adding new members to a class, it is easiest not to have to think about exactly where to put those members, but just add it in the first open space I find. Also, many of the Visual Studio built-in tools don't give you an option where they will put members which are automatically generated. Having to stop, find the right region for a member type, and then spend even the small amount of effort to put that member in proper alphabetical order is distracting when one is knee-deep coding something.

What It Can Do

With Regionerate, you can set up multiple layouts for code organization. As it's name implies, you can choose to group member types into regions, if you wish. Or, you can simply have member groups have a comment header announcing their location. Or both. Or neither. You can group members by type, scope, modifiers and custom pattern matches; define as many or as few specifications for each group that you wish, and the order you define the groups dictates the order in which they will appear. Each member is placed in the first group it matches entirely, even if another later group also matches it.

This lets you, for example, sort the Static members first, if you wish. Within that, perhaps you want fields, then properties. Separate them further by their visibility, if you like. Maybe the constructor next, and finally all other Static members. My primary layout has one group for Instance members which uses pattern matching to have all of the Windows Forms and web forms Lifecycle override functions in their own group (my layout file lumps them all together in one group, since only one or the other could exist in any one class).

Within each group, you may specify how the members are sorted; Alphabetically and by the number of parameters are quite useful; My layout sorts Alphabetically first, and then by the number of parameters, descending; This causes overloaded functions to have their largest overload at the top. Members themselves are not altered in any way - they are simply moved within the source file. Attributes and documentation comments are handled as expected, so no worries there (but see below about pre-processor directives).

Regionerate comes with a few good layouts, and others can be downloaded; but you may end up creating your own, as I did. At any given moment, I simply press Control-R, and let my class get sorted for me.

Quirks, Limitations

The most notable limitation is that this tool is only for C#. Visual Basic.NET could stand to have a tool for this, too! Even if it worked for C++, I would not be able to use it myself, though, because of the one notable issue I've found with it;

Regionerate (at least, the current version, 0.7.0.0) does not seem to know what to do with pre-processor directives. If I remember correctly, it treated them as if they were comments. Obviously, that can be a bad thing. The Regionerate Bug Report Forums have reports about various other constructs seemingly causing trouble, but I can not say I've witnessed any of the issues mentioned after a quick scan just now. YMMV; Be ready to check what it has done, and press Control-Z if need be!

Of course, any automatic code beautification add-ins you have may interfere with what Regionerate does. The default Control-R to activate Regionerate also causes a mild bit of confusion with my installation of Code Rush; After Regionerating, whatever the next key I press has no effect, and causes Code Rush to report No Code Rush Binding for Ctrl+R, X where X is whatever key was pressed next. I therefore have a habit of pressing an arrow key after using the Regionerate shortcut key.

Why Eels?

No one can really be certain. But those slimey underwater critters obviously have something going for them!

Links/Profile

Andrew Barber's Profiles:
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent the views of employees, contractors or clients of Inkwell Creative Group, LLC in any way.

© Copyright 2008, 2009 Andrew Barber