ReSharper, day 29: Refactoring with interfaces

Welcome to day 29 of my 31 Days of ReSharper.

Working with interfaces is, traditionally, pretty frustrating. You need them when you’re writing testable code, so you can fake the filesystem and pass in null objects, but they’re a pain when you’re trying to follow what the code is actually doing.

ReSharper has several features that make it much easier to work with interfaces. I’ve already covered two, Navigate to Inheritor and the Type Hierarchy view.

Today I’ll cover another ReSharper feature that’s handy when you’re working with interfaces: the “base method” prompt.

ReSharper prompt: Method 'Engine.RoomScreen.Update(int)' implements method from interface 'IScreen'. Do you want to rename the base method?

If you apply a refactoring — for example, a rename — to a method, and that method is there to satisfy an interface, ReSharper will ask if you want to apply that refactoring to the interface instead.

This dialog box kind of klunky — it disrupts the flow of my thinking, and it’s not the easiest question to understand. It just doesn’t work the way I think. Unfortunately, I don’t have any suggestions for improving it.

That said, I do love this feature. If I decide to rename a method, ReSharper will automatically jump in there and ask if I want to rename it on the interface as well. Usually the answer is yes, because that’ll keep the code compilable. (Sometimes the “yes” is preceded by “Oh yeah, that method is from an interface, isn’t it?”).

This also applies to Find Usages. If you try to Find Usages on a method that implements an interface, you’ll get asked “did you mean the method here, or the same method when accessed via the interface?” In this case, you may have to do the search twice to really get all the usages of that method (although if you’re diligent about only using the class through the interface, then you could just answer Yes and only do the search once.)

Leave a Reply

Your email address will not be published. Required fields are marked *