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!