Code Smells
   HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, a code smell is any characteristic in the
source code In computing, source code, or simply code, is any collection of code, with or without comments, written using a human-readable programming language, usually as plain text. The source code of a program is specially designed to facilitate the wo ...
of a
program Program, programme, programmer, or programming may refer to: Business and management * Program management, the process of managing several related projects * Time management * Program, a part of planning Arts and entertainment Audio * Progra ...
that possibly indicates a deeper problem. Determining what is and is not a code smell is subjective, and varies by language, developer, and development methodology. The term was popularised by
Kent Beck Kent Beck (born 1961) is an American software engineer and the creator of extreme programming, a software development methodology that eschews rigid formal specification for a collaborative and iterative design process. Beck was one of the 17 ori ...
on WardsWiki in the late 1990s. Usage of the term increased after it was featured in the 1999 book ''Refactoring: Improving the Design of Existing Code'' by Martin Fowler. It is also a term used by agile programmers.


Definition

One way to look at smells is with respect to principles and quality: "Smells are certain structures in the code that indicate violation of fundamental design principles and negatively impact design quality". Code smells are usually not bugs; they are not technically incorrect and do not prevent the program from functioning. Instead, they indicate weaknesses in design that may slow down development or increase the risk of bugs or failures in the future. Bad code smells can be an indicator of factors that contribute to
technical debt In software development, technical debt (also known as design debt or code debt) is the implied cost of additional rework caused by choosing an easy (limited) solution now instead of using a better approach that would take longer. Analogous with ...
.
Robert C. Martin Robert Cecil Martin (born 5 December 1952), colloquially called "Uncle Bob", is an American software engineer, instructor, and best-selling author. He is most recognized for developing many software design principles and for being a founder of t ...
calls a list of code smells a "value system" for software craftsmanship. Often the deeper problem hinted at by a code smell can be uncovered when the code is subjected to a short feedback cycle, where it is
refactored In computer programming and software design, code refactoring is the process of restructuring existing computer code—changing the '' factoring''—without changing its external behavior. Refactoring is intended to improve the design, structur ...
in small, controlled steps, and the resulting design is examined to see if there are any further code smells that in turn indicate the need for more refactoring. From the point of view of a programmer charged with performing refactoring, code smells are
heuristics A heuristic (; ), or heuristic technique, is any approach to problem solving or self-discovery that employs a practical method that is not guaranteed to be optimal, perfect, or rational, but is nevertheless sufficient for reaching an immediate, ...
to indicate when to refactor, and what specific refactoring techniques to use. Thus, a code smell is a driver for refactoring. A 2015 study utilizing automated analysis for half a million source code commits and the manual examination of 9,164 commits determined to exhibit "code smells" found that: * There exists empirical evidence for the consequences of "technical debt", but there exists only anecdotal evidence as to ''how'', ''when'', or ''why'' this occurs. * Common wisdom suggests that urgent maintenance activities and pressure to deliver features while prioritizing time-to-market over code quality are often the causes of such smells. Tools such as
Checkstyle Checkstyle is a static code analysis tool used in software development for checking if Java source code is compliant with specified coding rules. Originally developed by Oliver Burn back in 2001, the project is maintained by a team of developer ...
, PMD,
FindBugs FindBugs is an open-source static code analyser created by Bill Pugh and David Hovemeyer which detects possible bugs in Java programs. Potential errors are classified in four ranks: (i) scariest, (ii) scary, (iii) troubling and (iv) of concern ...
, and
SonarQube SonarQube (formerly Sonar) is an open-source platform developed by SonarSource for continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs and code smells on 29 programming languages. S ...
can automatically identify code smells.


Common code smells


Application-level smells

* '' Mysterious Name'': functions, modules, variables or classes that are named in a way that does not communicate what they do or how to use them. * '' Duplicated code'': identical or very similar code that exists in more than one location. * ''Contrived complexity'': forced usage of overcomplicated
design patterns ''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a foreword ...
where simpler design patterns would suffice. * ''
Shotgun surgery Shotgun surgery is an antipattern in software development and occurs where a developer adds features to an application codebase which span a multiplicity of implementors or implementations in a single change. This is common practice in many pr ...
'': a single change that needs to be applied to multiple classes at the same time. * ''Uncontrolled side effects'': side effects of coding that commonly cause runtime exceptions, with unit tests unable to capture the exact cause of the problem. * ''Variable mutations'': mutations that vary widely enough that refactoring the code becomes increasingly difficult, due to the actual value's status as unpredictable and hard to reason about. * ''Boolean blindness'': easy to assert on the opposite value and still type checks.


Class-level smells

* '' Large class'': a
class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differentl ...
that contains too many types or contains many unrelated methods * ''Feature envy'': a class that uses methods of another class excessively. * '' Inappropriate intimacy'': a class that has dependencies on implementation details of another class * '' Refused bequest'': a class that
override Override may refer to: * Dr. Gregory Herd, a Marvel Comics character formerly named Override * Manual override, a function where an automated system is placed under manual control * Method overriding, a subclassing feature in Object Oriented progr ...
s a method of a base class in such a way that the
contract A contract is a legally enforceable agreement between two or more parties that creates, defines, and governs mutual rights and obligations between them. A contract typically involves the transfer of goods, services, money, or a promise to tran ...
of the
base class In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object ( prototype-based inheritance) or class ( class-based inheritance), retaining similar implementation. Also defined as deriving new classe ...
is not honored by the
derived class In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object ( prototype-based inheritance) or class ( class-based inheritance), retaining similar implementation. Also defined as deriving new classe ...
* ''Lazy class/freeloader'': a class that does too little. * ''Excessive use of literals'': these should be coded as named constants, to improve readability and to avoid programming errors. Additionally, literals can and should be externalized into resource files/scripts, or other data stores such as databases where possible, to facilitate localization of software if it is intended to be deployed in different regions. * ''
Cyclomatic complexity Cyclomatic complexity is a software metric used to indicate the complexity of a program. It is a quantitative measure of the number of linearly independent paths through a program's source code. It was developed by Thomas J. McCabe, Sr. in 1976 ...
'': too many branches or loops; this may indicate a function needs to be broken up into smaller functions, or that it has potential for simplification/refactoring. * ''
Downcasting In class-based programming, downcasting or type refinement is the act of casting a reference of a base class to one of its derived classes. In many programming languages, it is possible to check through type introspection to determine whether the ...
'': a type cast which breaks the abstraction model; the abstraction may have to be refactored or eliminated. * ''Orphan variable or constant class'': a
class Class or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used differentl ...
that typically has a collection of constants which belong elsewhere where those constants should be owned by one of the other member classes. * '' Data clump'': Occurs when a group of variables are passed around together in various parts of the program. In general, this suggests that it would be more appropriate to formally group the different variables together into a single object, and pass around only the new object instead.


Method-level smells

* ''Too many parameters'': a long list of parameters is hard to read, and makes calling and testing the function complicated. It may indicate that the purpose of the function is ill-conceived and that the code should be refactored so responsibility is assigned in a more clean-cut way. * ''Long method'': a
method Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
, function, or procedure that has grown too large. * ''Excessively long identifiers'': in particular, the use of
naming conventions A naming convention is a convention (generally agreed scheme) for naming things. Conventions differ in their intents, which may include to: * Allow useful information to be deduced from the names based on regularities. For instance, in Manhatta ...
to provide disambiguation that should be implicit in the
software architecture Software architecture is the fundamental structure of a software system and the discipline of creating such structures and systems. Each structure comprises software elements, relations among them, and properties of both elements and relations. ...
. * ''Excessively short identifiers'': the name of a variable should reflect its function unless the function is obvious. * ''Excessive return of data'': a function or method that returns more than what each of its callers needs. * ''Excessive comments'': a class, function or method has irrelevant or trivial comments. A comment on an attribute setter/getter is a good example. * ''Excessively long line of code'' (or God Line): A line of code which is too long, making the code difficult to read, understand, debug, refactor, or even identify possibilities of software reuse.


See also

*
Anti-pattern An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
*
Design smell In computer programming, design smells are "structures in the design that indicate violation of fundamental design principles and negatively impact design quality".Girish Suryanarayana, Ganesh SG, Tushar Sharma (2014). "Refactoring for software des ...
*
List of tools for static code analysis This is a list of notable tools for static program analysis (program analysis is a synonym for code analysis). Static code analysis tools Languages Ada * * * * * * * * * * * C, C++ * * * * * * * * * * * * ...
*
Software rot Software rot (bit rot, code rot, software erosion, software decay, or software entropy) is either a slow deterioration of software quality over time or its diminishing responsiveness that will eventually lead to software becoming faulty, unusabl ...


References


Further reading

* *


External links

* {{Cite web , title=CodeSmell , url=https://martinfowler.com/bliki/CodeSmell.html , access-date=2022-03-01 , website=martinfowler.com * Boundy, David
Software cancer: the seven early warning signs
o
here
ACM SIGSOFT Software Engineering Notes, Vol. 18 No. 2 (April 1993), Association for Computing Machinery, New York, NY, USA Anti-patterns Computer programming folklore Software engineering folklore