SlideShare a Scribd company logo
2
Most read
12
Most read
23
Most read
Code Smell
Re-define refactoring at NAL (& code review)
Lan@NAL 201612
Once upon a time
We have “Code Review” on
own SDP (Software
Development Process), we
always said “do refactoring",
but ...
Once upon a time
We did not know own code
have bad smells, until ...
NOT BAD
Code Smell
Martin Fowler
https://martinfowler.com/bliki/CodeSmell.html
"A code smell is a surface indication
that usually corresponds to deeper
problem in the system."
In computer programming,
code smell is any symptom
in the source code of
program that possibly
indicates a deeper problem.
Code smells are usually not
bugs, they are not
technically incorrect and do
not currently prevent the
program from functioning.
Instead, they indicate
weaknesses in design
that may be slowing down
development or increasing
the risk of bugs or failures in
the future.
"A code smell is a hint that
something has gone wrong
somewhere in your code. Use the
smell to track down the problem."
Kent Beck
http://wiki.c2.com/?CodeSmell
Code Smells = Warning Signs
Code Smell ≠ A problem
Code Smell
“Bad code smells are symptons of poor design or
implementation choices”
[Martin Fowler]
● Pragmatic: code smells should be considered on a case by case basis
● Purist: all code smells should be avoided, no exceptions
Code Smell (taxonomy)
Smells Between Classes
Smells Within Classes
Object-orientation Abusers
Bloaters
Dispensables
Change Preventers
Couplers
Others
Encapsulation
Abstraction
Modularity
Hierarchy
Code Smell
Bad Smells:
● Bloaters
● Object-orientation Abusers
● Change Preventers
● Dispensables
● Couplers
Code Smell
Bloaters
Bloater smells represents something that has grown so large that it cannot be
effectively handled.
Long Method: A method contains too many lines of code. Generally, any
method longer than ten lines should make you start asking questions.
Large Class: A class contains many fields/methods/lines of code.
Long Parameter List: More than three or four parameters for a method.
Data Clumps: Sometimes different parts of the code contain identical groups
of variables (such as parameters for connecting to a database).
Primitive Obsession:
Use of primitives instead of small objects for simple tasks (such as
Code Smell
Object-orientation Abusers
All these smells are incomplete or incorrect application of object-
oriented programming principles.
Switch Statements: have a complex switch operator or
sequence of if statements.
Temporary Field: Temporary fields get their values (and thus
are needed by objects) only under certain circumstances.
Outside of these circumstances, they are empty.
Refused Bequest: If a subclass uses only some of the
methods and properties inherited from its parents, the
hierarchy is off-kilter. The unneeded methods may simply
go unused or be redefined and give off exceptions.
Alternative Classes with Different Interfaces: Two classes
perform identical functions but have different method
Code Smell
Change Preventers
These smells mean that if you need to change something in one place in your code,
you have to make many changes in other places too. Program development
becomes much more complicated and expensive as a result.
Divergent Change: You find yourself having to change many unrelated methods
when you make changes to a class. For example, when adding a new
product type you have to change the methods for finding, displaying, and
ordering products.
Shotgun Surgery: Making any modifications requires that you make many small
changes to many different classes.
Parallel Inheritance Hierarchies: Whenever you create a subclass for a class,
you find yourself needing to create a subclass for another class.
Code Smell
Dispensables
A dispensable is something pointless and unneeded whose absence
would make the code cleaner, more efficient and easier to understand.
Comments: A method is filled with explanatory comments.
Duplicate Code: Two code fragments look almost identical.
Lazy Class: Understanding and maintaining classes always costs time and money. So if a class
doesn't do enough to earn your attention, it should be deleted.
Data Class: A data class refers to a class that contains only fields and crude methods for accessing
them (getters and setters). These are simply containers for data used by other classes. These
classes do not contain any additional functionality and cannot independently operate on the
data that they own.
Dead Code: A variable, parameter, field, method or class is no longer used (usually because it is
obsolete).
Speculative Generality: There is an unused class, method, field or parameter.
Code Smell
Couplers
All the smells in this group contribute to excessive coupling between
classes or show what happens if coupling is replaced by excessive
delegation.
Feature Envy: A method accesses the data of another object more
than its own data.
Inappropriate Intimacy: One class uses the internal fields and
methods of another class.
Message Chains: In code you see a series of calls resembling $a->b()-
>c()->d()
Middle Man: If a class performs only one action, delegating work to
another class, why does it exist at all?
Code Smell
Others
Incomplete Library Class: Sooner or later, libraries stop meeting user
needs. The only solution to the problem – changing the library – is often
impossible since the library is read-only.
SO WHAT?!
Refactoring
In real world, WORKING WITH
CODE ...
20% Write new code
80% Modify existing code
Refactoring
● Unreadable Code …
● Duplicated Code …
● Complex Code …
…
Bad Smell Code is
HARD to MODIFY
Refactoring
Refactoring is ...
● Easier to Understand
● Cheaper to Modify
“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.”
Refactoring
Code Smells
Software Quality
Code Quality
READ
High Quality Code = easy to UNDERSTAND
CHANGE
In the Long Run, High Quality Code Costs less
Refactoring process ~ create Quality Code
Refactoring
Request Review
Review Comment
Refactoring
Repeat
Commit Code
Code Review
● Choose worst bad smell
● Select a refactoring
● Apply the refactoring
● Run all tests
Refactoring
Refactoring
“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.”
So…
HOWto do refactoring in a
controlled and efficient manner?!
More reading/explore
● Refactoring: Improving the Design of Existing Code
(by Martin Fowler, Kent Beck, John Brant, William
Opdyke, Don Roberts
● Refactoring Workbook (By William C. Wake)
● http://www.slideshare.net/mariosangiorgio/clean-
code-and-code-smells
● http://mikamantyla.eu/BadCodeSmellsTaxonomy.ht
ml

More Related Content

PDF
Code Smells and Its type (With Example)
PPTX
Presentation on software construction
PPTX
Code smells and remedies
PPTX
Extreme Programming
PDF
Kanban in 4 easy steps
PPT
Introduction to PowerShell
PDF
Introduction to Robot Framework
PPTX
upm ppt template_
Code Smells and Its type (With Example)
Presentation on software construction
Code smells and remedies
Extreme Programming
Kanban in 4 easy steps
Introduction to PowerShell
Introduction to Robot Framework
upm ppt template_

What's hot (20)

PPT
Refactoring Tips by Martin Fowler
PDF
Refactoring
PPTX
Code refactoring
PPTX
Refactoring and code smells
PDF
Code Refactoring
PPTX
PDF
Refactoring
PDF
Bad Code Smells
PPTX
Code Smell, Software Engineering
KEY
Clean code and Code Smells
PDF
PPTX
Solid principles
PPTX
SOLID principles
PPTX
Jsp with mvc
PPTX
The Art of Debugging.pptx
PPTX
clean code book summary - uncle bob - English version
ODP
Refactoring Techniques
PPT
SOLID Design Principles
PPTX
Software Development Process
PDF
Clean code
Refactoring Tips by Martin Fowler
Refactoring
Code refactoring
Refactoring and code smells
Code Refactoring
Refactoring
Bad Code Smells
Code Smell, Software Engineering
Clean code and Code Smells
Solid principles
SOLID principles
Jsp with mvc
The Art of Debugging.pptx
clean code book summary - uncle bob - English version
Refactoring Techniques
SOLID Design Principles
Software Development Process
Clean code
Ad

Viewers also liked (7)

PDF
Refactoring-ch7 moving feature btw objects
PDF
Code Smells
PPTX
Developing design sense of code smells
PPTX
Code Smells and How to avoid it
PPTX
Revisiting the Relationship Between Code Smells and Refactoring
PDF
PDF
Bad Smell In Codes 1
Refactoring-ch7 moving feature btw objects
Code Smells
Developing design sense of code smells
Code Smells and How to avoid it
Revisiting the Relationship Between Code Smells and Refactoring
Bad Smell In Codes 1
Ad

Similar to Code smell overview (20)

PDF
PDF
Code Smell and Refactoring
PPTX
Refactoring
DOCX
A Study on Code Smell Detection with Refactoring Tools in Object Oriented Lan...
PPTX
Refactoring, 2nd Edition
PPS
Few minutes To better Code - Refactoring
PPTX
Code quality
PPTX
Code smell & refactoring
ODP
Documenting Code - Patterns and Anti-patterns - NLPW 2016
PPT
Code Review
PDF
Refactoring 2 The Max
PPTX
Agile korea 2013 유석문
PDF
YAGNI Principle and Clean Code
PPTX
Software Craftsmanship - Code Smells - Dispensables
PPTX
Code Smells Part 1: Basic Smells
PDF
Reduce Reuse Refactor
ODP
Documenting code yapceu2016
PPTX
Code smells and Other Malodorous Software Odors
PPTX
Code review
PPT
Revisiting Refactoring
Code Smell and Refactoring
Refactoring
A Study on Code Smell Detection with Refactoring Tools in Object Oriented Lan...
Refactoring, 2nd Edition
Few minutes To better Code - Refactoring
Code quality
Code smell & refactoring
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Code Review
Refactoring 2 The Max
Agile korea 2013 유석문
YAGNI Principle and Clean Code
Software Craftsmanship - Code Smells - Dispensables
Code Smells Part 1: Basic Smells
Reduce Reuse Refactor
Documenting code yapceu2016
Code smells and Other Malodorous Software Odors
Code review
Revisiting Refactoring

More from Pham Manh Lan (6)

PPTX
Chăm sóc Khách Hàng trong tổ chức Agile
PPTX
Startup & marketing
PPTX
Customer care zero2one
PPTX
Agile and what's next
PPTX
Chất lượng ở NAL
PPTX
Nal Group - Hành trình Agile
Chăm sóc Khách Hàng trong tổ chức Agile
Startup & marketing
Customer care zero2one
Agile and what's next
Chất lượng ở NAL
Nal Group - Hành trình Agile

Recently uploaded (20)

PPTX
UNIT 4 Total Quality Management .pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
DOCX
573137875-Attendance-Management-System-original
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
Construction Project Organization Group 2.pptx
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
OOP with Java - Java Introduction (Basics)
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
Sustainable Sites - Green Building Construction
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
Well-logging-methods_new................
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Digital Logic Computer Design lecture notes
UNIT 4 Total Quality Management .pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
573137875-Attendance-Management-System-original
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Construction Project Organization Group 2.pptx
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
OOP with Java - Java Introduction (Basics)
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
CYBER-CRIMES AND SECURITY A guide to understanding
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Sustainable Sites - Green Building Construction
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
Well-logging-methods_new................
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Model Code of Practice - Construction Work - 21102022 .pdf
Digital Logic Computer Design lecture notes

Code smell overview

  • 1. Code Smell Re-define refactoring at NAL (& code review) Lan@NAL 201612
  • 2. Once upon a time We have “Code Review” on own SDP (Software Development Process), we always said “do refactoring", but ...
  • 3. Once upon a time We did not know own code have bad smells, until ...
  • 5. Code Smell Martin Fowler https://martinfowler.com/bliki/CodeSmell.html "A code smell is a surface indication that usually corresponds to deeper problem in the system." In computer programming, code smell is any symptom in the source code of program that possibly indicates a deeper problem. Code smells are usually not bugs, they are not technically incorrect and do not currently prevent the program from functioning. Instead, they indicate weaknesses in design that may be slowing down development or increasing the risk of bugs or failures in the future. "A code smell is a hint that something has gone wrong somewhere in your code. Use the smell to track down the problem." Kent Beck http://wiki.c2.com/?CodeSmell Code Smells = Warning Signs Code Smell ≠ A problem
  • 6. Code Smell “Bad code smells are symptons of poor design or implementation choices” [Martin Fowler] ● Pragmatic: code smells should be considered on a case by case basis ● Purist: all code smells should be avoided, no exceptions
  • 7. Code Smell (taxonomy) Smells Between Classes Smells Within Classes Object-orientation Abusers Bloaters Dispensables Change Preventers Couplers Others Encapsulation Abstraction Modularity Hierarchy
  • 8. Code Smell Bad Smells: ● Bloaters ● Object-orientation Abusers ● Change Preventers ● Dispensables ● Couplers
  • 9. Code Smell Bloaters Bloater smells represents something that has grown so large that it cannot be effectively handled. Long Method: A method contains too many lines of code. Generally, any method longer than ten lines should make you start asking questions. Large Class: A class contains many fields/methods/lines of code. Long Parameter List: More than three or four parameters for a method. Data Clumps: Sometimes different parts of the code contain identical groups of variables (such as parameters for connecting to a database). Primitive Obsession: Use of primitives instead of small objects for simple tasks (such as
  • 10. Code Smell Object-orientation Abusers All these smells are incomplete or incorrect application of object- oriented programming principles. Switch Statements: have a complex switch operator or sequence of if statements. Temporary Field: Temporary fields get their values (and thus are needed by objects) only under certain circumstances. Outside of these circumstances, they are empty. Refused Bequest: If a subclass uses only some of the methods and properties inherited from its parents, the hierarchy is off-kilter. The unneeded methods may simply go unused or be redefined and give off exceptions. Alternative Classes with Different Interfaces: Two classes perform identical functions but have different method
  • 11. Code Smell Change Preventers These smells mean that if you need to change something in one place in your code, you have to make many changes in other places too. Program development becomes much more complicated and expensive as a result. Divergent Change: You find yourself having to change many unrelated methods when you make changes to a class. For example, when adding a new product type you have to change the methods for finding, displaying, and ordering products. Shotgun Surgery: Making any modifications requires that you make many small changes to many different classes. Parallel Inheritance Hierarchies: Whenever you create a subclass for a class, you find yourself needing to create a subclass for another class.
  • 12. Code Smell Dispensables A dispensable is something pointless and unneeded whose absence would make the code cleaner, more efficient and easier to understand. Comments: A method is filled with explanatory comments. Duplicate Code: Two code fragments look almost identical. Lazy Class: Understanding and maintaining classes always costs time and money. So if a class doesn't do enough to earn your attention, it should be deleted. Data Class: A data class refers to a class that contains only fields and crude methods for accessing them (getters and setters). These are simply containers for data used by other classes. These classes do not contain any additional functionality and cannot independently operate on the data that they own. Dead Code: A variable, parameter, field, method or class is no longer used (usually because it is obsolete). Speculative Generality: There is an unused class, method, field or parameter.
  • 13. Code Smell Couplers All the smells in this group contribute to excessive coupling between classes or show what happens if coupling is replaced by excessive delegation. Feature Envy: A method accesses the data of another object more than its own data. Inappropriate Intimacy: One class uses the internal fields and methods of another class. Message Chains: In code you see a series of calls resembling $a->b()- >c()->d() Middle Man: If a class performs only one action, delegating work to another class, why does it exist at all?
  • 14. Code Smell Others Incomplete Library Class: Sooner or later, libraries stop meeting user needs. The only solution to the problem – changing the library – is often impossible since the library is read-only.
  • 16. Refactoring In real world, WORKING WITH CODE ... 20% Write new code 80% Modify existing code
  • 17. Refactoring ● Unreadable Code … ● Duplicated Code … ● Complex Code … … Bad Smell Code is HARD to MODIFY
  • 18. Refactoring Refactoring is ... ● Easier to Understand ● Cheaper to Modify “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.”
  • 19. Refactoring Code Smells Software Quality Code Quality READ High Quality Code = easy to UNDERSTAND CHANGE In the Long Run, High Quality Code Costs less Refactoring process ~ create Quality Code
  • 20. Refactoring Request Review Review Comment Refactoring Repeat Commit Code Code Review ● Choose worst bad smell ● Select a refactoring ● Apply the refactoring ● Run all tests
  • 22. Refactoring “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.” So… HOWto do refactoring in a controlled and efficient manner?!
  • 23. More reading/explore ● Refactoring: Improving the Design of Existing Code (by Martin Fowler, Kent Beck, John Brant, William Opdyke, Don Roberts ● Refactoring Workbook (By William C. Wake) ● http://www.slideshare.net/mariosangiorgio/clean- code-and-code-smells ● http://mikamantyla.eu/BadCodeSmellsTaxonomy.ht ml

Editor's Notes

  • #2: Nhìn lại hoạt động coding review & refactoring (tái định nghĩa trong NAL)
  • #3: Chúng ta có Coding Review trong quy trình, có nói về Refactoring ... nhưng có lẽ không hiểu mục đích và cách làm các công việc này giống nhau.
  • #4: Chúng ta không biết là Code của mình THỐI, cho đến khi khách hàng nói code của chúng ta thối.
  • #8: Bloaters Bloater smells represents something that has grown so large that it cannot be effectively handled. Bloaters are code, methods and classes that have increased to such gargantuan proportions that they are hard to work with. Usually these smells do not crop up right away, rather they accumulate over time as the program evolves (and especially when nobody makes an effort to eradicate them). Object-Orientation Abusers All these smells are incomplete or incorrect application of object-oriented programming principles. Change Preventers These smells mean that if you need to change something in one place in your code, you have to make many changes in other places too. Program development becomes much more complicated and expensive as a result. Dispensables A dispensable is something pointless and unneeded whose absence would make the code cleaner, more efficient and easier to understand. Couplers All the smells in this group contribute to excessive coupling between classes or show what happens if coupling is replaced by excessive delegation. --------- Abstraction: “An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of objects and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.” Encapsulation: “Encapsulation is the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation.” Modularity: “Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules Hierarchy: “Hierarchy is a ranking or ordering of abstractions.”
  • #15: Incomplete Library Class Sooner or later, libraries stop meeting user needs. The only solution to the problem – changing the library – is often impossible since the library is read-only.