SlideShare a Scribd company logo
1 of 10
software
craftsmanship
rsingla@ford.com
Code Smells
Change Preventers
Object-Orientation Abusers
4
5
Change Preventers
Couplers
3
2
Bloaters
1
Dispensables
CODE SMELLS!!!
Change Preventers
Prevent = keep (something) from happening
Dread it. Run from it. Destiny still arrives.
ACCEPT THE CHANGE!
5
Parallel Inheritance Hierarchies
Divergent Change Shotgun Surgery
DIVERGENT CHANGE
A class gets changed in different ways for various unrelated reasons
Increased code complexity
Unorganized code
Code becomes inextensible
Difficult to support code base
Violation of Single Responsibility Principle
Class takes care of more than one distinct responsibility
We should be able to jump to a single clear point in the code to make a change
CAUSE
- Code Extensibility
Identify behaviors that do not belong to the class or behaviors that change for a
particular cause  Put them into separate class
 Extract Class
Single Responsibility Principle
SHOTGUN SURGERY
A small change in code requires changes in multiple classes
CODE
Changing code = Firing shotgun at the code
Single change affects large number of classes
Increased code complexity
Unorganized code
Code becomes inextensible
Difficult to support code base
Single responsibility has been split across multiple classesCAUSE
Identify code in different classes that gets affected at the same time 
Group this into a single class
 Move Field
 Move Method
Certain class becomes empty after moving code?
 Inline Class
CODE
PARALLEL INHERITANCE HIERARCHIES
Creating a subclass in one hierarchy forces you to create a subclass in another hierarchy
Special case of Shotgun Surgery
Increased code duplication
Unorganized code
Code becomes inextensible
Difficult to support code base
VEHICLE
- operator
+ getOperator ( )
OPERATOR
+ accelerate ( )
+ turn ( )
CAR PLANE
PILOT
+ barrelRoll ( )
DRIVER
+ handBrake ( )
For every new subclass of Vehicle,
a new subclass of Operator is required
VEHICLE
+ getOperator(): Operator
OPERATOR
+ accelerate ( )
+ turn ( )
PILOT
+ barrelRoll ( )
DRIVER
+ handBrake ( )
CAR
Driver driver
+getOperator( ): Driver
PLANE
Pilot pilot
+getOperator( ): Pilot
Merge one hierarchy into another  Extend the new hierarchy
 Move Field
 Move Method
(class CarDriver implements VehicleOperator)
Do not collapse  Extend both hierarchies
(class CarDriver implements Vehicle, Operator)
None of the solutions feel good? Use:
 Intelligent Children Pattern
INTELLIGENT CHILDREN PATTERN
Trust your child
to do it for you!
I WILL NEVER WRITE BAD CODE
rsingla@ford.com

More Related Content

What's hot

Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract classAmit Trivedi
 
OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide.pdf
OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide.pdfOCA Oracle Certified Associate Java SE 8 Programmer I Study Guide.pdf
OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide.pdfpepillo14361
 
Command Line Arguments in C#
Command Line Arguments in C#Command Line Arguments in C#
Command Line Arguments in C#Ali Hassan
 
Clean Code Principles
Clean Code PrinciplesClean Code Principles
Clean Code PrinciplesYeurDreamin'
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and InterfaceHaris Bin Zahid
 
Lecture 5 sorting and searching
Lecture 5   sorting and searchingLecture 5   sorting and searching
Lecture 5 sorting and searchingNada G.Youssef
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructorsRavi_Kant_Sahu
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keywordtanu_jaswal
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresPriyanka Rana
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in javaHitesh Kumar
 

What's hot (20)

Code smell overview
Code smell overviewCode smell overview
Code smell overview
 
Pure virtual function and abstract class
Pure virtual function and abstract classPure virtual function and abstract class
Pure virtual function and abstract class
 
OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide.pdf
OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide.pdfOCA Oracle Certified Associate Java SE 8 Programmer I Study Guide.pdf
OCA Oracle Certified Associate Java SE 8 Programmer I Study Guide.pdf
 
Loops in C
Loops in CLoops in C
Loops in C
 
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & MultithreadingB.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
B.Sc. III(VI Sem) Advance Java Unit1: Exception Handling & Multithreading
 
Command Line Arguments in C#
Command Line Arguments in C#Command Line Arguments in C#
Command Line Arguments in C#
 
Clean Code Principles
Clean Code PrinciplesClean Code Principles
Clean Code Principles
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
 
Class method
Class methodClass method
Class method
 
Lecture 5 sorting and searching
Lecture 5   sorting and searchingLecture 5   sorting and searching
Lecture 5 sorting and searching
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
 
Ppt on this and super keyword
Ppt on this and super keywordPpt on this and super keyword
Ppt on this and super keyword
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Inheritance
InheritanceInheritance
Inheritance
 
Python set
Python setPython set
Python set
 
Merge sort
Merge sortMerge sort
Merge sort
 

Similar to Software Craftsmanship - Code Smells - Change Preventers

Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeValerio Maggio
 
Software Craftsmanship - Code Smells - Dispensables
Software Craftsmanship - Code Smells - DispensablesSoftware Craftsmanship - Code Smells - Dispensables
Software Craftsmanship - Code Smells - DispensablesRajat Singla
 
Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Anshul Vinayak
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxRudranilDas11
 
Baroda code smell and refactoring
Baroda  code smell and refactoringBaroda  code smell and refactoring
Baroda code smell and refactoringMamata Gelanee
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 
Visual studio2012 tipsandtricks
Visual studio2012 tipsandtricksVisual studio2012 tipsandtricks
Visual studio2012 tipsandtricksTung Nguyen Thanh
 
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
 
ABAP Object oriented concepts
ABAP Object oriented conceptsABAP Object oriented concepts
ABAP Object oriented conceptsDharmeshKumar49
 
Conscious Decoupling - Lone Star PHP
Conscious Decoupling - Lone Star PHPConscious Decoupling - Lone Star PHP
Conscious Decoupling - Lone Star PHPCiaranMcNulty
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...Simplilearn
 
Programming for a better world
Programming for a better worldProgramming for a better world
Programming for a better worldjhansi reddy
 
Top 10 Interview Questions For Java
Top 10 Interview Questions For JavaTop 10 Interview Questions For Java
Top 10 Interview Questions For JavaEME Technologies
 

Similar to Software Craftsmanship - Code Smells - Change Preventers (20)

Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing code
 
Software Craftsmanship - Code Smells - Dispensables
Software Craftsmanship - Code Smells - DispensablesSoftware Craftsmanship - Code Smells - Dispensables
Software Craftsmanship - Code Smells - Dispensables
 
Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Code Smells and Its type (With Example)
Code Smells and Its type (With Example)
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
Baroda code smell and refactoring
Baroda  code smell and refactoringBaroda  code smell and refactoring
Baroda code smell and refactoring
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
L04 Software Design Examples
L04 Software Design ExamplesL04 Software Design Examples
L04 Software Design Examples
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Visual studio2012 tipsandtricks
Visual studio2012 tipsandtricksVisual studio2012 tipsandtricks
Visual studio2012 tipsandtricks
 
Core java by amit
Core java by amitCore java by amit
Core java by amit
 
Clean code-v2.2
Clean code-v2.2Clean code-v2.2
Clean code-v2.2
 
Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - Refactoring
 
Refactoring Chapter11
Refactoring Chapter11Refactoring Chapter11
Refactoring Chapter11
 
Refactoring
RefactoringRefactoring
Refactoring
 
ABAP Object oriented concepts
ABAP Object oriented conceptsABAP Object oriented concepts
ABAP Object oriented concepts
 
Conscious Decoupling - Lone Star PHP
Conscious Decoupling - Lone Star PHPConscious Decoupling - Lone Star PHP
Conscious Decoupling - Lone Star PHP
 
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
C++ Inheritance Tutorial | Introduction To Inheritance In C++ Programming Wit...
 
Programming for a better world
Programming for a better worldProgramming for a better world
Programming for a better world
 
Refactoring
RefactoringRefactoring
Refactoring
 
Top 10 Interview Questions For Java
Top 10 Interview Questions For JavaTop 10 Interview Questions For Java
Top 10 Interview Questions For Java
 

More from Rajat Singla

Compliance Psychology
Compliance PsychologyCompliance Psychology
Compliance PsychologyRajat Singla
 
Introduction to Bill Of Materials
Introduction to Bill Of MaterialsIntroduction to Bill Of Materials
Introduction to Bill Of MaterialsRajat Singla
 
Software Craftsmanship - Code Smells - Object Orientation Abusers
Software Craftsmanship - Code Smells - Object Orientation AbusersSoftware Craftsmanship - Code Smells - Object Orientation Abusers
Software Craftsmanship - Code Smells - Object Orientation AbusersRajat Singla
 
Software Craftsmanship - Code Smells - Couplers
Software Craftsmanship - Code Smells - CouplersSoftware Craftsmanship - Code Smells - Couplers
Software Craftsmanship - Code Smells - CouplersRajat Singla
 
Software Craftsmanship - Introduction to Code Smells
Software Craftsmanship - Introduction to Code SmellsSoftware Craftsmanship - Introduction to Code Smells
Software Craftsmanship - Introduction to Code SmellsRajat Singla
 
Software Craftsmanship - Code Smells - Bloaters
Software Craftsmanship - Code Smells - BloatersSoftware Craftsmanship - Code Smells - Bloaters
Software Craftsmanship - Code Smells - BloatersRajat Singla
 
Introduction to Software Craftsmanship
Introduction to Software CraftsmanshipIntroduction to Software Craftsmanship
Introduction to Software CraftsmanshipRajat Singla
 

More from Rajat Singla (7)

Compliance Psychology
Compliance PsychologyCompliance Psychology
Compliance Psychology
 
Introduction to Bill Of Materials
Introduction to Bill Of MaterialsIntroduction to Bill Of Materials
Introduction to Bill Of Materials
 
Software Craftsmanship - Code Smells - Object Orientation Abusers
Software Craftsmanship - Code Smells - Object Orientation AbusersSoftware Craftsmanship - Code Smells - Object Orientation Abusers
Software Craftsmanship - Code Smells - Object Orientation Abusers
 
Software Craftsmanship - Code Smells - Couplers
Software Craftsmanship - Code Smells - CouplersSoftware Craftsmanship - Code Smells - Couplers
Software Craftsmanship - Code Smells - Couplers
 
Software Craftsmanship - Introduction to Code Smells
Software Craftsmanship - Introduction to Code SmellsSoftware Craftsmanship - Introduction to Code Smells
Software Craftsmanship - Introduction to Code Smells
 
Software Craftsmanship - Code Smells - Bloaters
Software Craftsmanship - Code Smells - BloatersSoftware Craftsmanship - Code Smells - Bloaters
Software Craftsmanship - Code Smells - Bloaters
 
Introduction to Software Craftsmanship
Introduction to Software CraftsmanshipIntroduction to Software Craftsmanship
Introduction to Software Craftsmanship
 

Recently uploaded

英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 

Recently uploaded (20)

英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 

Software Craftsmanship - Code Smells - Change Preventers

  • 3. Change Preventers Prevent = keep (something) from happening Dread it. Run from it. Destiny still arrives. ACCEPT THE CHANGE! 5 Parallel Inheritance Hierarchies Divergent Change Shotgun Surgery
  • 4. DIVERGENT CHANGE A class gets changed in different ways for various unrelated reasons Increased code complexity Unorganized code Code becomes inextensible Difficult to support code base Violation of Single Responsibility Principle Class takes care of more than one distinct responsibility We should be able to jump to a single clear point in the code to make a change CAUSE - Code Extensibility
  • 5. Identify behaviors that do not belong to the class or behaviors that change for a particular cause  Put them into separate class  Extract Class Single Responsibility Principle
  • 6. SHOTGUN SURGERY A small change in code requires changes in multiple classes CODE Changing code = Firing shotgun at the code Single change affects large number of classes Increased code complexity Unorganized code Code becomes inextensible Difficult to support code base Single responsibility has been split across multiple classesCAUSE
  • 7. Identify code in different classes that gets affected at the same time  Group this into a single class  Move Field  Move Method Certain class becomes empty after moving code?  Inline Class CODE
  • 8. PARALLEL INHERITANCE HIERARCHIES Creating a subclass in one hierarchy forces you to create a subclass in another hierarchy Special case of Shotgun Surgery Increased code duplication Unorganized code Code becomes inextensible Difficult to support code base VEHICLE - operator + getOperator ( ) OPERATOR + accelerate ( ) + turn ( ) CAR PLANE PILOT + barrelRoll ( ) DRIVER + handBrake ( ) For every new subclass of Vehicle, a new subclass of Operator is required
  • 9. VEHICLE + getOperator(): Operator OPERATOR + accelerate ( ) + turn ( ) PILOT + barrelRoll ( ) DRIVER + handBrake ( ) CAR Driver driver +getOperator( ): Driver PLANE Pilot pilot +getOperator( ): Pilot Merge one hierarchy into another  Extend the new hierarchy  Move Field  Move Method (class CarDriver implements VehicleOperator) Do not collapse  Extend both hierarchies (class CarDriver implements Vehicle, Operator) None of the solutions feel good? Use:  Intelligent Children Pattern INTELLIGENT CHILDREN PATTERN Trust your child to do it for you!
  • 10. I WILL NEVER WRITE BAD CODE rsingla@ford.com

Editor's Notes

  1. This presentation is continuation to “Software Craftsmanship – Code Smells_4_Object Orientation Abusers”. Here we will cover the fifth and the final category of code smells – “Change Preventers”.
  2. How do we know that code we have written is good or bad? Is it a subjective opinion or is there any metrics to determine whether it is readable/extensible/maintainable? Luckily for us, there IS A METRIC! When you go past a garbage can, you tend to block your nose, as the foul smell is almost unbearable. When there is something wrong with the code, we say there are certain elements in it which stink, making the whole code stink as well! These stinky elements are referred to as code smells. Now, when it comes to real life objects, the sense of foul smell is an innate human tendency. But when it comes to code, we need to teach ourselves to identify these code smells. In other words, we need to develop a nose to identify foul smells in the code. And this ability can only be developed when you are relatively young (new to development). As soon as a hatchling starts flying, its mother teaches it to hunt, for it won’t survive in its adulthood without the ability. The same goes with the ability to smell stink inside the code. It becomes harder and harder to develop this ability as we grow further into software development without it. For it is easier to learn than to unlearn. Writing a code with wrong practices is more dangerous than not writing the code at all! IT IS VERY IMPORTANT FOR ANY YOUNG DEVELOPER TO DEVELOP A NOSE FOR THESE CODE SMELLS! There are broadly 5 categories code smells are divided into. We are covering the fifth category here: Change Preventers
  3. When certain elements in your code prevent it from being modified, the elements are said to fall under the category of code smells called - Change Preventers These elements often make it difficult for the programmer to modify the code. Either a change breaks the code or it effects so many other elements in the code, that even for a small change, the programmer ends up spending huge amount of time on the code base. Program development becomes much more complicated and expensive as a result. Lets look at these elements in detail!
  4. Imagine a box outside your house that acts as a master control for electricity, water supply and gas supply. Now since this box handles so many supplies, there is a spaghetti of wires present inside it. Every time you have a problem with any of these three supplies, you end up looking inside the same box, trying to figure out which wire connects to which supply. There is a high probability of you touching a wrong wire and ending up loosing the water supply while fixing the electricity. Isn’t it? What’s the primary problem here? The box handles more than one supply. In a similar fashion, if your class handles more than 1 responsibility, you end up coming back to it for lots of different reasons. You end up modifying the same class for various unrelated feature requests and there is high probability of breaking one of the other features during modification. Your code is said to exhibit Divergent Change smell. Not only it is risky to modify the class, a class handling more than 1 responsibility brings with it all the drawbacks of a Large Class. (Both these code smells often co-exist)
  5. Classes that have more than one distinct responsibility (more than one reason to change) violate the Single Responsibility design principle. This principle states that a “class should handle one and only one responsibility alone.” Modify your code to follow this design principle Treatment Split up the behavior of the class via Extract Class. If different classes have the same behavior, you may want to combine the classes through inheritance (Extract Superclass and Extract Subclass)
  6. Consider yourself having around 5 boxes outside your house controlling just a single water supply. Any problem with water supply results in you spending huge amount of time looking at each of the boxes and trying to figure out which one has a defect. And fixing one may force you to change some connections in others as well! When a small change comes to your code, and forces you to make huge changes to your codebase by modifying many classes together at once, the change acts like a shot from a shotgun affecting huge amount of code in one shot! This happens when a single feature (responsibility), instead of being assigned to a single class, is assigned to multiple classes in the code. So when a change request comes for that particular feature, you end up modifying code at various places.
  7. Treatment (read the slide)
  8. When you create a new subclass for a class, and you find yourself needing to create a subclass for another class, you might have Parallel Inheritance Hierarchies in your code. (Read the slide)
  9. Treatment (read the slide)