CPP(Anti Pattern)

12
S.Archana Vibha Devi 08MX04

Transcript of CPP(Anti Pattern)

8/8/2019 CPP(Anti Pattern)

http://slidepdf.com/reader/full/cppanti-pattern 1/12

S.Archana Vibha Devi

08MX04

8/8/2019 CPP(Anti Pattern)

http://slidepdf.com/reader/full/cppanti-pattern 2/12

Introductiony Anti-patterns

y BAD solution to common problem  recognise and

refactor

y Cut-and-Paste Programming is a form of code reuseand commonly practiced in software development.

y It is a major source of bugs and maintainability problems.

8/8/2019 CPP(Anti Pattern)

http://slidepdf.com/reader/full/cppanti-pattern 3/12

Cut and Paste Programmingy  Also Known As

y Clipboard Coding, Software Cloning, Software Propagation

y

Most Frequent Scaley  Application

y Re-factored Solution Name

y Black Box Reuse

y

Re-factored Solution Typey Software

y Root Cause:

y Sloth (adaptation of the most simple solution)

8/8/2019 CPP(Anti Pattern)

http://slidepdf.com/reader/full/cppanti-pattern 4/12

CPP Contdy Unbalanced Forces

y Management of Resources, Technology Transfer

y Anecdotal Evidence

y Hey, I thought you fixed that bug already, so why is itdoing this again?

y Man, you guys work fast. Over 400,000 lines of code inthree weeks is outstanding progress!

8/8/2019 CPP(Anti Pattern)

http://slidepdf.com/reader/full/cppanti-pattern 5/12

General Formy Identified by the presence of several similar code segments

scattered throughout the software project.

y Usually, many programmers will be learning by followingthe examples of more experienced developers. They may potentially customize it to support new data types orslightly customized behavior. This creates code duplication.

y Leads to boosting line count metrics

y Its easy to extend the code as the developer has full controland can quickly meet the new requirements.

8/8/2019 CPP(Anti Pattern)

http://slidepdf.com/reader/full/cppanti-pattern 6/12

Exampleclass EmpLookup {

ArrayList<String> emplist;

public EmpLookup() {

emplist = new ArrayList<String>();

emplist.add("abc");

emplist.add("def");

emplist.add("ghi");

emplist.add("jkl");

emplist.add("mno");

}

public boolean containsKey(Object key) {

int length = emplist.size();

for (int i = 0; i < length; i++) {

if (emplist.get(i).equals(key)) {return (true);

}

}

return false;

}

}

class DepLookup {

ArrayList<String> deplist;

public DepLookup() {

deplist = new ArrayList<String>();

deplist.add("IT");

deplist.add("Maths");

deplist.add("CT");

deplist.add("ECE");

deplist.add("EEE");

}

public boolean containsKey(Object key) {

int length = deplist.size();

for (int i = 0; i < length; i++) {

if (deplist.get(i).equals(key)) {return (true);

}

}

return false;

}

}

8/8/2019 CPP(Anti Pattern)

http://slidepdf.com/reader/full/cppanti-pattern 7/12

Re-factored Solutiony W hite-box reuse (OOP)

y Black-box reuse (COP)

y Three stagesy Code mining - systematic identification of multiple

 versions of the same software segment

y Refactoring - developing a standard version of the code

segment and reinserting it into the code basey Configuration management - set of policies drawn up to

aid in the prevention of future occurrences of Cut-and-Paste Programming

8/8/2019 CPP(Anti Pattern)

http://slidepdf.com/reader/full/cppanti-pattern 8/12

Solution for the exampleabstract class Lookup{

public abstract ArrayList<String> getList();

public booleancontainsKey(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;

}}

8/8/2019 CPP(Anti Pattern)

http://slidepdf.com/reader/full/cppanti-pattern 9/12

Symptoms And Consequencesy Lines of code increase without adding to

overall productivity.

y Code reviews and inspections are needlessly extended.y It becomes difficult to locate and fix all instances of a

particular mistake.

y Excessive software maintenance costs.

y Reusable assets are not converted into an easily reusable form.

8/8/2019 CPP(Anti Pattern)

http://slidepdf.com/reader/full/cppanti-pattern 10/12

Typical Causesy Organization emphasizes short-term payoff more than

long-term investment.

y Organization does not advocate or reward reusablecomponents, and development speed overshadows allother evaluation factors.

y Poor understanding of inheritance, composition, and other

development strategies.

y Organization insists that code must be a perfect match tothe new task to allow it to be reused.

8/8/2019 CPP(Anti Pattern)

http://slidepdf.com/reader/full/cppanti-pattern 11/12

Related Solutionsy Spaghetti Code often contains several instances of theCut-and-Paste Programming AntiPattern.

8/8/2019 CPP(Anti Pattern)

http://slidepdf.com/reader/full/cppanti-pattern 12/12