Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm writing a piece of software which requires the creation of a number of nearly identical blocks of code that only vary in a few locations which need to be replaced by a unique value for each specific instance.

I have the template for the block of code written out, as well as the list of values that need to be feed into the template. How can I automate this with out actually writing more code to do it? Do any text editors have this kind of functionality? I think MS Word's mail merge feature would work, but I'd rather keep my source code away from Word. That just feels wrong. I just downloaded Notepad++, but nothing is really jumping out at me. Also this is a C# project in visual studio so if I can do it in visual studio that would be even better.


In response to the suggestions regarding duplicating functionality I'm really not. The block of code I'm duplicating is below. It's a non-generic class which derives from a. I need to create a large number of these non-generic versions of the class that are associated with specific types. I have

[ComponentDescription("Document XXXXXX", "Returns the XXXXXX objects from the document")]
[DataContract]
public class DocumentXXXXXXCollection : DocumentElements<XXXXXX>
{

}
share|improve this question
add comment

2 Answers

nearly identical blocks of code that only vary in a few locations

I would be thinking of Subroutines, Functions or Subclasses with inherited methods.

I have the template for the block of code written out, as well as the list of values that need to be feed into the template.

IDEs such as Eclipse have support for templates, but they expect you to supply missing values

How can I automate this with out actually writing more code to do it?

The trouble is, if you are a programmer, writing code to substitute values into templates is often a relatively easy solution. I find Perl is very suited to many sorts of text manipulation.

For example: http://template-toolkit.org/

share|improve this answer
add comment

Are you sure the way you are writing your program is really the best way? This kind of code duplication is rarely required and almost always inefficient and a waste of resources. Can you not come up with a way to re-use the same block of code passing it extra parameters, or something, instead of writing the same code over and over again?

share|improve this answer
    
See my addition to the question –  aireq Mar 26 '11 at 0:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.