Вы находитесь на странице: 1из 12

Cut and Paste Programming

(Anti Pattern)

S.Archana Vibha Devi


08MX04
Introduction
Anti-patterns
BAD solution to common problem – recognise and
refactor

Cut-and-Paste Programming is a form of code reuse


and commonly practiced in software development.

It is a major source of bugs and maintainability


problems.
Cut and Paste Programming
Also Known As
Clipboard Coding, Software Cloning, Software Propagation
Most Frequent Scale
Application
Re-factored Solution Name
Black Box Reuse
Re-factored Solution Type
Software
Root Cause:
Sloth (adaptation of the most simple solution)
CPP Contd…
Unbalanced Forces
Management of Resources, Technology Transfer

Anecdotal Evidence
“Hey, I thought you fixed that bug already, so why is it
doing this again?”
“Man, you guys work fast. Over 400,000 lines of code in
three weeks is outstanding progress!”
General Form
Identified by the presence of several similar code segments
scattered throughout the software project.

Usually, many programmers will be learning by following the


examples of more experienced developers. They may potentially
customize it to support new data types or slightly customized
behavior. This creates code duplication.

Leads to boosting line count metrics

It’s easy to extend the code as the developer has full control and
can quickly meet the new requirements.
Example
class EmpLookup { class DepLookup {
ArrayList<String> emplist; ArrayList<String> deplist;
public EmpLookup() { public DepLookup() {
emplist = new ArrayList<String>(); deplist = new ArrayList<String>();
emplist.add("abc"); deplist.add("IT");
emplist.add("def"); deplist.add("Maths");
emplist.add("ghi"); deplist.add("CT");
emplist.add("jkl"); deplist.add("ECE");
emplist.add("mno"); deplist.add("EEE");
} }
public boolean containsKey(Object key) { public boolean containsKey(Object key) {
int length = emplist.size(); int length = deplist.size();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
if (emplist.get(i).equals(key)) { if (deplist.get(i).equals(key)) {
return (true); return (true);
} }
} }
return false; return false;
} }
} }
Re-factored Solution
White-box reuse (OOP)
Black-box reuse (COP)
Three stages
Code mining - systematic identification of multiple
versions of the same software segment
Refactoring - developing a standard version of the code
segment and reinserting it into the code base
Configuration management - set of policies drawn up to
aid in the prevention of future occurrences of Cut-and-
Paste Programming
Solution for the example
abstract class Lookup
{
public abstract ArrayList<String> getList();
public boolean containsKey(ArrayList list,Object key) {
int length = list.size();
for (int i = 0; i < length; i++) {
if (list.get(i).equals(key)) {
return (true);
}
}
return false;
}
}
Symptoms And Consequences
Lines of code increase without adding to
overall productivity.
Code reviews and inspections are needlessly extended.
It becomes difficult to locate and fix all instances of a
particular mistake.
Excessive software maintenance costs.
Reusable assets are not converted into an easily
reusable form.
Typical Causes
Organization emphasizes short-term payoff more than long-
term investment.

Organization does not advocate or reward reusable components,


and development speed overshadows all other evaluation factors.

Poor understanding of inheritance, composition, and other


development strategies.

Organization insists that code must be a perfect match to the


new task to allow it to be reused.
Related Solutions
Spaghetti Code often contains several instances of the
Cut-and-Paste Programming AntiPattern.
y o u
h a n k
T

Вам также может понравиться