Generated Code Quality

time to read 2 min | 241 words

Jeremy Miller made the mistake of opening the DataSet.Designer.cs file and got:

You know the orange bars that ReSharper puts into the vertical scroll bar to denote warnings in the code?  In the DataSet code it looked like a solid bar of orange sherbert. 

The discussion in the comments is about the quality of generated code, and it is fairly interesting. As someone who does quite a bit with code generation, I wanted to note two things:

  • The quality of the generated code is the result of the generator, with the qualification that the generated code may be used to off load nasty parts of the code base. The code generated by NHQG is doing some really crazy stuff with generics and operator overloading, and it is something that can get very hairy very fast.
  • The other issue is that the use of CodeDOM means that you are often specifying redundant things. In this case, this this.myName and the fully qualified namespaces could go away, and this is something that ReSharper detects and warns against.
public virtual Query_Post<T1> With() {
	Query_Post<T1> query = new Query_Post<T1>(this, this.myName, this.associationPath);
	query.joinType = NHibernate.SqlCommand.JoinType.InnerJoin;
	query.fetchMode = NHibernate.FetchMode.Default;
	return query;
}

It is not code generation in general that is problematic, in my view, it is the use of code generation to cover problems with the underlying platform that is usually what is causing the pain.