views:

71

answers:

3

I have a C# project with a large collection of cut-and paste cloned code. I have implemented a method that covers 90% of the usages (about 300 instances), but manually converting them by hand will probably introduce more errors than it fixes. Are there any (preferably free) tools that are designed to help with this?

Basically the Code looks something like this:

////////////// Section marker (sometimes absent)
TempCheck = "SomeFlavour1";
// 5 lines of common code related to TempCheck with various different whitespace and tabbing and newlines
Create new Object of type CSomeFlavour1()
// 20 lines of common code with different whitespace and tabbing and newlines

////////////// Section marker (sometimes absent)
TempCheck = "SomeFlavour2";
// 5 lines of common code related to TempCheck with various different whitespace and tabbing and newlines
Create new Object of type CSomeFlavour2
// 20 lines of common code with different whitespace and tabbing and newlines

I want to reduce these to:

NewMethod("SomeFlavour1","CSomeFlavour1");
NewMethod("SomeFlavour2","CSomeFlavour2");

Hope that makes sense.

A: 

PMD is a project that aims to help with this. I've never used it so can't vouch for its abilities, but it might help.

Checkstyle seems to be another free option.

simian costs, but might be worth it.

This question also has some other options

Sam Holder
A: 

Is all the code common except for the Flavours?

Just write a small console app or use a tool like Linqpad to output the method calls. eg

console.writeline(string.format("NewMethod("{0}","{1}")",input1,input2));

(My c# is rusty so that might not be 100% syntactically correct)

Then copy and paste that in to replace the existing code.

geoff
In 90% of the cases the code is common. I'm looking to find the cases that are different as a by-product of this clean-up
My Other Me
+5  A: 

I've not had a chance to play with it yet, but version 5 (currently in beta) of ReSharper offers a new feature called Structural Search and Replace which offers:

Solutions are commonly flooded with various code smells that are hard to eliminate right away. ReSharper 5 helps you get rid of them in existing code, but more than that, it allows you to create custom inspections to remove poor coding artifacts on a recurring basis.

Specifically, ReSharper helps configure custom, sharable code patterns, search for them, replace them, include them in code analysis, and even use quick-fixes for regular code maintenance! Building patterns and enforcing good practices has never been this easy. Corporate and team policies, custom frameworks, favorite open source libraries and tools — structured patterns are able to cover them all.

Buying a R# 4.5 licence now will entitle you to a free upgrade to 5 when it gets its full release. And no, they don't pay me to say any of this :)

AakashM
That looks awesome. I'll give that a try and see if I can push it past the budget controllers.
My Other Me