120 likes | 439 Views
The Evils of Copy and Paste. Presented by Daniel Daugherty Daniel_cfug@danield.us. Why is Copy and Paste Evil?. Wait a second I use copy and paste all the time what is wrong with it?????? You mean I have to retype everything every time??. Why is Copy and Paste Evil?.
E N D
The Evils of Copy and Paste • Presented by • Daniel Daugherty • Daniel_cfug@danield.us
Why is Copy and Paste Evil? • Wait a second I use copy and paste all the time what is wrong with it?????? • You mean I have to retype everything every time??
Why is Copy and Paste Evil? • Copy and Paste != Code Reuse • Copy and Paste is code duplication • But more is good right?
What should I do instead? • Cut and Paste is your friend. • Cut the code and place where it can be referenced in two or more places (actual code reuse). • This may require some refactoring of the code into a more reusable segment of code.
Advantages Of Cut and Paste • No duplication of code • No duplication of bug(s) within duplicated code. • Improved Readability • Improved Maintainability • Improved Testability
variables include module custom tags import CFC Functions Web Services AJAX Code Reuse options in ColdFusion
Does it matter how much I copy and paste? • Copying as little as one word could be a reason to extract that into a reusable segment. Even simple text labels. • Examples • Externalize a field label or column header • If length(x) and isdate(x) • If length(y) and isdate(y) becomes • If validDate(x) If validDate(y)
Copy Paste Change? • But I just need to copy paste this 3 lines and make two small changes. • Small changes can be very error prone. Large changes tend to be tested better than small. • Possible options for reuse of this code. • CFModule/Custom Tag • CFC/Function
What about other languages? • JavaScript, ActionScript - methods, variables, external files, prototypes. • CSS - named classes , multiple selectors for one set of attribute(s). Breaking style up into smaller re-useable blocks: Layout, Text, Color … • Java - classes, methods, variables, JSP tags, EJB, web services, …
What about other applications? • Are both applications for same customer/company/client? • Use web services to make the logic available to both applications. • If both live on the same servers use mappings/custom tag paths to allow both apps to reference the same code.
Wrap Up • Try keeping track of how many times you copy and paste code for one day. You might be surprised at how often you do it. • Then think each time you do how can I move this to a reusable format. • Some copy paste is OK. • Questions?