Book Image

Refactoring with Microsoft Visual Studio 2010

By : Peter Ritchie
Book Image

Refactoring with Microsoft Visual Studio 2010

By: Peter Ritchie

Overview of this book

<p>Changes to design are an everyday task for many people involved in a software project. Refactoring recognizes this reality and systematizes the distinct process of modifying design and structure without affecting the external behavior of the system. As you consider the benefits of refactoring, you will need this complete guide to steer you through the process of refactoring your code for optimum results.<br /><br />This book will show you how to make your code base more maintainable by detailing various refactorings. Visual Studio includes some basic refactorings that can be used independently or in conjunction to make complex refactorings easier and more approachable. This book will discuss large-scale code management, which typically calls for refactoring. To do this, we will use enterprise editions of Visual Studio, which incorporate features like Application Performance Explorer and Visual Studio Analyzer. These features make it simple to handle code and prove helpful for refactoring quickly.<br /><br />This book introduces you to improving a software system's design through refactoring. It begins with simple refactoring and works its way through complex refactoring. You will learn how to change the design of your software system and how to prioritize refactorings—including how to use various Visual Studio features to focus and prioritize design changes. The book also covers how to ensure quality in the light of seemingly drastic changes to a software system. You will also be able to apply standard established principles and patterns as part of the refactoring effort with the help of this book. You will be able to support your evolving code base by refactoring architectural behavior. As an end result, you will have an adaptable system with improved code readability, maintainability, and navigability.</p>
Table of Contents (17 chapters)
Refactoring with Microsoft Visual Studio 2010
Credits
About the Author
Acknowledgement
About the Reviewers
Preface
6
Improving Class Quality
9
Improving Architectural Behavior

Preface

This book introduces the reader to improving a software system's design through refactoring.

It begins with simple refactorings and works its way through complex refactorings by building on the simple refactorings. You will learn how to focus changing the design of their software system and how to prioritize refactorings including how to use various Visual Studio features to focus and prioritize design changes. The book also covers how to ensure quality in light of seemingly drastic changes to a software system. You will also be able to apply standard established principles and patterns as part of the refactoring effort with the help of this book.

What this book covers

Chapter 1, Introducing Refactoring, describes what refactoring is, its importance, and its priority in the software development effort. Comparison to re-writing and what "Technical Debt" is and how refactoring can be used to pay down technical debt is covered in this chapter.

Chapter 2, Improving Code Readability, begins detailing the refactorings built in to Visual Studio and how they can make code more readable. Code smells are introduced, and which code smells apply to readability, and how to detect and refactor them are detailed in this chapter.

Chapter 3, Improving Code Maintainability, continues to detail the refactorings built in to Visual Studio and how they can make code more maintainable. Code smells that apply to maintainability, how to detect and refactor them are detailed in this chapter. The importance of unit testing is covered in this chapter.

Chapter 4, Improving code navigation, continues with simple refactorings and how code can be refactored to improve its navigability in general and takes into account Visual Studio code navigation abilities.

Chapter 5, Improving design correctness, begins detailing complex refactorings. Design principles such as Liskov Substitution and Composition over Inheritance are introduced and how to perform refactorings related to these principles is covered in this chapter.

Chapter 6, Improving class quality, introduces code quality metrics like cohesion and coupling. Principles related to cohesion and coupling are introduced and refactorings that increase cohesion and decrease coupling are covered in this chapter.

Chapter 7, Refactoring to loosely-coupled, expands on coupling from the previous chapter and drills-down on loosely-coupled design. Principles related to loosely-coupled are introduced and complex refactorings related to loosening coupling are covered in this chapter.

Chapter 8, Refactoring to layers, continues with more complex refactorings that involve layered architectures. Typical layers, Model View Presenter, and Repository patterns and how and when to refactor to them are also detailed in this chapter.

Chapter 9, Improving architectural behavior, details complex refactorings to improve architectural behavior. Design behavior patterns, when and how to refactor to them are detailed in this chapter.

Chapter 10, Improving architectural structure, continues with architectural-related complex refactorings. Object-Relational Mapping (ORM) and refactoring Repository implementations are included in this chapter.

Chapter 11, Ensuring Quality with Unit Testing, details the importance of unit testing. How unit testing applies to refactoring, examples of unit testing to support the refactoring effort, and legacy code are also detailed in this chapter.

What you need for this book

We will use examples in C# in Visual Studio 2010, but the concepts can be applied to any version of Visual Studio since version 2005.

Who this book is for

This book is primarily for developers who want to refactor their code in Visual Studio. However, the book can be used by anyone using Visual Studio. Developers, designers, and architects who are eager to improve the performance of their craft will find this book useful because it details refactoring existing code to use recognized and established patterns and principles to improve code structure and architectural behavior. The book assumes that the reader knows both Visual Studio and C#. No previous knowledge of refactoring is required.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

foreach (InvoiceLineItem invoiceLineItem in
InvoiceLineItems)
{
invoiceSubTotal +=
(float)((decimal)(invoiceLineItem.Price
- invoiceLineItem.Discount)
* (decimal)invoiceLineItem.Quantity);
}

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

foreach (InvoiceLineItem invoiceLineItem in
InvoiceLineItems)
{
invoiceSubTotal +=
(float)((decimal)(invoiceLineItem.Price
- invoiceLineItem.Discount)
* (decimal)invoiceLineItem.Quantity);
}

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Note

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to, and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book on, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Note

Downloading the example code for the book

You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books maybe a mistake in the text or the code we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the let us know link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.