SlideShare a Scribd company logo
1 of 14
Avoid Code Duplication!
  Principles & Patterns

          Lars-Erik Kindblad
           Senior Consultant
          Blog: kindblad.com
Agenda

 Why avoid code duplication?
 Principles and patterns:
  Extract Method Principle
  Inheritance Principle
  Execute Around Method Pattern
Why avoid code duplication?

 “The way you get programmer productivity is by eliminating the lines of
  code you have to write” – Steve Jobs

 How? Stop duplicating and repeating code! Less Code to Write = Less
  Code to Maintain = More features delivered = More productive
 We normalize databases – should also normalize our code
 DRY – Don’t repeat yourself – Single Source of Truth
Extract Method Principle: Example


                                    Duplicated Code
Extract Method Principle: Example


              Extract the code to a new method
Extract Method Principle: Example


              Refactored to call the new method
Inheritance Principle: Example


                  Duplicated Code
Inheritance Principle: Example


            Refactored to a common base class
Execute Around Method Pattern: Example


                       Duplicated Code
Execute Around Method Pattern: Example

         Refactored to a new execute around method
Execute Around Method Pattern: Example


             Refactored to use the new method
Execute Around Method Pattern: Example


                 Or:
About Capgemini
With more than 120,000 people in 40 countries, Capgemini is one
of the world's foremost providers of consulting, technology and
outsourcing services. The Group reported 2011 global revenues
of EUR 9.7 billion.
Together with its clients, Capgemini creates and delivers
business and technology solutions that fit their needs and drive
the results they want. A deeply multicultural organization,
Capgemini has developed its own way of working, the
Collaborative Business ExperienceTM, and draws on Rightshore ®,
its worldwide delivery model.

Rightshore® is a trademark belonging to Capgemini
                                                                   www.capgemini.com

                                                                    The information contained in this presentation is proprietary.
                                                                                         © 2012 Capgemini. All rights reserved.

More Related Content

Viewers also liked

Application Architecture by Lars-Erik Kindblad, Capgemini
Application Architecture by Lars-Erik Kindblad, CapgeminiApplication Architecture by Lars-Erik Kindblad, Capgemini
Application Architecture by Lars-Erik Kindblad, Capgemini
Lars-Erik Kindblad
 
Anforderungsanalyse und UML Grundlagen
Anforderungsanalyse und UML GrundlagenAnforderungsanalyse und UML Grundlagen
Anforderungsanalyse und UML Grundlagen
Christian Baranowski
 

Viewers also liked (20)

Android coding standard
Android coding standard Android coding standard
Android coding standard
 
Responsive Design - Quick & Dirty
Responsive Design - Quick & DirtyResponsive Design - Quick & Dirty
Responsive Design - Quick & Dirty
 
Ready or not: No UI vom Verschwinden des Graphical User Interfaces
Ready or not: No UI vom Verschwinden des Graphical User InterfacesReady or not: No UI vom Verschwinden des Graphical User Interfaces
Ready or not: No UI vom Verschwinden des Graphical User Interfaces
 
Webinar: Responsive Design
Webinar: Responsive DesignWebinar: Responsive Design
Webinar: Responsive Design
 
Unit Tests are Overrated (NDCOslo 2013)
Unit Tests are Overrated (NDCOslo 2013)Unit Tests are Overrated (NDCOslo 2013)
Unit Tests are Overrated (NDCOslo 2013)
 
Dependency Injection vs Service Locator - Best Practice
Dependency Injection vs Service Locator - Best PracticeDependency Injection vs Service Locator - Best Practice
Dependency Injection vs Service Locator - Best Practice
 
Introduction to FluentData - The Micro ORM
Introduction to FluentData - The Micro ORMIntroduction to FluentData - The Micro ORM
Introduction to FluentData - The Micro ORM
 
Application Architecture by Lars-Erik Kindblad, Capgemini
Application Architecture by Lars-Erik Kindblad, CapgeminiApplication Architecture by Lars-Erik Kindblad, Capgemini
Application Architecture by Lars-Erik Kindblad, Capgemini
 
How to build more reliable, robust and scalable distributed systems
How to build more reliable, robust and scalable distributed systemsHow to build more reliable, robust and scalable distributed systems
How to build more reliable, robust and scalable distributed systems
 
Systementwurf mit UML
Systementwurf mit UMLSystementwurf mit UML
Systementwurf mit UML
 
The Fluent Interface Pattern
The Fluent Interface PatternThe Fluent Interface Pattern
The Fluent Interface Pattern
 
Application Architecture April 2014
Application Architecture April 2014Application Architecture April 2014
Application Architecture April 2014
 
Anforderungsanalyse und UML Grundlagen
Anforderungsanalyse und UML GrundlagenAnforderungsanalyse und UML Grundlagen
Anforderungsanalyse und UML Grundlagen
 
Domain Driven Design und Nosql
Domain Driven Design und Nosql Domain Driven Design und Nosql
Domain Driven Design und Nosql
 
Mobile Patterns - Wie Apps und Co. digitale Interfaces revolutionieren
Mobile Patterns - Wie Apps und Co. digitale Interfaces revolutionierenMobile Patterns - Wie Apps und Co. digitale Interfaces revolutionieren
Mobile Patterns - Wie Apps und Co. digitale Interfaces revolutionieren
 
The Single Responsibility Principle
The Single Responsibility PrincipleThe Single Responsibility Principle
The Single Responsibility Principle
 
Publish & Subscribe to events using an Event Aggregator
Publish & Subscribe to events using an Event AggregatorPublish & Subscribe to events using an Event Aggregator
Publish & Subscribe to events using an Event Aggregator
 
Inversion of Control - Introduction and Best Practice
Inversion of Control - Introduction and Best PracticeInversion of Control - Introduction and Best Practice
Inversion of Control - Introduction and Best Practice
 
Data Access - Best Practice
Data Access - Best PracticeData Access - Best Practice
Data Access - Best Practice
 
Domain-driven design - eine Einführung
Domain-driven design - eine EinführungDomain-driven design - eine Einführung
Domain-driven design - eine Einführung
 

Similar to Avoid code duplication! Principles & Patterns

SLADE 2013 pre-Congress 10-09-2012
SLADE 2013 pre-Congress 10-09-2012SLADE 2013 pre-Congress 10-09-2012
SLADE 2013 pre-Congress 10-09-2012
ldduval
 
Open Innovation: What's behind the buzzword?
Open Innovation:  What's behind the buzzword?Open Innovation:  What's behind the buzzword?
Open Innovation: What's behind the buzzword?
SAP Ariba
 
Leading them on – secrets of successful lead nurturing programmes - The Marke...
Leading them on – secrets of successful lead nurturing programmes - The Marke...Leading them on – secrets of successful lead nurturing programmes - The Marke...
Leading them on – secrets of successful lead nurturing programmes - The Marke...
B2B Marketing
 

Similar to Avoid code duplication! Principles & Patterns (20)

The Product Manager Pathfinder - AIPMM Presentation - ProductCamp SV Spring 2012
The Product Manager Pathfinder - AIPMM Presentation - ProductCamp SV Spring 2012The Product Manager Pathfinder - AIPMM Presentation - ProductCamp SV Spring 2012
The Product Manager Pathfinder - AIPMM Presentation - ProductCamp SV Spring 2012
 
SLADE 2013 pre-Congress 10-09-2012
SLADE 2013 pre-Congress 10-09-2012SLADE 2013 pre-Congress 10-09-2012
SLADE 2013 pre-Congress 10-09-2012
 
Developing your business idea
Developing your business ideaDeveloping your business idea
Developing your business idea
 
Cloud Transformation: A Pragmatic Approach
Cloud Transformation: A Pragmatic ApproachCloud Transformation: A Pragmatic Approach
Cloud Transformation: A Pragmatic Approach
 
Managerial economics ppt @ mba 2009
Managerial economics ppt @ mba 2009Managerial economics ppt @ mba 2009
Managerial economics ppt @ mba 2009
 
Managerial economics ppt @ mba 2009
Managerial economics ppt @ mba 2009Managerial economics ppt @ mba 2009
Managerial economics ppt @ mba 2009
 
7 ways to future-proof your company
7 ways to future-proof your company7 ways to future-proof your company
7 ways to future-proof your company
 
How SAP Uses Website Testing to Drive Business Value
How SAP Uses Website Testing to Drive Business ValueHow SAP Uses Website Testing to Drive Business Value
How SAP Uses Website Testing to Drive Business Value
 
twago @ Maastricht week of entrepreneurship
twago @ Maastricht week of entrepreneurshiptwago @ Maastricht week of entrepreneurship
twago @ Maastricht week of entrepreneurship
 
Capgemini
CapgeminiCapgemini
Capgemini
 
Innovation for the Future, Željko Vujinović
Innovation for the Future, Željko VujinovićInnovation for the Future, Željko Vujinović
Innovation for the Future, Željko Vujinović
 
Aca Talent
Aca TalentAca Talent
Aca Talent
 
Dutch entrepreneurs visiting twago in Berlin
Dutch entrepreneurs visiting twago in BerlinDutch entrepreneurs visiting twago in Berlin
Dutch entrepreneurs visiting twago in Berlin
 
Corporate Presentation R3
Corporate Presentation R3Corporate Presentation R3
Corporate Presentation R3
 
Open Innovation: What's behind the buzzword?
Open Innovation:  What's behind the buzzword?Open Innovation:  What's behind the buzzword?
Open Innovation: What's behind the buzzword?
 
Leading them on – secrets of successful lead nurturing programmes - The Marke...
Leading them on – secrets of successful lead nurturing programmes - The Marke...Leading them on – secrets of successful lead nurturing programmes - The Marke...
Leading them on – secrets of successful lead nurturing programmes - The Marke...
 
Digital manufacturing cwin18 mexico
Digital manufacturing cwin18 mexicoDigital manufacturing cwin18 mexico
Digital manufacturing cwin18 mexico
 
Developing your business idea
Developing your business ideaDeveloping your business idea
Developing your business idea
 
eFolder Partner Chat_How to Do Lunch and Learns
eFolder Partner Chat_How to Do Lunch and LearnseFolder Partner Chat_How to Do Lunch and Learns
eFolder Partner Chat_How to Do Lunch and Learns
 
No Cost Product Training
No Cost Product TrainingNo Cost Product Training
No Cost Product Training
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Avoid code duplication! Principles & Patterns

  • 1. Avoid Code Duplication! Principles & Patterns Lars-Erik Kindblad Senior Consultant Blog: kindblad.com
  • 2. Agenda  Why avoid code duplication?  Principles and patterns:  Extract Method Principle  Inheritance Principle  Execute Around Method Pattern
  • 3. Why avoid code duplication?  “The way you get programmer productivity is by eliminating the lines of code you have to write” – Steve Jobs  How? Stop duplicating and repeating code! Less Code to Write = Less Code to Maintain = More features delivered = More productive  We normalize databases – should also normalize our code  DRY – Don’t repeat yourself – Single Source of Truth
  • 4. Extract Method Principle: Example Duplicated Code
  • 5. Extract Method Principle: Example Extract the code to a new method
  • 6. Extract Method Principle: Example Refactored to call the new method
  • 8. Inheritance Principle: Example Refactored to a common base class
  • 9. Execute Around Method Pattern: Example Duplicated Code
  • 10. Execute Around Method Pattern: Example Refactored to a new execute around method
  • 11. Execute Around Method Pattern: Example Refactored to use the new method
  • 12. Execute Around Method Pattern: Example Or:
  • 13.
  • 14. About Capgemini With more than 120,000 people in 40 countries, Capgemini is one of the world's foremost providers of consulting, technology and outsourcing services. The Group reported 2011 global revenues of EUR 9.7 billion. Together with its clients, Capgemini creates and delivers business and technology solutions that fit their needs and drive the results they want. A deeply multicultural organization, Capgemini has developed its own way of working, the Collaborative Business ExperienceTM, and draws on Rightshore ®, its worldwide delivery model. Rightshore® is a trademark belonging to Capgemini www.capgemini.com The information contained in this presentation is proprietary. © 2012 Capgemini. All rights reserved.