📄 lib0129.html
字号:
<html>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<head>
<title>Top Refactoring Indicators</title>
<link rel="STYLESHEET" type="text/css" href="images/xpolecat.css">
<link rel="STYLESHEET" type="text/css" href="images/ie.content.css">
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0128.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0130.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
<br>
<div class="chapter">
<a name="ch20"></a>
<div class="section">
<h2 class="first-section-title"><a name="637"></a><a name="ch20lev1sec3"></a>Top Refactoring Indicators</h2><p class="first-para">
<b class="bold">Refactoring</b> is rewriting selected code to make it easier to understand and maintain. <a href="LiB0131.html#653" target="_parent" class="chapterjump">Fowler (2000)</a> provides an extensive list of conditions that indicate the need to refactor—conditions he calls "Bad Smells in Code." Although his list is so comprehensive I wouldn't presume to add to it, it is code centric. For readers who may not have the intimate understanding of code needed to apply Fowler's advice, I describe some observable symptoms that may indicate a need to refactor but don't require a full audit of the application's source.</p>
<p class="para">
<b class="bold">Classes that you can't change without inadvertently creating other bugs</b> <b class="bold">may need to be refactored.</b> This symptom is reminiscent of the movie <i class="emphasis">Night</i> <i class="emphasis">of the Living Dead.</i> Some programming bugs don't die, they just come back in different forms. Various circumstances can cause a bug to undergo such a metamorphosis.</p>
<p class="para">Sometimes this happens when code within a class behaves differently <a name="638"></a><a name="IDX-267"></a>depending on context. For example, I had one client that used a central API to provide reports for multiple applications. For political reasons, the API interpreted some of the argument values differently depending on which application was calling it (not a good idea, I know). Eventually, this service needed to be refactored because we couldn't change it without inadvertently causing bugs in some of the applications calling it.</p>
<p class="para">Sometimes bugs morph when code within a single class is doing too much and should be separated into multiple classes. For example, one application I worked on had to be able to accept data from multiple data sources. Some of the data sources were relational databases; some weren't. At first we had only two data sources. The programmer took a shortcut and put conditional logic in the class managing input to handle either data source. When we had to add data sources, the class had to be refactored.</p>
<p class="para">
<b class="bold">Enhancements or bug fixes requiring identical changes in multiple classes</b> <b class="bold">may indicate a need to refactor.</b> Some developers are almost too fond of copy-and-paste technology. In most cases, identical code in multiple classes should become common code "called" by multiple classes. Given a tight time frame, the developer who discovers a case of copied code might not have the time to make the code common. The architect or manager can assist by providing a mechanism to track these cases so they can be fixed when time permits.</p>
<p class="last-para">
<b class="bold">Abnormally complicated methods or classes that developers fear changing may need to be refactored.</b> Sometimes this symptom occurs in combination with the morphing-bug symptom described earlier. It is another indication that the code is too complex and needs to be refactored. Of course, the validity of this symptom depends on the assumption that developers are rational and their "fear" justified, which might not always be the case.</p>
</div>
</div><br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td><div STYLE="MARGIN-LEFT: 0.15in;"><a href="toc.html"><img src="images/teamlib.gif" width="62" height="15" border="0" align="absmiddle" alt="Team LiB"></a></div></td>
<td align="right"><div STYLE="MARGIN-LEFT: 0.15in;">
<a href="LiB0128.html"><img src="images/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="LiB0130.html"><img src="images/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</div></td></tr></table>
</body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -