Refactor object-oriented code with ReSharper
Object-oriented programming is a popular programming paradigm. Many developers create object models for use in object-oriented systems. When those models are built, they often need to undergo some refactoring so that they are efficient, readable, and maintainable. ReSharper is there to help you make your object models better.
You may have created a specific class such as a
CheckingAccount class, and later find out that requirements have changed and you’ll need other types of accounts that are specific as well. This means you’ll have to modify your object model so that there is a generic base class, and the
CheckingAccount and new classes derive from it.
Use Refactor This (Ctrl+Shift+R) in ReSharper to create super classes and derived classes in a snap.
In the following example, the members of the
CheckingAccount class are more appropriate in the
BankAccount base class.
From this point you can create new derived classes from the parent
BankAccount class, such as a
During this process, you might find that some members in a class are now not well suited for that class. Since you have an inheritance hierarchy, you can move members around so they fit into the right class. No need to cut and paste! Use Alt+Enter to push members up or down the inheritance chain.
As you go through the code, you’ll notice that ReSharper indicates opportunities for improvement. Use Alt+Enter, and see what the intention suggests.
As an example, a field such as the
_minimumBalance is something that should be set in the constructor, perhaps after reading the value from a database.
Many developers mock-up classes in code by adding a series of fields and they later change them into properties. This workflow gives an overall picture of the classes, their properties, and their relationships in code. But then you must refactor many of the fields into properties to enforce business rules about the data in those classes.
ReSharper contains an Encapsulate Field refactoring that converts fields to properties – auto properties, expression-bodied properties, or read-only or write-only properties.
Utilizing ReSharper’s expression-bodied property refactorings are an excellent way to modernize the syntax of an existing .NET application. Expression-bodied syntax makes the code more concise and readable.
Expression-bodied properties are backing properties that have been condensed into a single expression, using a lambda. Use Alt+Enter for this refactoring.
ReSharper allows you to change the access modifiers of a property or method between private, public, internal, protected, and private protected, with Alt+Enter. This enables you to create a robust object-oriented data model.
Refactor for class maintainability
Developers may put classes in a single file for various reasons. Sometimes, this ends up with a bloated and difficult-to-maintain file. Moving these classes to their own locations helps make software more maintainable.
ReSharper notices when there are multiple files in a class and displays a hint notifying you that classes can be moved to their own file. Use Alt+Enter to move the classes and verify the file name.
ReSharper is great for creating and refactoring object-oriented models. With just a few keystrokes, you can form object models to meet the precise requirements of your application. It covers all object-oriented programming refactorings from abstraction to properties.
Try out ReSharper, then let us know what you think!
Subscribe to Blog updates
Thanks, we've got you!
dotCover, dotMemory, dotPeek, and dotTrace 2023.3 Release
dotCover 2023.3, dotMemory 2023.3, dotPeek 2023.3, and dotTrace 2023.3 have been released and are ready for download! Let’s take a look at what’s new with these .NET tools. dotMemory 2023.3 dotMemory Standalone is now available on Linux and macOS. dotMemor…
ReSharper 2023.3: Support For C# 12, Performance Enhancements, and More AI-Powered Features
ReSharper 2023.3 and new versions of other JetBrains .NET tools have just been released. This version brings support for the latest C# 12 features, optimized background code analysis, and the ability to control the amount of resources allocated to solution-wide code analysis. AI Assistant has also r…
Rider 2023.3: Support for .NET 8 SDK, the Latest From C# 12, Performance Enhancements, Predictive Debugging, and More AI-powered Features.
A major update for Rider has just been published! Rider 2023.3 offers support for the .NET 8 SDK, including the latest features from C# 12. Other enhancements include an improved experience when running multiple projects, a predictive debugging mode, and the ability to easily create and navigate thr…
Another Look into the Future with Rider’s Predictive Debugger
In the 2023.2 release cycle, we’ve introduced the Predictive Debugger in ReSharper, which gives you predictions about code paths and variables beyond the current execution pointer. We’ve written extensively about its advantages compared to alternative debugging strategies like thorough thinking, log…