📄 codesmells.html
字号:
<head><title>Code Smells</title></head><body><h1><img src="logo.gif"> Code Smells</h1>A <em>code smell</em> is a hint that something has gone wrong somewhere in your code. Use the smell to track down the problem. <a href="KentBeck.html">KentBeck</a> seems to have coined the phrase in <a href="http://c2.com/cgi/wiki?OnceAndOnlyOnce">OnceAndOnlyOnce</a>.
<p>Highly experienced and knowledgeable developers have a "feel" for good design.
Having reached a state of "<a href="http://c2.com/cgi/wiki?UnconsciousCompetence">UnconsciousCompetence</a>," where they routinely practice good design without thinking about it too much, they find that they can look at a design or the code and immediately get a "feel" for its quality, without getting bogged down in extensive "logically detailed arguments".
<p><hr>
<p>Here are some <em>Code Smells</em> that tell you something needs to be changed. Please link them to Wiki pages with discussion.
<p><UL><li> Duplicated code -- see <a href="http://c2.com/cgi/wiki?OnceAndOnlyOnce">OnceAndOnlyOnce</a>
<li> Methods too big -- see <a href="http://c2.com/cgi/wiki?ComposedMethod">ComposedMethod</a>
<li> Classes with too many instance variables -- see ExtractComponent<a href="http://c2.com/cgi/wiki?edit=ExtractComponent">?</a>, <a href="http://c2.com/cgi/wiki?ValueObject">ValueObject</a>, and WholeValue<a href="http://c2.com/cgi/wiki?edit=WholeValue">?</a>
<li> Classes with too few instance variables
<li> Classes with too much code --see <a href="http://c2.com/cgi/wiki?OneResponsibilityRule">OneResponsibilityRule</a>
<li> Classes with too little code --see <a href="http://c2.com/cgi/wiki?OneResponsibilityRule">OneResponsibilityRule</a>
<li> Strikingly similar subclasses -- See <a href="http://c2.com/cgi/wiki?BehaviorToState">BehaviorToState</a>
<li> Different rates of change in the same object -- see <a href="http://c2.com/cgi/wiki?ChangeVelocity">ChangeVelocity</a>
<li> The same rate of change in different objects, particularly if they are disconnected
<li> A conceptually simple change that requires modification to code in many places -- see <a href="http://c2.com/cgi/wiki?RapeAndPasteProgramming">RapeAndPasteProgramming</a>
<li> <a href="http://c2.com/cgi/wiki?ParallelInheritanceHierarchies">ParallelInheritanceHierarchies</a> (a violation of <a href="http://c2.com/cgi/wiki?OnceAndOnlyOnce">OnceAndOnlyOnce</a>)
<li> An instance variable that is only set in some circumstances
<li> Asymmetry
<li> complex conditionals
<li> conditional code checking for variables or parameters equal to null
<li> Imbalance
<li> Many messages to the same object from the same method -- see <a href="http://c2.com/cgi/wiki?MoveMethod">MoveMethod</a>
<li> Methods with no messages to self (UtilityMethod<a href="http://c2.com/cgi/wiki?edit=UtilityMethod">?</a>)
<li> Messages to the results of messages (<a href="http://c2.com/cgi/wiki?LawOfDemeter">LawOfDemeter</a>)
<li> Methods requiring <a href="http://c2.com/cgi/wiki?SpecialFormatting">SpecialFormatting</a> to be readable
<li> Empty catch clauses
<li> <a href="http://c2.com/cgi/wiki?ArrowAntiPattern">ArrowAntiPattern</a> (nested if statements)
<li> <a href="http://c2.com/cgi/wiki?ExpensiveSetUpSmell">ExpensiveSetUpSmell</a>
<li> <a href="http://c2.com/cgi/wiki?BackPedalling">BackPedalling</a> (loss of context)
<li> <a href="http://c2.com/cgi/wiki?CeePreprocessorStatements">CeePreprocessorStatements</a>
<li> Long method names. Seriously: If you follow good naming standards, long method names are often an indication that the method is in the wrong class. e.g createWhateverFromWhateverOtherClass(OtherClass<a href="http://c2.com/cgi/wiki?edit=OtherClass">?</a> creator) vs creator.createWhatever(). See <a href="http://c2.com/cgi/wiki?UsingGoodNamingToDetectBadCode">UsingGoodNamingToDetectBadCode</a>.
<li> Explicitly setting variables to null. It's OK here and there, but more than a few indicates that either
<UL><li> there are references to things that this code has no business referencing, or
<li> the structure is so complex that the programmer doesn't really understand it and feels the need to do this to be safe.
<p></UL></UL>A large set of these seem to be covered in the <a href="http://c2.com/cgi/wiki?OneResponsibilityRule">OneResponsibilityRule</a>.
It's maybe one of the better principles from <a href="http://c2.com/cgi/wiki?BertrandMeyer">BertrandMeyer</a>, and seems to help
a lot in creating simple code (simple in fact, not just simple to write).
<p>Some Principles for judging whether code smells bad or good are defined in <a href="http://c2.com/cgi/wiki?PrinciplesOfObjectOrientedDesign">PrinciplesOfObjectOrientedDesign</a>.
<p>See Also: <a href="http://c2.com/cgi/wiki?SameNameDifferentMeaning">SameNameDifferentMeaning</a>, <a href="http://c2.com/cgi/wiki?VagueIdentifier">VagueIdentifier</a>, <a href="http://c2.com/cgi/wiki?TallerThanMe">TallerThanMe</a>, <a href="http://c2.com/cgi/wiki?ContrivedInterfaces">ContrivedInterfaces</a>, <a href="http://c2.com/cgi/wiki?ModelSmells">ModelSmells</a>
<hr>
<p><a href="CodeSmells.html">CodeSmells</a> referenced in:
<UL><li> <a href="http://c2.com/cgi/wiki?ChangeVelocity">ChangeVelocity</a> - <em>potential metric (of many) to drive refactoring: instance variables change at different rates.</em>
<li> <a href="http://c2.com/cgi/wiki?CodeSmellsIllustratedWithJavaAwt">CodeSmellsIllustratedWithJavaAwt</a> - example from Java AWT. [Sort of heuristic use of informal code metrics.]
<li> <a href="http://c2.com/cgi/wiki?MethodsShouldBePublic">MethodsShouldBePublic</a> - <a href="http://c2.com/cgi/wiki?MichaelFeathers">MichaelFeathers</a> suggests that having too many private or protected methods (or even variables) is bad.
<li> <a href="http://c2.com/cgi/wiki?OnceAndOnlyOnce">OnceAndOnlyOnce</a> - <a href="KentBeck.html">KentBeck</a>'s comment on code "wanting to be simple" <em>[...which should suggest <a href="RefactorMercilessly.html">RefactorMercilessly</a>.]</em>
<li> <a href="http://c2.com/cgi/wiki?RefactoringImprovingtheDesignofExistingCode">RefactoringImprovingtheDesignofExistingCode</a> - a book including <a href="KentBeck.html">KentBeck</a>'s comments on <a href="CodeSmells.html">CodeSmells</a> and refactoring.
<li> <a href="http://c2.com/cgi/wiki?SmallLint">SmallLint</a> - a <a href="http://c2.com/cgi/wiki?SmalltalkLanguage">SmalltalkLanguage</a> style checker, with some rules for "good" and "bad" constructs.
<li> <a href="http://c2.com/cgi/wiki?UnconsciousCompetence">UnconsciousCompetence</a> - and how an expert who has reached this stage of competence will make a "snap judgement" of code quality <strong>without</strong> resorting to detailed metric-by-metric computation.
<li> <a href="WikiPagesAboutRefactoring.html">WikiPagesAboutRefactoring</a> - related pages.
<li> <a href="http://c2.com/cgi/wiki?BeautyAintMyBusinessNoSir">BeautyAintMyBusinessNoSir</a> - <a href="KentBeck.html">KentBeck</a>'s comment on parallel inheritance hierarchies, [a minor example of code that smells].
<li> <a href="http://c2.com/cgi/wiki?BiggerRefactoringThoughts">BiggerRefactoringThoughts</a> - <em><a href="CodeSmells.html">CodeSmells</a> prompts <a href="RefactorMercilessly.html">RefactorMercilessly</a>, which often ends up rediscovering <a href="http://c2.com/cgi/wiki?DesignPatterns">DesignPatterns</a>.</em>
<li> <a href="http://c2.com/cgi/wiki?CodingCostModel">CodingCostModel</a> - <em>asking for an objective metric for "better" (IE: "not smells")</em>
<li> <a href="http://c2.com/cgi/wiki?DavesRealExampleWhereThinkingAheadWouldHaveHelped">DavesRealExampleWhereThinkingAheadWouldHaveHelped</a> - An argument over <a href="YouArentGonnaNeedIt.html">YouArentGonnaNeedIt</a>: After <a href="DoTheSimplestThingThatCouldPossiblyWork.html">DoTheSimplestThingThatCouldPossiblyWork</a> the <a href="CodeSmells.html">CodeSmells</a> because it violates the <a href="http://c2.com/cgi/wiki?OnceAndOnlyOnce">OnceAndOnlyOnce</a> rule.
<li> <a href="http://c2.com/cgi/wiki?ExtremeNormalFormDefined">ExtremeNormalFormDefined</a> - <em>another metric to drive refactoring?</em>
<li> <a href="http://c2.com/cgi/wiki?ExtremeNormalFormDefinitions">ExtremeNormalFormDefinitions</a> - <em>(same as <a href="http://c2.com/cgi/wiki?ExtremeNormalFormDefined">ExtremeNormalFormDefined</a>)</em>
<li> <a href="ExtremeReuse.html">ExtremeReuse</a> - reuse is good. But you may have to ignore <a href="CodeSmells.html">CodeSmells</a> in 3rd party library, unless you want maintenance and upgrade problems.
<li> <a href="ListenToTheCode.html">ListenToTheCode</a> - a similar concept to <a href="CodeSmells.html">CodeSmells</a>. <em>(...at least according to some people. ;-)</em>
<li> <a href="http://c2.com/cgi/wiki?PyWiki">PyWiki</a> - <a href="http://c2.com/cgi/wiki?TimVoght">TimVoght</a>'s experiences with refactoring.
<li> <a href="http://c2.com/cgi/wiki?TooMuchDocumentation">TooMuchDocumentation</a> - and an obscure comparison to <a href="LiterateProgramming.html">LiterateProgramming</a>.
<li> <a href="http://c2.com/cgi/wiki?XpProductivityMeasurementProblem">XpProductivityMeasurementProblem</a> - and does it, and refactoring, violate <a href="YouArentGonnaNeedIt.html">YouArentGonnaNeedIt</a>?
<li> <a href="http://c2.com/cgi/wiki?MyJavaStudents">MyJavaStudents</a> - <em>(...just a reading recommendation.)</em>
<li> <a href="http://c2.com/cgi/wiki?ValueObjectsShouldBePassedByValue">ValueObjectsShouldBePassedByValue</a> - ''a vague feeling of "wrongness."
<li> <a href="http://c2.com/cgi/wiki?YouArentGonnaNeedItAndYtwok">YouArentGonnaNeedItAndYtwok</a> - general comments on <a href="YouArentGonnaNeedIt.html">YouArentGonnaNeedIt</a>.
<p></UL><hr>
<hr>
<strong>Discussion:</strong>
<p>What ever happened to "Listen to what Smalltalk is telling you?"
<p><em>Same thing, isn't it? Just a different analogy: perhaps some of us smell out code problems; some see them; and some <a href="ListenToTheCode.html">ListenToTheCode</a>. </em>
<p>. . . . . .
<p>I have recently completed a Sensory Orientation Survey of 11,352 randomly-selected software developers, determining their primary sensory mode. 10,901 were visually-oriented, 430 hearing-oriented, 20 were touch-oriented, and one was smell-oriented. Some other interesting statistics were also gleaned, but I'm not sure how they are related. There were 386 visually-impaired developers, 19 vision-and-hearing-impaired developers, and one developer named Spot.
<p><em>I think I interviewed Spot today. </em>
<p>I think my major problem with the terminology is that it complicates critiques. It means one thing for one developer to say to another, "your code doesn't sound right," or, "your code sounds off-key," and quite a different thing to say, "your code smells." I wish the earlier auditory metaphor had stuck... -- <a href="http://c2.com/cgi/wiki?RussellGold">RussellGold</a>
<p>Most people are visual ("I see that"). Fewer are auditory ("I hear you"). Fewer still are tactile ("I feel that"). So few use smell as their primary sense that speaking of smells seems a bit alien to almost everyone. Perhaps that makes it a good choice.
<p><em>You see <strong>this</strong>, you hear <strong>that</strong>. But you go "<strong>what</strong> is that smell ?"</em>
<hr>
Code is too abstract to map well to visual metaphor, and not enough people have the ear for auditory metaphor, but [almost] everyone has a nose and can smell garbage or roses. -- Pete Hardie
<hr>
Everyone knows that BadCodeStinks<a href="http://c2.com/cgi/wiki?edit=BadCodeStinks">?</a>. Right? -- <a href="http://c2.com/cgi/wiki?ErikMeade">ErikMeade</a>
<hr><a href="http://c2.com/cgi/wiki?edit=CodeSmells">EditText</a> of this page (last edited February 9, 2001)<br><a href="http://c2.com/cgi/wiki?FindPage&value=CodeSmells">FindPage</a> by browsing or searching<p><font color=gray size=-1>This page mirrored in <a href="index.html">ExtremeProgrammingRoadmap</a> as of March 31, 2001</font></body>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -