SlideShare uma empresa Scribd logo
1 de 23
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

Mais conteúdo relacionado

Mais procurados

Clean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a MonolithClean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a MonolithVictor Rentea
 
clean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionclean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionsaber tabatabaee
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerIgor Crvenov
 
Clean architecture - Protecting the Domain
Clean architecture - Protecting the DomainClean architecture - Protecting the Domain
Clean architecture - Protecting the DomainVictor Rentea
 
Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean ArchitectureRoc Boronat
 
Code Smell, Software Engineering
Code Smell, Software EngineeringCode Smell, Software Engineering
Code Smell, Software EngineeringAnshul Vinayak
 
The Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignThe Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignVictor Rentea
 
Clean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software CraftsmanshipClean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software CraftsmanshipIvan Paulovich
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smellskim.mens
 
The Art of Clean code
The Art of Clean codeThe Art of Clean code
The Art of Clean codeVictor Rentea
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelinesAnkur Goyal
 

Mais procurados (20)

Clean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a MonolithClean Pragmatic Architecture - Avoiding a Monolith
Clean Pragmatic Architecture - Avoiding a Monolith
 
clean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionclean code book summary - uncle bob - English version
clean code book summary - uncle bob - English version
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 
Clean architecture - Protecting the Domain
Clean architecture - Protecting the DomainClean architecture - Protecting the Domain
Clean architecture - Protecting the Domain
 
Clean code
Clean codeClean code
Clean code
 
Clean code
Clean codeClean code
Clean code
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean code
Clean codeClean code
Clean code
 
Introducing Clean Architecture
Introducing Clean ArchitectureIntroducing Clean Architecture
Introducing Clean Architecture
 
Code Smell, Software Engineering
Code Smell, Software EngineeringCode Smell, Software Engineering
Code Smell, Software Engineering
 
The Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignThe Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable Design
 
Clean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software CraftsmanshipClean Architecture Essentials - Stockholm Software Craftsmanship
Clean Architecture Essentials - Stockholm Software Craftsmanship
 
Clean code
Clean code Clean code
Clean code
 
Bad Code Smells
Bad Code SmellsBad Code Smells
Bad Code Smells
 
Tech talks#6: Code Refactoring
Tech talks#6: Code RefactoringTech talks#6: Code Refactoring
Tech talks#6: Code Refactoring
 
Refactoring
RefactoringRefactoring
Refactoring
 
Clean Code
Clean CodeClean Code
Clean Code
 
The Art of Clean code
The Art of Clean codeThe Art of Clean code
The Art of Clean code
 
Onion architecture
Onion architectureOnion architecture
Onion architecture
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelines
 

Destaque

Refactoring-ch7 moving feature btw objects
Refactoring-ch7 moving feature btw objectsRefactoring-ch7 moving feature btw objects
Refactoring-ch7 moving feature btw objectsfungfung Chen
 
Developing design sense of code smells
Developing design sense of code smellsDeveloping design sense of code smells
Developing design sense of code smellsLlewellyn Falco
 
Code Smells and How to avoid it
Code Smells and How to avoid itCode Smells and How to avoid it
Code Smells and How to avoid itSyed Shah
 
Revisiting the Relationship Between Code Smells and Refactoring
Revisiting the Relationship Between Code Smells and RefactoringRevisiting the Relationship Between Code Smells and Refactoring
Revisiting the Relationship Between Code Smells and RefactoringNorihiro Yoshida
 

Destaque (7)

Refactoring-ch7 moving feature btw objects
Refactoring-ch7 moving feature btw objectsRefactoring-ch7 moving feature btw objects
Refactoring-ch7 moving feature btw objects
 
Code Smells
Code SmellsCode Smells
Code Smells
 
Developing design sense of code smells
Developing design sense of code smellsDeveloping design sense of code smells
Developing design sense of code smells
 
Code Smells and How to avoid it
Code Smells and How to avoid itCode Smells and How to avoid it
Code Smells and How to avoid it
 
Revisiting the Relationship Between Code Smells and Refactoring
Revisiting the Relationship Between Code Smells and RefactoringRevisiting the Relationship Between Code Smells and Refactoring
Revisiting the Relationship Between Code Smells and Refactoring
 
Code smells
Code smellsCode smells
Code smells
 
Bad Smell In Codes 1
Bad Smell In Codes 1Bad Smell In Codes 1
Bad Smell In Codes 1
 

Semelhante a Code smell overview

Code Smell and Refactoring
Code Smell and RefactoringCode Smell and Refactoring
Code Smell and Refactoringkimsrung lov
 
A Study on Code Smell Detection with Refactoring Tools in Object Oriented Lan...
A Study on Code Smell Detection with Refactoring Tools in Object Oriented Lan...A Study on Code Smell Detection with Refactoring Tools in Object Oriented Lan...
A Study on Code Smell Detection with Refactoring Tools in Object Oriented Lan...ijcnes
 
Refactoring, 2nd Edition
Refactoring, 2nd EditionRefactoring, 2nd Edition
Refactoring, 2nd Editionjexp
 
Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - RefactoringDiaa Al-Salehi
 
Code smell & refactoring
Code smell & refactoringCode smell & refactoring
Code smell & refactoringMamata Gelanee
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Søren Lund
 
Code Review
Code ReviewCode Review
Code ReviewRavi Raj
 
Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문Sangcheol Hwang
 
YAGNI Principle and Clean Code
YAGNI Principle and Clean CodeYAGNI Principle and Clean Code
YAGNI Principle and Clean CodeLuan Reffatti
 
Software Craftsmanship - Code Smells - Dispensables
Software Craftsmanship - Code Smells - DispensablesSoftware Craftsmanship - Code Smells - Dispensables
Software Craftsmanship - Code Smells - DispensablesRajat Singla
 
Code Smells Part 1: Basic Smells
Code Smells Part 1:  Basic SmellsCode Smells Part 1:  Basic Smells
Code Smells Part 1: Basic SmellsNancy Henson
 
Documenting code yapceu2016
Documenting code yapceu2016Documenting code yapceu2016
Documenting code yapceu2016Søren Lund
 
Code smells and Other Malodorous Software Odors
Code smells and Other Malodorous Software OdorsCode smells and Other Malodorous Software Odors
Code smells and Other Malodorous Software OdorsClint Edmonson
 
Revisiting Refactoring
Revisiting RefactoringRevisiting Refactoring
Revisiting RefactoringNick Harrison
 

Semelhante a Code smell overview (20)

Refactoring
RefactoringRefactoring
Refactoring
 
Code Smell and Refactoring
Code Smell and RefactoringCode Smell and Refactoring
Code Smell and Refactoring
 
Refactoring
RefactoringRefactoring
Refactoring
 
A Study on Code Smell Detection with Refactoring Tools in Object Oriented Lan...
A Study on Code Smell Detection with Refactoring Tools in Object Oriented Lan...A Study on Code Smell Detection with Refactoring Tools in Object Oriented Lan...
A Study on Code Smell Detection with Refactoring Tools in Object Oriented Lan...
 
Refactoring, 2nd Edition
Refactoring, 2nd EditionRefactoring, 2nd Edition
Refactoring, 2nd Edition
 
Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - Refactoring
 
Code quality
Code quality Code quality
Code quality
 
Code smell & refactoring
Code smell & refactoringCode smell & refactoring
Code smell & refactoring
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016
 
Code Review
Code ReviewCode Review
Code Review
 
Refactoring 2 The Max
Refactoring 2 The MaxRefactoring 2 The Max
Refactoring 2 The Max
 
Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문
 
YAGNI Principle and Clean Code
YAGNI Principle and Clean CodeYAGNI Principle and Clean Code
YAGNI Principle and Clean Code
 
Software Craftsmanship - Code Smells - Dispensables
Software Craftsmanship - Code Smells - DispensablesSoftware Craftsmanship - Code Smells - Dispensables
Software Craftsmanship - Code Smells - Dispensables
 
Code Smells Part 1: Basic Smells
Code Smells Part 1:  Basic SmellsCode Smells Part 1:  Basic Smells
Code Smells Part 1: Basic Smells
 
Reduce Reuse Refactor
Reduce Reuse RefactorReduce Reuse Refactor
Reduce Reuse Refactor
 
Documenting code yapceu2016
Documenting code yapceu2016Documenting code yapceu2016
Documenting code yapceu2016
 
Code smells and Other Malodorous Software Odors
Code smells and Other Malodorous Software OdorsCode smells and Other Malodorous Software Odors
Code smells and Other Malodorous Software Odors
 
Code review
Code reviewCode review
Code review
 
Revisiting Refactoring
Revisiting RefactoringRevisiting Refactoring
Revisiting Refactoring
 

Mais de Pham Manh Lan

Chăm sóc Khách Hàng trong tổ chức Agile
Chăm sóc Khách Hàng trong tổ chức AgileChăm sóc Khách Hàng trong tổ chức Agile
Chăm sóc Khách Hàng trong tổ chức AgilePham Manh Lan
 
Customer care zero2one
Customer care  zero2oneCustomer care  zero2one
Customer care zero2onePham Manh Lan
 
Agile and what's next
Agile and what's nextAgile and what's next
Agile and what's nextPham Manh Lan
 
Chất lượng ở NAL
Chất lượng ở NALChất lượng ở NAL
Chất lượng ở NALPham Manh Lan
 
Nal Group - Hành trình Agile
Nal Group - Hành trình AgileNal Group - Hành trình Agile
Nal Group - Hành trình AgilePham Manh Lan
 

Mais de Pham Manh Lan (6)

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

Último

11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdfHafizMudaserAhmad
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...121011101441
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate productionChinnuNinan
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
Crystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxCrystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxachiever3003
 
Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptbibisarnayak0
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...Erbil Polytechnic University
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxsiddharthjain2303
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfChristianCDAM
 

Último (20)

11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...Instrumentation, measurement and control of bio process parameters ( Temperat...
Instrumentation, measurement and control of bio process parameters ( Temperat...
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
Designing pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptxDesigning pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptx
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
Crushers to screens in aggregate production
Crushers to screens in aggregate productionCrushers to screens in aggregate production
Crushers to screens in aggregate production
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
Crystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptxCrystal Structure analysis and detailed information pptx
Crystal Structure analysis and detailed information pptx
 
Autonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.pptAutonomous emergency braking system (aeb) ppt.ppt
Autonomous emergency braking system (aeb) ppt.ppt
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Energy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptxEnergy Awareness training ppt for manufacturing process.pptx
Energy Awareness training ppt for manufacturing process.pptx
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdf
 

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

Notas do Editor

  1. Nhìn lại hoạt động coding review & refactoring (tái định nghĩa trong NAL)
  2. 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.
  3. 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.
  4. 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.”
  5. 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.