Wednesday, October 03, 2007

Tool to generate code on C#

While reading in "Pragmatic programmer" chapter about automation I've formed an idea of creating a tool for C# which will reduce amount of manual typed code and as a result help to avoid errors.

Now I've got only one idea for this tool, but maybe in future I've got some more. So that's an idea? In many cases I've needed to create a public property and private field to keep value of property. In some cases I also need to create an event which will happen when value of property is changed. It is a lot of similar typing work. Of course it can be made easier by Visual Studio snippets and I've used them, but this still leaves much work during maintenance:
  • while renaming I need to rename field, property and if exists event. Of course refactoring capabilities of IDE free me from necessity to search of uses of this names, but I still have to rename 2-3 elements of class.
  • I have to have separate snippets to cases when I don't want to have an event, or want to make set private. Also when this details are changed during refactoring it is needed to make them by hand.
  • It takes a lot of locs. If it be possible to write all of necessary data in one line it will be much better to read and understand.
So I've written a tool on Python which eliminates these problems. It parsers file and search for special marker in commentaries. When it founds it, tool reads options and replaces this code with a valid C# code. Lets look at example:

public event EventHandler DoubleFieldChanged;
private double _doubleField;
public double DoubleField {
get { return _doubleField; }
set {
this._doubleField = value;
if ( this.DoubleFieldChanged != null )
this.DoubleFieldChanged(this, new EventArgs());
}
}

Is an original code on C#. Most of these tokens are just speaking what is already said by others. It can be replaced by just one line:

    // @Field double Double Field

Note that name is written not as identifier but as a casual name. My tool automatically forms valid identifiers for field, property and event. And if field is renamed all identifiers are changed by program not by programmer. Moreover 10 lines are replaced by 1! Of course not in all cases such tool can give so good results.

Moreover my tool already can read some parameters of created field. Want to have set be private? No problem:

    // @Field double Double Field( private set )

And tool will make the rest. The same way I can command it not to generate event (and of course not to call it!). Of course both options can be combined together.

My tool (arrrhhh... I call it csgen!) also provides basic customization to format of output identifiers. But now csgen couldn't make complex generations. In some cases it is needed to manually create all of this code on C#.

Though I nave some ideas of making csgen more useful. For example if set needs to check incoming value. Just add to description something like: // @Field: set requires Checker.IsValid(value) and throw an ArgumentException in case of invalid value. To implement this feature I csgen will need all text after requires and paste it into if condition. Moreover in such way can be implemented some other features.

But there will be a problem: compiler will point to errors in generated code and show its line numbers. So programmer have to read faulty code in generated file and then match it with content of original file and fix errors there. Not very good.

However to finish on good news I have to say that tried to implement this feature as a 'custom tool' in Visual Studio. I'll soon write post about it, but I can say that succeeded to write module that returns IDE text and it works with it!

No comments: