Writing custom refactor: finding public methods that can "safely" be made internal/protected/private.

In my experience too many developers make things public too often and when looking to refactor a module the first thing I want to know is how big its surface area is , if everything is public this can be a challenge. using search and replace to change public to internal and then seeing what breaks is an unsubtle and time consuming way of gather in this data.

What I want: To write a refactor which can find and suggest public classes and methods which could be made "safely" made internal.

Because they are not referenced by classes out-side the assembly, and are not involved in implementing an interface.

I'm an ok cook-book programmer and I've tried using  hararis clean code project ( https://github.com/hhariri/CleanCode ) as a template for implementing this myself.

I've found the SDK https://www.jetbrains.com/help/resharper/sdk/Intro/ReSharperPlatform.html

But I find the resharper nuget module somewhat "undiscoverable".

Anyone recommend an existing project as a better starting point - or some good beginners documentation for the resharper API ?




1 comment
Official comment

Hi Barry. The Clean Code plugin is a good place to start looking at code that performs inspections, as is the Cyclomatic Complexity plugin, and the Unity support is a good sized plugin with a lot of different functionality.

But what you're looking at is a lot more complicated - you essentially need to keep track of every method definition and whether or not it's being called. This is a huge amount of data and dependencies to keep track of and manage in an efficient manner.

Fortunately, ReSharper already does this!

By default, ReSharper will only analyse open files. It will create usage information for that file, but it is limited in scope to that file, which means it can only highlight private fields, methods, etc. as redundant - anything public might be used in another file.

But if you enable Solution Wide Analysis, ReSharper will analyse the whole solution, instead of just looking at currently open files. It runs all of the inspections on all the files, and will show all warnings and errors in a single tool window. This will help if you change a class or type member to be private - any errors in the solution are immediately displayed.

But better than that, Solution Wide Analysis also tracks usage data, and knows if a type member isn't called, even if it's public. So ReSharper can mark methods as redundant, or mark them as being able to change to private/internal. This sounds just like what you're after.

Please sign in to leave a comment.