SlideShare une entreprise Scribd logo
1  sur  15
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

Contenu connexe

En vedette

En vedette (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
 

Dernier

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
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 Processorsdebabhi2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 

Dernier (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

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

Notes de l'éditeur

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