Make Code More Readable by Refactoring It With ReSharper
Refactoring is the process of restructuring code to improve it. The improvements may be making the code more readable, formatting the code, or organizing it better.
Refactoring shouldn’t change the behavior of code, just the quality of it. Readability is an important factor in the quality of code. If you can’t read the code, you can’t maintain it well. ReSharper helps you refactor to make your code more readable and understandable.
Introduce variables for readability
Often, developers use the same expression or property over and over. When that happens the code isn’t very readable. Often, it causes the developer to have to navigate to the property declaration to recall the property’s purpose. The code is also more prone to bugs. ReSharper has the Introduce Variable intention to help you! Use Alt+Enter and convert those overused expressions to variables.
One way to help make code more readable is adhering to the "Don’t Repeat Yourself (DRY) principle. Often, during development, code is copied or repeated a few times. This makes it more difficult to read the code, as you need to verify it is the same code and doing the same thing. It also makes introducing bugs easier, since a change in one copy of the code likely won’t be reflected in all the copies.
ReSharper has the Extract Method refactoring to help in these cases. Additionally, using the Extract Method refactoring helps keep the size of methods smaller, which in turn makes them easier to read and control defects.
In this example, we can extract the logic that calculates the fee for a bank account into its own method, and apply the fee in this one. Use Ctrl+Shift+R to refactor.
A quick switch to readability
Code that is readable is code that humans can scan through without physically jarring their eyes, or having to pause for long to figure out simple statements.
Simple branching and switching statements should flow easily so that human eyes can easily scan them.
In the following example, an
if statement makes a decision based off of an enum. This is easier to read and more efficient if we change it to the block structure format of a
switch statement. A quick Alt+Enter gives us the option to switch to a
switch. The code is smaller, more succinct and easier to read now. This refactoring works in reverse in case a
switch statement exists and an
if statement reads better.
Short and sweet readability
Shortening a long form assignment is a tiny but useful refactoring. There’s always some code that can be shortened for readability.
A great example is the
balance variable when the fee is deducted. As you can see, it’s a long hand assignment,
_balance = _balance - _fee;. ReSharper notices this and displays an indicator lightbulb letting you know there’s an improvement to make with a quick Alt+Enter. Then you can you can shorten the expression to a compound assignment. This is a tiny, nice, modern refactoring.
Invert for a more readable style
Sometimes, a branching structure doesn’t look right, or seems to have a difficult flow to it. Or perhaps, the agreed upon team standard is different from the block of code you’ve come across. Either way, ReSharper is there with Alt+Enter to help you refactor it to more readable and better flowing code.
var readable = true ? “code is readable” : “code is not readable”
Sometimes, readability is in the eye of the beholder. This is certainly the case with
if statements versus the ternary operator. Some folks prefer a style closer to English-language with
if, and others prefer the compact syntax of the
No matter which is easier on your eyes, ReSharper has you covered. Just press Alt+Enter!
ReSharper has many excellent refactorings that help you keep your code as readable and maintainable as possible. Try out refactorings not mentioned here by placing the caret on any programming construct, and pressing Alt+Enter or Ctrl+Shift+R for Refactor This.
Download ReSharper now! And let us know what you find!