Prevent Resharper From Adding Regions
A couple of days ago I was annoyed that Resharper was insisting on turning my abstract base NUnit test class with nothing in it but a shared [SetUp] method into a one line class with a collapsed Setup / Teardown region in it. While I didn’t always feel this way, my experience has taught me that regions are a smell in your code. They are a way to hide things you don’t want to deal with or look at. It’s kind of like putting makeup over a melanoma instead of having a doctor remove it. Here’s a pretty good analysis of why regions are a code smell if you’re interested.
So anyway, I’m using Resharper and am in general a huge fan, and I know I can configure this thing every which way, but I look through the options and nothing jumps out at me about how to adjust Region settings for NUnit tests. I manually removed the region and checked in my code and, rather than spending more time and effort researching a solution, griped about it on twitter today. Not long after, Todd Ropog let me know about this:
Prevent Resharper from adding Regions to Interfaces
Chris outlines the steps nicely:
This is why I never found what I was looking for – if you don’t uncheck the “Use Default Patterns” checkbox, you never even see the XML used. Once you see this:
you’re pretty much there. All you need to do is look for things that say:
<Group>
<Name Region="Some Region Name"/>
</Group>
So, to sum up:
- Regions can indicate a code smell and should be used sparingly.
- Resharper is a fabulous tool…
- …but it’s so darned flexible that finding how to do some things can be hard.
- Twitter can be a great way to get other people to tell you how to do things you don’t know how to do. :)
- Hopefully this helps a few people Googling/Binging or having their friends Tweet how to remove regions from Resharper code cleanup in the future.




Comments
Chris Marisic said on 05 Mar 2010 at 10:00 AM
I'm the opposite I actually use a formatting file that adds alot more granular regions
exceptionz.wordpress.com/.../resharper-30-me
I did some patching to it for it to group MSTest correctly also
<!--Special formatting of NUnit test fixture-->
<Pattern RemoveAllRegions="true">
<Match>
<And Weight="100">
<Kind Is="class"/>
<Or>
<HasAttribute CLRName="NUnit.Framework.TestFixtureAttribute" Inherit="true"/>
<HasAttribute CLRName="Microsoft.VisualStudio.TestTools.UnitTesting.TestClass" Inherit="true"/>
</Or>
</And>
</Match>
<!--Setup/Teardow-->
<Entry>
<Match>
<And>
<Kind Is="method"/>
<Or>
<HasAttribute CLRName="NUnit.Framework.SetUpAttribute" Inherit="true"/>
<HasAttribute CLRName="NUnit.Framework.TearDownAttribute" Inherit="true"/>
<HasAttribute CLRName="NUnit.Framework.FixtureSetUpAttribute" Inherit="true"/>
<HasAttribute CLRName="NUnit.Framework.FixtureTearDownAttribute" Inherit="true"/>
<HasAttribute CLRName="Microsoft.VisualStudio.TestTools.UnitTesting.TestInitialize" Inherit="true"/>
<HasAttribute CLRName="Microsoft.VisualStudio.TestTools.UnitTesting.ClassInitialize" Inherit="true"/>
<HasAttribute CLRName="Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanup" Inherit="true"/>
<HasAttribute CLRName="Microsoft.VisualStudio.TestTools.UnitTesting.ClassCleanup" Inherit="true"/>
</Or>
</And>
</Match>
<Group Region="Setup/Teardown"/>
</Entry>
<!--All other members-->
<Entry/>
<!--Test methods-->
<Entry>
<Match>
<And Weight="100">
<Kind Is="method"/>
<Or>
<HasAttribute CLRName="Microsoft.VisualStudio.TestTools.UnitTesting.TestMethod" Inherit="true"/>
<HasAttribute CLRName="NUnit.Framework.TestAttribute" Inherit="false"/>
</Or>
</And>
</Match>
<Sort>
<Name/>
</Sort>
</Entry>
</Pattern>
ssmith said on 06 Mar 2010 at 12:43 PM
@Chris,
When C# first came out, I was a fan of regions. They were nice at hiding things I didn't really care about, like the auto-generated goo in the ASP.NET 1.0 codebehind files. But in using them in my own classes, I've shifted over the years to considering them a symptom. If I'm inclined to hide things in regions, it's like sweeping things under the rug rather than actually cleaning. If the class is too complex, or has stuff in it that doesn't really belong, it's better to refactor than to region/hide. It's also annoying when I work in others' codebases and everything is hidden away in collapsed regions, because it obscures intent (and often skeletons that should be killed).