Refactoring: Improving the Design of Existing Code (Hardcover)
暫譯: 重構:改善現有程式碼的設計 (精裝版)

Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts

買這商品的人也買了...

相關主題

商品描述

Book Description
Book Description
As the application of object technology-particularly the Java programming language-has become commonplace, a new problem has emerged to confront the software development community. Significant numbers of poorly designed programs have been created by less-experienced developers, resulting in applications that are inefficient and hard to maintain and extend. Increasingly, software system professionals are discovering just how difficult it is to work with these inherited, "non-optimal" applications. For several years, expert-level object programmers have employed a growing collection of techniques to improve the structural integrity and performance of such existing software programs. Referred to as "refactoring," these practices have remained in the domain of experts because no attempt has been made to transcribe the lore into a form that all developers could use. . .until now. In Refactoring: Improving the Design of Existing Software, renowned object technology mentor Martin Fowler breaks new ground, demystifying these master practices and demonstrating how software practitioners can realize the significant benefits of this new process.

With proper training a skilled system designer can take a bad design and rework it into well-designed, robust code. In this book, Martin Fowler shows you where opportunities for refactoring typically can be found, and how to go about reworking a bad design into a good one. Each refactoring step is simple-seemingly too simple to be worth doing. Refactoring may involve moving a field from one class to another, or pulling some code out of a method to turn it into its own method, or even pushing some code up or down a hierarchy. While these individual steps may seem elementary, the cumulative effect of such small changes can radically improve the design. Refactoring is a proven way to prevent software decay.

In addition to discussing the various techniques of refactoring, the author provides a detailed catalog of more than seventy proven refactorings with helpful pointers that teach you when to apply them; step-by-step instructions for applying each refactoring; and an example illustrating how the refactoring works. The illustrative examples are written in Java, but the ideas are applicable to any object-oriented programming language.

Book Info
Demonstrates where opportunities for refactoring typically lie, and how to go about reworking a bad design into a good one using elementary steps. DLC: Software refactoring.


From the Inside Flap

Once upon a time, a consultant made a visit to a development project. The consultant looked at some of the code that had been written; there was a class hierarchy at the center of the system. As he wandered through the hierarchy, the consultant saw that it was rather messy. The higher-level classes made certain assumptions about how the classes would work, assumptions that were embodied in inherited code. That code didn't suit all the subclasses, however, and was overridden quite heavily. If the superclass had been modified a little, then much less overriding would have been necessary. In other places some of the intention of the superclass had not been properly understood, and behavior present in the superclass was duplicated. In yet other places several subclasses did the same thing with code that could clearly be moved up the hierarchy.

The consultant recommended to the project management that the code be looked at and cleaned up, but the project management didn't seem enthusiastic. The code seemed to work and there were considerable schedule pressures. The managers said they would get around to it at some later point.

The consultant had also shown the programmers who had worked on the hierarchy what was going on. The programmers were keen and saw the problem. They knew that it wasn't really their fault; sometimes a new pair of eyes are needed to spot the problem. So the programmers spent a day or two cleaning up the hierarchy. When they were finished, the programmers had removed half the code in the hierarchy without reducing its functionality. They were pleased with the result and found that it became quicker and easier both to add new classes to the hierarchy and to use the classes in the rest of the system.

The project management was not pleased. Schedules were tight and there was a lot of work to do. These two programmers had spent two days doing work that had done nothing to add the many features the system had to deliver in a few months time. The old code had worked just fine. So the design was a bit more "pure" a bit more "clean." The project had to ship code that worked, not code that would please an academic. The consultant suggested that this cleaning up be done on other central parts of the system. Such an activity might halt the project for a week or two. All this activity was devoted to making the code look better, not to making it do anything that it didn't already do.

 

How do you feel about this story? Do you think the consultant was right to suggest further clean up? Or do you follow that old engineering adage, "if it works, don't fix it"?

I must admit to some bias here. I was that consultant. Six months later the project failed, in large part because the code was too complex to debug or to tune to acceptable performance.

The consultant Kent Beck was brought in to restart the project, an exercise that involved rewriting almost the whole system from scratch. He did several things differently, but one of the most important was to insist on continuous cleaning up of the code using refactoring. The success of this project, and role refactoring played in this success, is what inspired me to write this book, so that I could pass on the knowledge that Kent and others have learned in using refactoring to improve the quality of software. What Is Refactoring?

Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure. It is a disciplined way to clean up code that minimizes the chances of introducing bugs. In essence when you refactor you are improving the design of the code after it has been written.

 

"Improving the design after it has been written." That's an odd turn of phrase. In our current understanding of software development we believe that we design and then we code. A good design comes first, and the coding comes second. Over time the code will be modified, and the integrity of the system, its structure according to that design, gradually fades. The code slowly sinks from engineering to hacking.

Refactoring is the opposite of this practice. With refactoring you can take a bad design, chaos even, and rework it into well-designed code. Each step is simple, even simplistic. You move a field from one class to another, pull some code out of a method to make into its own method, and push some code up or down a hierarchy. Yet the cumulative effect of these small changes can radically improve the design. It is the exact reverse of the normal notion of software decay.

With refactoring you find the balance of work changes. You find that design, rather than occurring all up front, occurs continuously during development. You learn from building the system how to improve the design. The resulting interaction leads to a program with a design that stays good as development continues. What's in This Book?

This book is a guide to refactoring; it is written for a professional programmer. My aim is to show you how to do refactoring in a controlled and efficient manner. You will learn to refactor in such a way that you don't introduce bugs into the code but instead methodically improve the structure.

 

It's traditional to start books with an introduction. Although I agree with that principle, I don't find it easy to introduce refactoring with a generalized discussion or definitions. So I start with an example. Chapter 1 takes a small program with some common design flaws and refactors it into a more acceptable object-oriented program. Along the way we see both the process of refactoring and the application of several useful refactorings. This is the key chapter to read if you want to understand what refactoring really is about.

In Chapter 2 I cover more of the general principles of refactoring, some definitions, and the reasons for doing refactoring. I outline some of the problems with refactoring. In Chapter 3 Kent Beck helps me describe how to find bad smells in code and how to clean them up with refactorings. Testing plays a very important role in refactoring, so Chapter 4 describes how to build tests into code with a simple open-source Java testing framework.

The heart of the book, the catalog of refactorings, stretches from Chapter 5 through Chapter 12. This is by no means a comprehensive catalog. It is the beginning of such a catalog. It includes the refactorings that I have written down so far in my work in this field. When I want to do something, such as Replace Conditional with Polymorphism (255), the catalog reminds me how to do it in a safe, step-by-step manner. I hope this is the section of the book you'll come back to often.

In this book I describe the fruit of a lot of research done by others. The last chapters are guest chapters by some of these people. Chapter 13 is by Bill Opdyke, who describes the issues he has come across in adopting refactoring in commercial development. Chapter 14 is by Don Roberts and John Brant, who describe the true future of refactoring, automated tools. I've left the final word, Chapter 15, to the master of the art, Kent Beck. Refactoring in Java

For all of this book I use examples in Java. Refactoring can, of course, be done with other languages, and I hope this book will be useful to those working with other languages. However, I felt it would be best to focus this book on Java because it is the language I know best. I have added occasional notes for refactoring in other languages, but I hope other people will build on this foundation with books aimed at specific languages.

To help communicate the ideas best, I have not used particularly complex areas of the Java language. So I've shied away from using inner classes, reflection, threads, and many other of Java's more powerful features. This is because I want to focus on the core refactorings as clearly as I can.

 

I should emphasize that these refactorings are not done with concurrent or distributed programming in mind. Those topics introduce additional concerns that are beyond the scope of this book. Who Should Read This Book?

This book is aimed at a professional programmer, someone who writes software for a living. The examples and discussion include a lot of code to read and understand. The examples are all in Java. I chose Java because it is an increasingly well-known language that can be easily understood by anyone with a background in C. It is also an object-oriented language, and object-oriented mechanisms are a great help in refactoring.

Although it is focused on the code, refactoring has a large impact on the design of system. It is vital for senior designers and architects to understand the principles of refactoring and to use them in their projects. Refactoring is best introduced by a respected and experienced developer. Such a developer can best understand the principles behind refactoring and adapt those principles to the specific workplace. This is particularly true when you are using a language other than Java, because you have to adapt the examples I've given to other languages.

Here's how to get the

From the Back Cover
As the application of object technology--particularly the Java programming language--has become commonplace, a new problem has emerged to confront the software development community. Significant numbers of poorly designed programs have been created by less-experienced developers, resulting in applications that are inefficient and hard to maintain and extend. Increasingly, software system professionals are discovering just how difficult it is to work with these inherited, "non-optimal" applications. For several years, expert-level object programmers have employed a growing collection of techniques to improve the structural integrity and performance of such existing software programs. Referred to as "refactoring," these practices have remained in the domain of experts because no attempt has been made to transcribe the lore into a form that all developers could use. . .until now. In Refactoring: Improving the Design of Existing Code, renowned object technology mentor Martin Fowler breaks new ground, demystifying these master practices and demonstrating how software practitioners can realize the significant benefits of this new process.

With proper training a skilled system designer can take a bad design and rework it into well-designed, robust code. In this book, Martin Fowler shows you where opportunities for refactoring typically can be found, and how to go about reworking a bad design into a good one. Each refactoring step is simple--seemingly too simple to be worth doing. Refactoring may involve moving a field from one class to another, or pulling some code out of a method to turn it into its own method, or even pushing some code up or down a hierarchy. While these individual steps may seem elementary, the cumulative effect of such small changes can radically improve the design. Refactoring is a proven way to prevent software decay.

In addition to discussing the various techniques of refactoring, the author provides a detailed catalog of more than seventy proven refactorings with helpful pointers that teach you when to apply them; step-by-step instructions for applying each refactoring; and an example illustrating how the refactoring works. The illustrative examples are written in Java, but the ideas are applicable to any object-oriented programming language.

0201485672B04062001

About the Author

Martin Fowler is an independent consultant who has applied objects to pressing business problems for more than a decade. He has consulted on systems in fields such as health care, financial trading, and corporate finance. His clients include Chrysler, Citibank, UK National Health Service, Andersen Consulting, and Netscape Communications. In addition, Fowler is a regular speaker on objects, the Unified Modeling Language, and patterns.

Kent Beck is a noted programmer, tester, refactorer, author, and banjoist.

John Brant and Don Roberts are the authors of the Refactoring Browser for Smalltalk, which is found at st-cs.uiuc/~brant/RefactoringBrowser/. They are also consultants who have studied both the practical and theoretical aspects of refactoring for six years.

William Opdyke's doctoral research on refactoring object-oriented frameworks at the University of Illinois led to the first major publication on this topic. He is currently a Distinguished Member of Technical Staff at Lucent Technologies/Bell Laboratories.

John Brant and Don Roberts are the authors of the Refactoring Browser for Smalltalk, which is found at st-cs.uiuc/~brant/RefactoringBrowser/. They are also consultants who have studied both the practical and theoretical aspects of refactoring for six years.

 
Table of Contents
N/A

商品描述(中文翻譯)

書籍描述
隨著物件技術的應用,特別是 Java 程式語言,已變得司空見慣,軟體開發社群面臨了一個新問題。許多設計不良的程式是由經驗較少的開發者創建的,導致應用程式效率低下且難以維護和擴展。越來越多的軟體系統專業人士發現,處理這些繼承的「非最佳」應用程式是多麼困難。多年來,專家級的物件程式設計師使用越來越多的技術來改善這些現有軟體程式的結構完整性和性能。這些技術被稱為「重構」,但由於沒有將這些知識轉錄成所有開發者都能使用的形式,因此這些做法一直停留在專家的領域……直到現在。在《重構:改善現有軟體的設計》一書中,著名的物件技術導師馬丁·福勒開創了新局面,揭示了這些大師級的做法,並展示了軟體從業者如何實現這一新過程的顯著好處。

透過適當的訓練,熟練的系統設計師可以將不良設計重新加工成設計良好、穩健的程式碼。在本書中,馬丁·福勒告訴你重構的機會通常可以在哪裡找到,以及如何將不良設計重新加工成良好設計。每個重構步驟都很簡單——似乎過於簡單而不值得去做。重構可能涉及將一個欄位從一個類別移動到另一個類別,或將某些程式碼從一個方法中提取出來,變成自己的方法,甚至將某些程式碼向上或向下推進層級。雖然這些單獨的步驟看似基本,但這些小變化的累積效果可以徹底改善設計。重構是一種防止軟體衰退的有效方法。

除了討論各種重構技術外,作者還提供了超過七十種經過驗證的重構詳細目錄,並附有有用的提示,教你何時應用它們;每個重構的逐步指導;以及一個示例,說明重構如何運作。示例是用 Java 編寫的,但這些想法適用於任何物件導向程式語言。

書籍資訊
展示重構的機會通常位於何處,以及如何使用基本步驟將不良設計重新加工成良好設計。DLC:軟體重構。

內頁介紹
從前,一位顧問造訪了一個開發專案。顧問查看了一些已編寫的程式碼;系統的中心有一個類別層級。當他在層級中漫遊時,顧問發現它相當混亂。高層類別對類別的運作做出了一些假設,這些假設體現在繼承的程式碼中。然而,這些程式碼並不適合所有子類別,並且被大量覆寫。如果超類別稍微修改,那麼就不需要這麼多的覆寫。在其他地方,超類別的一些意圖沒有被正確理解,超類別中存在的行為被重複。在其他地方,幾個子類別用明顯可以向上移動的程式碼做了同樣的事情。

顧問建議專案管理層查看並清理程式碼,但專案管理層似乎並不熱衷。程式碼似乎運作良好,且有相當大的時間壓力。經理們表示他們會在稍後的某個時候處理這個問題。

顧問還向在層級上工作的程式設計師展示了發生了什麼。程式設計師們很熱衷,並看到了問題。他們知道這並不完全是他們的錯;有時需要一雙新眼睛來發現問題。因此,程式設計師們花了一兩天的時間清理層級。當他們完成時,程式設計師們在不減少功能的情況下,移除了層級中一半的程式碼。他們對結果感到滿意,並發現添加新類別到層級中以及在系統的其餘部分使用這些類別變得更快更容易。

專案管理層並不滿意。時間緊迫,工作量很大。這兩位程式設計師花了兩天的時間做的工作並沒有增加系統在幾個月內必須交付的許多功能。舊程式碼運作得很好。因此,設計變得更「純粹」和更「乾淨」。專案必須交付運作的程式碼,而不是能取悅學術界的程式碼。顧問建議在系統的其他核心部分進行這種清理。這樣的活動可能會使專案停滯一兩週。所有這些活動都是為了讓程式碼看起來更好,而不是讓它做任何它尚未做到的事情。

你對這個故事有什麼感覺?你認為顧問建議進一步清理是正確的嗎?還是你遵循那句老工程格言,「如果它能運作,就不要修理它」?

我必須承認這裡有一些偏見。我就是那位顧問。六個月後,專案失敗,主要原因是程式碼過於複雜,無法進行除錯或調整到可接受的性能。

顧問肯特·貝克被請來重啟專案,這是一項幾乎從頭開始重寫整個系統的工作。他做了幾件事情不同,但最重要的一件是堅持不斷清理程式碼,使用重構。這個專案的成功,以及重構在這一成功中所扮演的角色,激勵我寫這本書,以便我能傳遞肯特和其他人在使用重構改善軟體質量方面所學到的知識。什麼是重構?

重構是以不改變程式碼外部行為的方式改變軟體系統的過程,同時改善其內部結構。這是一種有紀律的清理程式碼的方法,最小化引入錯誤的機會。實質上,當你進行重構時,你是在改善已編寫程式碼的設計。

「在編寫後改善設計。」這是一個奇怪的說法。在我們目前對軟體開發的理解中,我們認為我們先設計,然後再編碼。良好的設計是第一步,編碼是第二步。隨著時間的推移,程式碼將被修改,系統的完整性,即根據該設計的結構,逐漸消退。程式碼慢慢地從工程變成了黑客。

重構正好與這種做法相反。透過重構,你可以將不良設計,甚至混亂,重新加工成設計良好的程式碼。每一步都很簡單,甚至過於簡單。你將一個欄位從一個類別移動到另一個類別,將一些程式碼從一個方法中提取出來,變成自己的方法,並將一些程式碼向上或向下推進層級。然而,這些小變化的累積效果可以徹底改善設計。這正是軟體衰退的正常觀念的完全相反。

透過重構,你找到工作變更的平衡。你會發現設計並不是在一開始就發生,而是在開發過程中持續發生。你從構建系統中學會如何改善設計。最終的互動導致一個設計在開發過程中保持良好的程式。這本書中有什麼?

這本書是重構的指南;它是為專業程式設計師撰寫的。我的目標是向你展示如何以受控和高效的方式進行重構。你將學會以不引入錯誤的方式進行重構,而是有條不紊地改善結構。

傳統上,書籍以引言開始。雖然我同意這一原則,但我發現用一般性的討論或定義來介紹重構並不容易。因此,我從一個示例開始。第一章以一個有一些常見設計缺陷的小程式為例,將其重構為一個更可接受的物件導向程式。在這個過程中,我們看到重構的過程以及幾個有用的重構的應用。如果你想了解重構的真正意義,這是關鍵的一章。

在第二章中,我涵蓋了更多重構的一般原則,一些定義,以及進行重構的原因。我概述了一些重構的問題。在第三章中,肯特·貝克幫助我描述如何在程式碼中找到壞味道,以及如何用重構來清理它們。測試在重構中扮演著非常重要的角色,因此第四章描述了如何使用一個簡單的開源 Java 測試框架將測試構建到程式碼中。

本書的核心,重構目錄,從第五章延伸到第十二章。這絕不是一個全面的目錄。這是這樣一個目錄的開始。它包括了我在這個領域的工作中迄今為止所記錄的重構。當我想做某件事情,例如用多型取代條件式(255),目錄提醒我如何以安全、逐步的方式做到這一點。我希望這是你會經常回來查閱的書籍部分。

在這本書中,我描述了許多其他人所做的研究成果。最後幾章是這些人的客座章節。第十三章由比爾·奧普代克撰寫,他描述了在商業開發中採用重構所遇到的問題。第十四章由唐·羅伯茲和約翰·布蘭特撰寫,他們描述了重構的真正未來。