Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Why Copy – Paste is Bad for Your
            Code
    A short intro to Design Patterns
            Carlos C Tapang
             May 26th, 2012
How many are developers?
How many have studied OO in college? Studied on your own?
How many are applying OO concepts at work? Hobby?
How many of you would say that OO concepts are hard?


I HAVE QUESTIONS
List of Requirements:
• Each type of Animal can have a different number of legs.
• Animal objects must be able to remember and retrieve this
    information.
• Each type of animal can have a different type of movement.
• Animal objects must be able to return how long it will take to move
    from one place to another given a specified type of terrain.

BUILDING A WILDLIFE GAME :
ANIMAL TAXONOMY
First-Cut Design                           Animal
enum Movement
                                      Movement movement
{ Walking, Flying, Slithering }

class Animal
Movement movement

class Mammal : Animal
int numLegs                         Mammal
                                  int numLegs
class Reptile : Animal
int numLegs




                                                    Reptile
                                                 int numLegs
Animal
First-Cut Design                  --------------------------------------
enum Movement
                                  int numLegs
{ Walking, Flying, Slithering }   Movement movement

class Animal
int numLegs
Movement movement

class Mammal : Animal
                                  Mammal
class Reptile : Animal




                                                              Reptile
Animal
First-Cut Design                            --------------------------------------
enum Movement
                                            int numLegs
{ Walking, Flying, Slithering }             Movement movement

enum Terrain
{ Mountains, SandDunes, Sea }

class Animal
int numLegs                         Mammal
Movement movement                   decimal Speed(Terrain t)
decimal virtual Speed(Terrain t)

class Mammal : Animal
decimal override Speed(Terrain t)

class Reptile : Animal                                    Reptile
decimal override Speed(Terrain t)
                                                          decimal Speed(Terrain t)
First-Cut
           decimal Speed(Terrain t)
Mammal                      Bird
switch (movement)           switch(movement)
{                           {
  case Walking:              case Walking:
   break;                      break;
  case Flying:                case Flying:
   break;                      break;
  case Slithering:            case Slithering:
   throw new Exception();      throw new Exception();
}                           }
First-Cut
             decimal Speed(Terrain t)
Mammal                                Bird
switch (movement)                     switch(movement)
{                                     {
  case Walking:
                                       case Walking:
   return WalkingSpeed(numLegs, t);
   break;                                return WalkingSpeed(numLegs, t);
  case Flying:                           break;
   if (this is Bat)                     case Flying:
     return BatFlyingSpeed();            return BirdFlyingSpeed();
   else throw new Exception();           break;
   break;                               case Slithering:
  case Slithering:
                                         throw new Exception();
   throw new Exception();
}                                     }
Second-Cut Design                              Animal
class Animal
                                            int numLegs
int numLegs

class AnimalThatWalks : Animal

class AnimalThatFlies : Animal


                                 AnimalThatFlies




                                                   AnimalThatWalks
Animal
Second-Cut Design                                int numLegs
enum Terrain
{ Mountains, SandDunes, Sea}

class Animal
int numLegs
decimal Speed(Terrain t)            AnimalThatFlies        AnimalThatWalks

class AnimalThatWalks : Animal
decimal override Speed(Terrain t)

class AnimalThatFlies : Animal
decimal override Speed(Terrain t)
                                      Bird
class Bird : AnimalThatFlies
decimal override Speed(Terrain t)
                                                                Mammal

class Mammal : AnimalThatWalks

class Reptile : AnimalThatWalks
                                             Reptile
Animal             AnimalMovement
Third-cut design
class AnimalMovement

class Flying : AnimalMovement

class Walking : AnimalMovement            Walking

                                                           Flying
class Animal
int numLegs
AnimalMovement movement

class Mammal : Animal

class Reptile : Animal           Mammal        Reptile
Animal
Third-cut design                 decimal Speed(Terrain t)
enum Terrain { Mountains, …}

class AnimalMovement
decimal Speed(Terrain t)

class Flying : AnimalMovement                  AnimalMovement
decimal Speed(Terrain t)                       decimal Speed(Terrain t)
class Walking : AnimalMovement
decimal Speed(Terrain t)

class Animal
int numLegs
AnimalMovement movement          Walking                       Flying
decimal Speed(Terrain t)
                                 decimal Speed(…)              decimal Speed(…)
class Mammal : Animal

class Reptile : Animal
Third-Cut Design
Animal                                   AnimalMovement
class Animal                             class AnimalMovement
int numLegs;                             Animal animal;
decimal speedFactor;
AnimalMovement movement;                 public AnimalMovement(Animal a)
                                         {
public Animal()                            animal = a;
{                                        }
  movement = new AnimalMovement(this);
}                                        public decimal Speed(Terrain t)
                                         {
public decimal Speed(Terrain t)            return CalcSpeed(
{                                              animal.numLegs,
  return movement.Speed(t);                    animal.speedFactor, t);
}                                        }
If you find that you are doing a lot of copy-paste, it maybe time to step
back and review your design.

CONCLUSION
If I were to conduct a seminar on Design Patterns, would you attend?
How much is it worth to you? (full day seminar)
Yes, indeed I am conducting a seminar on June 23rd right here in Cebu.

If you need a copy of these slides and the sample, join me at Software
Design Patterns Philippines at LinkedIn.com or email me at
ctapang@hotmail.com

A SEMINAR FOR YOU

More Related Content

Viewers also liked

Write awesome personalized donor thank you emails using GlobalGiving and Face...
Write awesome personalized donor thank you emails using GlobalGiving and Face...Write awesome personalized donor thank you emails using GlobalGiving and Face...
Write awesome personalized donor thank you emails using GlobalGiving and Face...
Marc Maxson / GlobalGiving
 
Obama pecha kucha
Obama pecha kuchaObama pecha kucha
Obama pecha kucha
Marc Maxson / GlobalGiving
 
DB Replication With Rails
DB Replication With RailsDB Replication With Rails
DB Replication With Rails
schoefmax
 
Ushahidi in action worldwide
Ushahidi in action worldwideUshahidi in action worldwide
Ushahidi in action worldwide
Marc Maxson / GlobalGiving
 
What you can do In WatiR
What you can do In WatiRWhat you can do In WatiR
What you can do In WatiR
Wesley Chen
 
Version Control and Continuous Integration
Version Control and Continuous IntegrationVersion Control and Continuous Integration
Version Control and Continuous Integration
Geff Henderson Chang
 
GlobalGiving :- an Agile approach to the Japan Earthquake Disaster, and inter...
GlobalGiving :- an Agile approach to the Japan Earthquake Disaster, and inter...GlobalGiving :- an Agile approach to the Japan Earthquake Disaster, and inter...
GlobalGiving :- an Agile approach to the Japan Earthquake Disaster, and inter...
Marc Maxson / GlobalGiving
 
About The Givers At Global Giving
About The Givers At Global GivingAbout The Givers At Global Giving
About The Givers At Global Giving
Marc Maxson / GlobalGiving
 
Idea webinar-oct-25-2011
Idea webinar-oct-25-2011Idea webinar-oct-25-2011
Idea webinar-oct-25-2011
Marc Maxson / GlobalGiving
 
Promise Power
Promise PowerPromise Power
Caching with Varnish
Caching with VarnishCaching with Varnish
Caching with Varnish
schoefmax
 

Viewers also liked (11)

Write awesome personalized donor thank you emails using GlobalGiving and Face...
Write awesome personalized donor thank you emails using GlobalGiving and Face...Write awesome personalized donor thank you emails using GlobalGiving and Face...
Write awesome personalized donor thank you emails using GlobalGiving and Face...
 
Obama pecha kucha
Obama pecha kuchaObama pecha kucha
Obama pecha kucha
 
DB Replication With Rails
DB Replication With RailsDB Replication With Rails
DB Replication With Rails
 
Ushahidi in action worldwide
Ushahidi in action worldwideUshahidi in action worldwide
Ushahidi in action worldwide
 
What you can do In WatiR
What you can do In WatiRWhat you can do In WatiR
What you can do In WatiR
 
Version Control and Continuous Integration
Version Control and Continuous IntegrationVersion Control and Continuous Integration
Version Control and Continuous Integration
 
GlobalGiving :- an Agile approach to the Japan Earthquake Disaster, and inter...
GlobalGiving :- an Agile approach to the Japan Earthquake Disaster, and inter...GlobalGiving :- an Agile approach to the Japan Earthquake Disaster, and inter...
GlobalGiving :- an Agile approach to the Japan Earthquake Disaster, and inter...
 
About The Givers At Global Giving
About The Givers At Global GivingAbout The Givers At Global Giving
About The Givers At Global Giving
 
Idea webinar-oct-25-2011
Idea webinar-oct-25-2011Idea webinar-oct-25-2011
Idea webinar-oct-25-2011
 
Promise Power
Promise PowerPromise Power
Promise Power
 
Caching with Varnish
Caching with VarnishCaching with Varnish
Caching with Varnish
 

Recently uploaded

FIDO Munich Seminar: Securing Smart Car.pptx
FIDO Munich Seminar: Securing Smart Car.pptxFIDO Munich Seminar: Securing Smart Car.pptx
FIDO Munich Seminar: Securing Smart Car.pptx
FIDO Alliance
 
The Maritime Security. OSINT [EN] .pdf
The Maritime Security. OSINT [EN]   .pdfThe Maritime Security. OSINT [EN]   .pdf
The Maritime Security. OSINT [EN] .pdf
Snarky Security
 
Starlink Product Specifications_HighPerformance-1.pdf
Starlink Product Specifications_HighPerformance-1.pdfStarlink Product Specifications_HighPerformance-1.pdf
Starlink Product Specifications_HighPerformance-1.pdf
ssuser0b9571
 
Top keywords searches on home and garden
Top keywords searches on home and gardenTop keywords searches on home and garden
Top keywords searches on home and garden
riannecreativetwo
 
Webinar: Transforming Substation Automation with Open Source Solutions
Webinar: Transforming Substation Automation with Open Source SolutionsWebinar: Transforming Substation Automation with Open Source Solutions
Webinar: Transforming Substation Automation with Open Source Solutions
DanBrown980551
 
Scientific-Based Blockchain TON Project Analysis Report
Scientific-Based Blockchain  TON Project Analysis ReportScientific-Based Blockchain  TON Project Analysis Report
Scientific-Based Blockchain TON Project Analysis Report
SelcukTOPAL2
 
UiPath Community Day Amsterdam presentations
UiPath Community Day Amsterdam presentationsUiPath Community Day Amsterdam presentations
UiPath Community Day Amsterdam presentations
UiPathCommunity
 
Using ScyllaDB for Real-Time Write-Heavy Workloads
Using ScyllaDB for Real-Time Write-Heavy WorkloadsUsing ScyllaDB for Real-Time Write-Heavy Workloads
Using ScyllaDB for Real-Time Write-Heavy Workloads
ScyllaDB
 
Planetek Italia Corporate Profile Brochure
Planetek Italia Corporate Profile BrochurePlanetek Italia Corporate Profile Brochure
Planetek Italia Corporate Profile Brochure
Planetek Italia Srl
 
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptxFIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Alliance
 
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptxFIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Alliance
 
Project Delivery Methodology on a page with activities, deliverables
Project Delivery Methodology on a page with activities, deliverablesProject Delivery Methodology on a page with activities, deliverables
Project Delivery Methodology on a page with activities, deliverables
CLIVE MINCHIN
 
Epicor Kinetic REST API Services Overview.pptx
Epicor Kinetic REST API Services Overview.pptxEpicor Kinetic REST API Services Overview.pptx
Epicor Kinetic REST API Services Overview.pptx
Piyush Khalate
 
FIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Munich Seminar: FIDO Tech Principles.pptxFIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Alliance
 
Bài tập tiếng anh lớp 9 - Ôn tập tuyển sinh
Bài tập tiếng anh lớp 9 - Ôn tập tuyển sinhBài tập tiếng anh lớp 9 - Ôn tập tuyển sinh
Bài tập tiếng anh lớp 9 - Ôn tập tuyển sinh
NguynThNhQunh59
 
Project management Course in Australia.pptx
Project management Course in Australia.pptxProject management Course in Australia.pptx
Project management Course in Australia.pptx
deathreaper9
 
FIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptxFIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Alliance
 
The learners analyze the various sectors of ICT and evaluate the potential ca...
The learners analyze the various sectors of ICT and evaluate the potential ca...The learners analyze the various sectors of ICT and evaluate the potential ca...
The learners analyze the various sectors of ICT and evaluate the potential ca...
maricrismontales
 
Global Collaboration for Space Exploration.pdf
Global Collaboration for Space Exploration.pdfGlobal Collaboration for Space Exploration.pdf
Global Collaboration for Space Exploration.pdf
Sachin Chitre
 
TrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc Webinar - Innovating with TRUSTe Responsible AI CertificationTrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc
 

Recently uploaded (20)

FIDO Munich Seminar: Securing Smart Car.pptx
FIDO Munich Seminar: Securing Smart Car.pptxFIDO Munich Seminar: Securing Smart Car.pptx
FIDO Munich Seminar: Securing Smart Car.pptx
 
The Maritime Security. OSINT [EN] .pdf
The Maritime Security. OSINT [EN]   .pdfThe Maritime Security. OSINT [EN]   .pdf
The Maritime Security. OSINT [EN] .pdf
 
Starlink Product Specifications_HighPerformance-1.pdf
Starlink Product Specifications_HighPerformance-1.pdfStarlink Product Specifications_HighPerformance-1.pdf
Starlink Product Specifications_HighPerformance-1.pdf
 
Top keywords searches on home and garden
Top keywords searches on home and gardenTop keywords searches on home and garden
Top keywords searches on home and garden
 
Webinar: Transforming Substation Automation with Open Source Solutions
Webinar: Transforming Substation Automation with Open Source SolutionsWebinar: Transforming Substation Automation with Open Source Solutions
Webinar: Transforming Substation Automation with Open Source Solutions
 
Scientific-Based Blockchain TON Project Analysis Report
Scientific-Based Blockchain  TON Project Analysis ReportScientific-Based Blockchain  TON Project Analysis Report
Scientific-Based Blockchain TON Project Analysis Report
 
UiPath Community Day Amsterdam presentations
UiPath Community Day Amsterdam presentationsUiPath Community Day Amsterdam presentations
UiPath Community Day Amsterdam presentations
 
Using ScyllaDB for Real-Time Write-Heavy Workloads
Using ScyllaDB for Real-Time Write-Heavy WorkloadsUsing ScyllaDB for Real-Time Write-Heavy Workloads
Using ScyllaDB for Real-Time Write-Heavy Workloads
 
Planetek Italia Corporate Profile Brochure
Planetek Italia Corporate Profile BrochurePlanetek Italia Corporate Profile Brochure
Planetek Italia Corporate Profile Brochure
 
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptxFIDO Munich Seminar In-Vehicle Payment Trends.pptx
FIDO Munich Seminar In-Vehicle Payment Trends.pptx
 
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptxFIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
FIDO Munich Seminar: Strong Workforce Authn Push & Pull Factors.pptx
 
Project Delivery Methodology on a page with activities, deliverables
Project Delivery Methodology on a page with activities, deliverablesProject Delivery Methodology on a page with activities, deliverables
Project Delivery Methodology on a page with activities, deliverables
 
Epicor Kinetic REST API Services Overview.pptx
Epicor Kinetic REST API Services Overview.pptxEpicor Kinetic REST API Services Overview.pptx
Epicor Kinetic REST API Services Overview.pptx
 
FIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Munich Seminar: FIDO Tech Principles.pptxFIDO Munich Seminar: FIDO Tech Principles.pptx
FIDO Munich Seminar: FIDO Tech Principles.pptx
 
Bài tập tiếng anh lớp 9 - Ôn tập tuyển sinh
Bài tập tiếng anh lớp 9 - Ôn tập tuyển sinhBài tập tiếng anh lớp 9 - Ôn tập tuyển sinh
Bài tập tiếng anh lớp 9 - Ôn tập tuyển sinh
 
Project management Course in Australia.pptx
Project management Course in Australia.pptxProject management Course in Australia.pptx
Project management Course in Australia.pptx
 
FIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptxFIDO Munich Seminar Workforce Authentication Case Study.pptx
FIDO Munich Seminar Workforce Authentication Case Study.pptx
 
The learners analyze the various sectors of ICT and evaluate the potential ca...
The learners analyze the various sectors of ICT and evaluate the potential ca...The learners analyze the various sectors of ICT and evaluate the potential ca...
The learners analyze the various sectors of ICT and evaluate the potential ca...
 
Global Collaboration for Space Exploration.pdf
Global Collaboration for Space Exploration.pdfGlobal Collaboration for Space Exploration.pdf
Global Collaboration for Space Exploration.pdf
 
TrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc Webinar - Innovating with TRUSTe Responsible AI CertificationTrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
TrustArc Webinar - Innovating with TRUSTe Responsible AI Certification
 

Why Copy-Paste is Bad for Your Code

  • 1. Why Copy – Paste is Bad for Your Code A short intro to Design Patterns Carlos C Tapang May 26th, 2012
  • 2. How many are developers? How many have studied OO in college? Studied on your own? How many are applying OO concepts at work? Hobby? How many of you would say that OO concepts are hard? I HAVE QUESTIONS
  • 3. List of Requirements: • Each type of Animal can have a different number of legs. • Animal objects must be able to remember and retrieve this information. • Each type of animal can have a different type of movement. • Animal objects must be able to return how long it will take to move from one place to another given a specified type of terrain. BUILDING A WILDLIFE GAME : ANIMAL TAXONOMY
  • 4. First-Cut Design Animal enum Movement Movement movement { Walking, Flying, Slithering } class Animal Movement movement class Mammal : Animal int numLegs Mammal int numLegs class Reptile : Animal int numLegs Reptile int numLegs
  • 5. Animal First-Cut Design -------------------------------------- enum Movement int numLegs { Walking, Flying, Slithering } Movement movement class Animal int numLegs Movement movement class Mammal : Animal Mammal class Reptile : Animal Reptile
  • 6. Animal First-Cut Design -------------------------------------- enum Movement int numLegs { Walking, Flying, Slithering } Movement movement enum Terrain { Mountains, SandDunes, Sea } class Animal int numLegs Mammal Movement movement decimal Speed(Terrain t) decimal virtual Speed(Terrain t) class Mammal : Animal decimal override Speed(Terrain t) class Reptile : Animal Reptile decimal override Speed(Terrain t) decimal Speed(Terrain t)
  • 7. First-Cut decimal Speed(Terrain t) Mammal Bird switch (movement) switch(movement) { { case Walking: case Walking: break; break; case Flying: case Flying: break; break; case Slithering: case Slithering: throw new Exception(); throw new Exception(); } }
  • 8. First-Cut decimal Speed(Terrain t) Mammal Bird switch (movement) switch(movement) { { case Walking: case Walking: return WalkingSpeed(numLegs, t); break; return WalkingSpeed(numLegs, t); case Flying: break; if (this is Bat) case Flying: return BatFlyingSpeed(); return BirdFlyingSpeed(); else throw new Exception(); break; break; case Slithering: case Slithering: throw new Exception(); throw new Exception(); } }
  • 9. Second-Cut Design Animal class Animal int numLegs int numLegs class AnimalThatWalks : Animal class AnimalThatFlies : Animal AnimalThatFlies AnimalThatWalks
  • 10. Animal Second-Cut Design int numLegs enum Terrain { Mountains, SandDunes, Sea} class Animal int numLegs decimal Speed(Terrain t) AnimalThatFlies AnimalThatWalks class AnimalThatWalks : Animal decimal override Speed(Terrain t) class AnimalThatFlies : Animal decimal override Speed(Terrain t) Bird class Bird : AnimalThatFlies decimal override Speed(Terrain t) Mammal class Mammal : AnimalThatWalks class Reptile : AnimalThatWalks Reptile
  • 11. Animal AnimalMovement Third-cut design class AnimalMovement class Flying : AnimalMovement class Walking : AnimalMovement Walking Flying class Animal int numLegs AnimalMovement movement class Mammal : Animal class Reptile : Animal Mammal Reptile
  • 12. Animal Third-cut design decimal Speed(Terrain t) enum Terrain { Mountains, …} class AnimalMovement decimal Speed(Terrain t) class Flying : AnimalMovement AnimalMovement decimal Speed(Terrain t) decimal Speed(Terrain t) class Walking : AnimalMovement decimal Speed(Terrain t) class Animal int numLegs AnimalMovement movement Walking Flying decimal Speed(Terrain t) decimal Speed(…) decimal Speed(…) class Mammal : Animal class Reptile : Animal
  • 13. Third-Cut Design Animal AnimalMovement class Animal class AnimalMovement int numLegs; Animal animal; decimal speedFactor; AnimalMovement movement; public AnimalMovement(Animal a) { public Animal() animal = a; { } movement = new AnimalMovement(this); } public decimal Speed(Terrain t) { public decimal Speed(Terrain t) return CalcSpeed( { animal.numLegs, return movement.Speed(t); animal.speedFactor, t); } }
  • 14. If you find that you are doing a lot of copy-paste, it maybe time to step back and review your design. CONCLUSION
  • 15. If I were to conduct a seminar on Design Patterns, would you attend? How much is it worth to you? (full day seminar) Yes, indeed I am conducting a seminar on June 23rd right here in Cebu. If you need a copy of these slides and the sample, join me at Software Design Patterns Philippines at LinkedIn.com or email me at ctapang@hotmail.com A SEMINAR FOR YOU

Editor's Notes

  1. Self-Intro:Microsoft IT Division “We eat dogfood”Physics Instructor (Physics 101 for non-science majors)