duplicate C++ code

G

Greg Coles0n

I need to clean up some sloppy programming in a project I am working
on. The previous coder did not believe in functions, they just
duplicated the same code a lot. Is there some utility that can give me
some type of listing of duplicated sections of code so I can make them
into functions?
I expect this could be a utility that could be easy to write, but take
a long time to run depending on the length of duplication it was
looking for.
 
P

Pete Becker

Greg said:
I need to clean up some sloppy programming in a project I am working
on. The previous coder did not believe in functions, they just
duplicated the same code a lot. Is there some utility that can give me
some type of listing of duplicated sections of code so I can make them
into functions?
I expect this could be a utility that could be easy to write, but take
a long time to run depending on the length of duplication it was
looking for.

Regular expression search: (.+).*\1
 
P

Phlip

Greg said:
I need to clean up some sloppy programming in a project I am working
on. The previous coder did not believe in functions, they just
duplicated the same code a lot. Is there some utility that can give me
some type of listing of duplicated sections of code so I can make them
into functions?
I expect this could be a utility that could be easy to write, but take
a long time to run depending on the length of duplication it was
looking for.

Finding copied and pasted code:
http://pmd.sourceforge.net/cpd.html

Shareware:
http://www.topshareware.com/AntiCutAndPaste-download-10608.htm

I apologize for my e-comrades' answers (except Tomás's).

As you go, add unit tests to your code. These are amazingly important, and I
apologize again for we never told you about them.
 
P

Phlip

Greg said:
I found what I was looking for and it works great

http://pmd.sourceforge.net/cpd.html

Next tip:

Given two functions that are similar, don't just cram them back together.
Use "Extract Method Refactor" on the smallest set of lines inside them that
are the same. These will become the new abstraction. Always refactor in
order from the low-hanging fruit.

And read /Working Effectively with Legacy Code/ by Mike Feathers.
 
I

Ira Baxter

Greg Coles0n said:
I need to clean up some sloppy programming in a project I am working
on. The previous coder did not believe in functions, they just
duplicated the same code a lot. Is there some utility that can give me
some type of listing of duplicated sections of code so I can make them
into functions?
I expect this could be a utility that could be easy to write, but take
a long time to run depending on the length of duplication it was
looking for.

Easy to write? No. A good one parses the source code to
determine reasonable boundaries of entities to match,
and will find both exact and near misses (clones that
have been edited).

See http://www.semanticdesigns.com/Products/Clone/index.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top