Another reason to love Resharper: it can make you look like a LINQ-ninja

For those of who spend our time immersed in code, we're usually trying to find ways to write better code that more clearly expresses our intent.  And in a place like EMC Consulting there is a very healthy competition between devs to write code that is better, cleaner, faster, etc.  One of the new features of Resharper 5.0 (R#) manages to address both of these points!  [:D]

Jetbrains have given R# a pretty deep understanding of LINQ.  To be honest, I'm feeling like R# knows LINQ considerably better than I do!  You see, R# 5.0 will offer to convert your code into LINQ statements wherever it can.  Here's an example:

My code:

   1: foreach(var summaryType in TypeMapper.GetMapper(entityType.Assembly)
   2:           .GetSummaryTypes(entityType))
   3: {
   4:   if(typeof(VersionableSummaryBase).IsAssignableFrom(summaryType))
   5:   {
   6:     var summary = (VersionableSummaryBase)EntityCache.Instance.RetrieveIfCached(id, summaryType);
   7:     if (((IVersionable)entity).Version < summary.Version)
   8:     {
   9:        mustReload = true;
  10:        break;
  11:     }
  12:   }
  13: }

Applying the R# suggestion:

   1: mustReload = TypeMapper.GetMapper(entityType.Assembly)
   2:                  .GetSummaryTypes(entityType)
   3:                  .Where(summaryType => typeof(VersionableSummaryBase).IsAssignableFrom(summaryType))
   4:                  .Select(summaryType => (VersionableSummaryBase) EntityCache.Instance.RetrieveIfCached(id, summaryType))
   5:                  .Where(summary => summary != null)
   6:                  .Select(summary => summary.Version)
   7:                  .Any(summaryVersion => ((IVersionable) entity).Version < summaryVersion);


Even without understanding what my TypeMapper class is doing, or any of the other details you can clearly see, I think that the second version is easier to read and more clearly expresses the underlying algorithm.  If you have an opinion on this, then please leave it in the comments ... I'd love to get other views.

Here's another gem from R#.  My code:

   1: var sourceElements = new Dictionary<int, EntityBase>();
   2: foreach (EntityBase element in collectionFromSource)
   3: {
   4:     sourceElements.Add(element.InternalID, element);
   5: }

And the R# version:

   1: var sourceElements = collectionFromSource
   2:                         .Cast<EntityBase>()
   3:                         .ToDictionary(element => element.InternalID);

I didn't even know that that particular extension methods existed until R# suggested I use it!  And perhaps with R# 5.0 in my toolkit, I'll be able to get the edge over some of the other devs at work.  At least, until someone blogs about it ... oh bother!

December 2 2009

ReSharper 4.0 from JetBrains is released

The JetBrains team have been working hard at getting functionality and bug fixes into version 4.0 of ReSharper.  I'm very excited to see that it's finally reached RTM status and now fully supports Visual Studio 2008 and all the C# 3.0 goodness!  Go get it here.  For those who have been involved in the EAP program, it's actually build 819.

For those who've never tried R#, I strongly suggest that you take it for a spin.  The improvements it brings to productivity are just immense.  For the full list of features the JetBrains site has all the info, but here are a couple of my favourite features:

Go To File / MemberResharper_navigation
This functionality is brilliant at quickly navigating around large solutions.  You can type in the full name of the class / method / file that you are after, just the capital letters, you can use wildcards or you can combine all these techniques.

Refactoring Options
Yes I know that Visual Studio has included some refactoring functionality for a while now, but this one of ReSharper primary strengths.  It includes options such as:

  • Rename class / method / variable / parameter / field / property (optionally searches within comments and strings)
  • Change signature
  • Extract / inline a method
  • Extract interface / superclass
  • Push members up / pull members down
  • Introduce parameter
  • Introduce field
  • Introduce variable (I esp like the way it detects multiple occurrences of an expression of offers to replace them all)
  • Convert anonymous delegates to / from lambdas
  • Set field values from constructor arguments

There's so many of these - these are just the ones I can think of right now.  In fact, there are so many that I always keep a copy of the ReSharper keyboard mappings on my desk.

Coding in Reverse
Undoubtedly the most powerful feature available within ReSharper, JetBrains picked it out as the primary skill of the ReSharper Jedi.  This is a technique where you use variables / methods / classes before they exist.  ReSharper will then infer their definition from your usage and allow you to actualize them with a couple of keystrokes.  It's immensely powerful, but it's really got to be seen to be fully appreciated.

And More
I strongly ReSharper to all developers who want to improve their productivity ... which should be all developers.  I also strongly suggest that you take some time out to learn the featureset and some of the options available within ReSharper.  Joe White's blog series The 31 Days of ReSharper is a great place to start this journey.  Just be warned that you might get seriously hooked.

June 9 2008
Older Posts