Professional Documents
Culture Documents
03 Software AntiPatterns
03 Software AntiPatterns
2
Quality seen by the customer
3
Software errors, faults and failures
Not all software errors become software faults. in some cases, the
software error can cause improper functioning of the software. In
many other cases, erroneous code lines will not affect the functionality
of the software as a whole.
4
Causes of software failures
© Aditya P. Mathur 6
2009
OO paradigm
?
Design defects
• Refactoring
– The process of improving a code after it has been written by
changing its internal structure without changing the external
behavior (Fowler et al., ‘99)
– Examples: Move method, extract class, move attribute...
• Refactoring steps
– Detection of code fragments to improve (e.g., defects)
– Identification of refactoring operations
12
Detection strategy
• Initialization: Constructors
• External interface: Libraries, Inheritance,
Interfaces etc.
• Internal Implementation: Class own methods,
polymorphic functions.
God Class
• 453 methods
• 114 attributes
• over 3500 LOC
• all methods
and all
attributes are
static
Design defects
Data Class
Property
Design defects
Refused Parent Bequest
Modeller Class
Design defects
Feature Envy
ClassDiagramLayouter
Design defects
Shotgun Surgery
Refactoring
Move method
Extract class
Move field
?
Add association
…
7/4/2022 29
Test Smells
• Fix:
– Remove unneeded assertions
– Add Assertion Explanation
Assertion Roulette
• Fix:
– Apply Extract Method refactoring to split the test
method in order to specialize its responsibilities.
Eager Test
• Fix:
– Setup External Resource operation is suggested,
no definitive solution for this smell.
Mystery Guest
• Fix:
– The application of an Extract Method to break
down the testing logic into separate methods.
Conditional Test Logic
• Test methods need to be simple and execute all
statements in the production method.
• Conditions within the test method will alter the
behavior of the test and its expected output, and
would lead to situations where the test fails to
detect defects in the production method since
test statements were not executed as a condition
was not met.
• Deep nested Conditional code within a test
method negatively impacts the ease of
comprehension.
Constructor Initialization
• Fix:
– Use setup instead of the constructor when
applying any initialization.
Constructor Initialization
• Fix:
– Use proper naming convention and use the
default test file as a template and not as one of
the main testing files.
Default Test
• By default some IDEs create default test classes
when a project is created. These classes are
meant to serve as an example for developers
when wring unit tests and should either be
removed or renamed.
• Having such files in the project will cause
developers to start adding test methods into
these files, making the default test class a
container of all test cases.
• This also would possibly cause problems when
the classes need to be renamed in the future.
Empty Test
• Fix:
– Remove all empty tests.
Empty Test
• Occurs when a test method does not contain
executable statements. Such methods are possibly
created for debugging purposes and then forgotten
about or contains commented out code.
• An empty test can be considered problematic and
more dangerous than not having a test case at all since
JUnit will indicate that the test passes even if there are
no executable statements present in the method body.
• As such, developers introducing behavior breaking
changes into production class, will not be notified of
the alternated outcomes as JUnit will report the test as
passing.
Unknown Test
• Fix:
– Change the testing logic to use asserts.
Unknown Test
• An assertion statement is used to declare an
expected Boolean condition for a test method. By
examining the assertion statement it is possible
to understand the purpose of the test method.
• However, It is possible for a test method to
written without an assertion statement, in such
an instance JUnit will show the test method as
passing if the statements within the test method
did not result in an exception, when executed.
• Difficult in understanding the purpose of such
test methods (more so if the name of the test
method is not descriptive enough).
Design defects
•Mystery Guest
•Resource Optimism
•Test Run War
•General Fixture
•Eager Test
•Lazy Test
•Assertion Roulette
•Indirect Testing
•For Testers Only
•Sensitive Equlity
•Test Code Duplication
Defects summary by Fowler (2002)
1. Duplicated code: means that the same code structure appears in more than one place.
2. Long method: is a method that is too long, so it is difficult to understand, change, or extend.
3. Large class: means that a class is trying to do too much. These classes have too many instances, variables or methods.
4. God class: usually called also design flaw, refer to class that tends to centralize the intelligence of the system.
5. Divergent Change: Adding a small feature to the code requires updating more than one class or method, Extract class solve this by putting them together.
6. Shotgun surgery: Similar to Divergent Change but in opposite way, changing a small feature requires more small changes that are hard to find.
7. Long parameter list: a long list of parameters in a procedure or function make readability and code quality worse.
8. Feature envy: A method is more interested in other classes than the one where it is located. It is in the wrong place since it is more tightly coupled to the other classes.
9. Contrived complexity: forced usage of overly complicated design pattern where simpler design would suffice.
10. Complex conditionals: branches that check lots of unrelated conditions and edge cases that don't seem to capture the meaning of a block of code.
11. Primitive obsession: the smell represents a case where primitives are used instead of small classes.
12. Switch statement: the smell means a case where type codes or runtime class type detection are used instead of polymorphism.
13. Data clumps: the smell means that software has data items that often appear together.
14. Temporary fields: the smell means that class has a variable which is only used in some situation.
15. Refused bequest: the smell means that a child class does not fully support all the methods or data it inherits.
16. Lazy class: a class that is doing nothing enough and should be removed.
17. Data class: a class that contain data, but hardly any logic for it.
18. Middle man: it means that a class is delegating most of its tasks to subsequent classes.
19. Divergent change: the smell means that one class need to be continuously changed for different reasons.
20. Parallel Inheritance Hierarchies: Special case of shotgun surgery, The creation of a subclass of one class leads to creation of a subclass of another class.
21. Speculative Generality: A code which is too general: Long hierarchies with many abstract classes and many delegations.
22. Message Chains: When called action creates a chain of objects that communicate to get to the last one to respond to the requested action.
Defects summary by Mantaya (2003)
Defects summary by Marienscu (2003)
Defects summary by Marienscu (2006)
Moha’s smells classification (2009)
Android Smells by Reimann (2014)
•Data Transmission Without •Rigid Alarm Manager
Compression •Slow Loop
•Debuggable Release •Unclosed Closable
•Durable Wakelock •Etc. (15 more).
•Inefficient Data Format and
Parser
•Inefficient Data Structure
•Inefficient SQL Query
•Internal Getter and Setter
•Leaking Innder Class
•Leaking Thread
•Member Ignoring Method
•No low Memory Resolver
•Public Data
Summary
Richard Soley - CEO - Object Management Group – About Standardizing
defects metrics:
http://www.youtube.com/watch?v=bMYv6FYVOVA#t=104
56