Академический Документы
Профессиональный Документы
Культура Документы
(Anti Pattern)
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.
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.