Why Copy-Paste is Bad for Your Code

481 views
366 views

Published on

This is Carlos Tapang's slides on "Why Copy-Paste is Bad for Your Code", which was presented at the Cebu StackOverflow Meetup on 26 May 2012.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
481
On SlideShare
0
From Embeds
0
Number of Embeds
64
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Self-Intro:Microsoft IT Division “We eat dogfood”Physics Instructor (Physics 101 for non-science majors)
  • Why Copy-Paste is Bad for Your Code

    1. 1. Why Copy – Paste is Bad for Your Code A short intro to Design Patterns Carlos C Tapang May 26th, 2012
    2. 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. 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. 4. First-Cut Design Animalenum Movement Movement movement{ Walking, Flying, Slithering }class AnimalMovement movementclass Mammal : Animalint numLegs Mammal int numLegsclass Reptile : Animalint numLegs Reptile int numLegs
    5. 5. AnimalFirst-Cut Design --------------------------------------enum Movement int numLegs{ Walking, Flying, Slithering } Movement movementclass Animalint numLegsMovement movementclass Mammal : Animal Mammalclass Reptile : Animal Reptile
    6. 6. AnimalFirst-Cut Design --------------------------------------enum Movement int numLegs{ Walking, Flying, Slithering } Movement movementenum Terrain{ Mountains, SandDunes, Sea }class Animalint numLegs MammalMovement movement decimal Speed(Terrain t)decimal virtual Speed(Terrain t)class Mammal : Animaldecimal override Speed(Terrain t)class Reptile : Animal Reptiledecimal override Speed(Terrain t) decimal Speed(Terrain t)
    7. 7. First-Cut decimal Speed(Terrain t)Mammal Birdswitch (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. 8. First-Cut decimal Speed(Terrain t)Mammal Birdswitch (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. 9. Second-Cut Design Animalclass Animal int numLegsint numLegsclass AnimalThatWalks : Animalclass AnimalThatFlies : Animal AnimalThatFlies AnimalThatWalks
    10. 10. AnimalSecond-Cut Design int numLegsenum Terrain{ Mountains, SandDunes, Sea}class Animalint numLegsdecimal Speed(Terrain t) AnimalThatFlies AnimalThatWalksclass AnimalThatWalks : Animaldecimal override Speed(Terrain t)class AnimalThatFlies : Animaldecimal override Speed(Terrain t) Birdclass Bird : AnimalThatFliesdecimal override Speed(Terrain t) Mammalclass Mammal : AnimalThatWalksclass Reptile : AnimalThatWalks Reptile
    11. 11. Animal AnimalMovementThird-cut designclass AnimalMovementclass Flying : AnimalMovementclass Walking : AnimalMovement Walking Flyingclass Animalint numLegsAnimalMovement movementclass Mammal : Animalclass Reptile : Animal Mammal Reptile
    12. 12. AnimalThird-cut design decimal Speed(Terrain t)enum Terrain { Mountains, …}class AnimalMovementdecimal Speed(Terrain t)class Flying : AnimalMovement AnimalMovementdecimal Speed(Terrain t) decimal Speed(Terrain t)class Walking : AnimalMovementdecimal Speed(Terrain t)class Animalint numLegsAnimalMovement movement Walking Flyingdecimal Speed(Terrain t) decimal Speed(…) decimal Speed(…)class Mammal : Animalclass Reptile : Animal
    13. 13. Third-Cut DesignAnimal AnimalMovementclass Animal class AnimalMovementint 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. 14. If you find that you are doing a lot of copy-paste, it maybe time to stepback and review your design.CONCLUSION
    15. 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 SoftwareDesign Patterns Philippines at LinkedIn.com or email me atctapang@hotmail.comA SEMINAR FOR YOU

    ×