Reader small image

You're reading from  Refactoring with C#

Product typeBook
Published inNov 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781835089989
Edition1st Edition
Languages
Right arrow
Author (1)
Matt Eland
Matt Eland
author image
Matt Eland

Matt Eland is a Microsoft MVP in Artificial Intelligence (AI) who has been working with .NET since 2001. Matt has served as a senior engineer, software engineering manager, and .NET programming instructor. He is currently an AI specialist and senior consultant at Leading EDJE near Columbus, Ohio, where he helps companies with their software engineering and data science needs using C# and related technologies. Matt speaks and writes in his community and co-organizes the Central Ohio .NET Developers Group while pursuing a master's degree in data analytics.
Read more about Matt Eland

Right arrow

Introduction to Refactoring

The best way to learn refactoring is to look at an example. In this chapter, we’ll explore a sample refactoring scenario using C# and Visual Studio and see firsthand how refactoring can transform the maintainability of code without altering its functionality.

In this chapter, we’re going to cover the following main areas:

  • Refactoring a baggage price calculator
  • Refactoring in other editors

Along the way, we’ll cover refactorings around introducing locals, constants, and parameters, extracting methods, and removing unreachable/unused code, as well as touching upon the importance of testing in any refactoring endeavor.

Technical requirements

If you want to follow along with this chapter, you can clone this book’s code from GitHub at https://github.com/PacktPublishing/Refactoring-with-CSharp.

The starting code for this chapter can be found in the Chapter02/Ch2BeginningCode folder after cloning the repository.

Refactoring a baggage price calculator

We’ll start by examining a baggage price calculator used by the staff of Cloudy Skies Airline during baggage checks to determine the amount an individual customer must pay.

The rules for baggage pricing are as follows:

  • All carry-on baggage costs $30 per bag
  • The first checked bag a passenger checks costs $40
  • Each subsequent checked bag costs $50
  • If the travel occurs during the holidays, a 10% surcharge is applied

This code lives in a C# BaggageCalculator class that we’ll review in a few blocks of code, starting with the class definition, field, and full property:

BaggageCalculator.cs:

public class BaggageCalculator {
  private decimal holidayFeePercent = 0.1M;
  public decimal HolidayFeePercent {
    get { return holidayFeePercent; }
    set { holidayFeePercent = value; }
  }

This is a simple class with an older style of property...

Refactoring in other editors

Before we end the chapter, let’s talk about refactoring in editors other than Visual Studio.

This book primarily focuses on refactoring in Visual Studio because that’s the current primary development environment for .NET developers. However, there are a few other editors and extensions that are frequently used for .NET development and offer refactoring support:

  • Visual Studio Code
  • JetBrains Rider
  • JetBrains ReSharper (Visual Studio Extension)

These tools will not be featured in examples throughout the remainder of the book since Visual Studio is the primary editing experience. However, most of what I’ll show you in the remainder of the book is also possible using these tools.

Refactoring in Visual Studio Code with the C# Dev Kit

Visual Studio Code (VS Code) is rapidly becoming a highly capable editing environment for .NET projects with its C# extension.

Where VS Code really comes into its own is with...

Summary

Throughout this chapter, we explored refactoring by taking a class with a bit of complexity and applied targeted refactorings to make it easier to read, maintain, and expand.

We went from a modestly complex class to a relatively simple one by following a set of repeatable actions that transformed the code from one form to another without changing its overall behavior or result.

Although Visual Studio supports very capable refactoring tools, it is up to you as an experienced developer to know when you might want to apply each individual refactoring, based on the current level of complexity of your code and the code smells you’re observing.

Over the next three chapters, we’ll explore the built-in refactorings in more depth by exploring refactorings related to methods, classes, and individual lines of code.

Questions

  1. What are some ways of triggering Quick Actions for a block of code?
  2. Does Visual Studio ever indicate that refactorings are possible or recommended?
  3. How can you know what a Quick Action will do before performing it?
  4. Are Visual Studio Quick Actions the only way to refactor code?

Further reading

You can find more information about refactoring in Visual Studio and other environments at these URLs:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Refactoring with C#
Published in: Nov 2023Publisher: PacktISBN-13: 9781835089989
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Matt Eland

Matt Eland is a Microsoft MVP in Artificial Intelligence (AI) who has been working with .NET since 2001. Matt has served as a senior engineer, software engineering manager, and .NET programming instructor. He is currently an AI specialist and senior consultant at Leading EDJE near Columbus, Ohio, where he helps companies with their software engineering and data science needs using C# and related technologies. Matt speaks and writes in his community and co-organizes the Central Ohio .NET Developers Group while pursuing a master's degree in data analytics.
Read more about Matt Eland