Code Generation to Reduce Software Costs

I have developed software for many years now and am surprised at just how much money companies spend on writing and maintaining code that would be more reliable, lest costly, and more easily maintained if code generation tools were used instead. As an American developer the best way for us to compete on cost is to think smarter unless we want to work every waking minute for less money, and die young from stress related disease. That being said, code generation requires planning and careful thought because there is a balance that must be maintained lest you incur technical debt in the form of future support costs.

If your code generation strategy does not accomplish the following objectives, you have it wrong:

  1. Code generation should reduce cost of development
  2. Code generation should reduce time of development
  3. Code generation should increase quality and reliability of code

When some people think of code generation they immediately think of software that produces the User Interface tier of an application based on the schema in a database. Although I am a prolific user of code generation, rarely will I use code generation in the UI tier (aside from ASP.NET MVC which is technically generating HTML for you, or maintenance screens). The vast majority code generation that I implement is for what I call “plumbing code”. A good candidate for this type of code generation has the following qualities in my opinion:

  1. At least 90% of the time the code should follow the same repeatable pattern
  2. It requires little or no custom business logic, and little thought on behalf of the developer
  3. It is part of an architecture that you don’t want to be violated (such as bypassing layers)
  4. If it is in the UI tier, it is for maintenance or administrative views where usability is not a concern
  5. Other approaches wouldn’t solve¬†your problem better

Where are some ideal places in your application to leverage code generation and who should maintain the code generation templates?

In many cases you will leverage code generation tools created for you, often in your data access layer to generate entities or code that maps between entities and your database. Entity Framework and nHibernate are good examples of this. If there is an industry accepted approach developed by a company responsible for maintaining the template, you will want to leverage their solution as often as possible and limit customization of their provided templates. That way when you are ready to update to a newer release, the cost of labor is minimal.

Other areas that I focus on are the “edges” of an application where code does little more than act as a traffic cop. I make it easy to override generated code in partial classes so that in the rare event you must deviate from the default pattern you are able to do so. Once you learn how to leverage code generation tools effectively, you will want to abuse this power (which I admittedly have done in the past) so be selective. In short, don’t reinvent the wheel here, and don’t believe the extremists on either side who say that all code generation is bad or who say that everything should be generated!

My final piece of advice is that unless your business is creating code generation tools that you not create the tooling itself and instead purchase a code generation framework. Your job should be adding business value for your customers, not creating a management nightmare for them for years to come. CodeSmith is a great platform that I had used for years, while today I tend to use T4 because of the built-in support with Visual Studio (plus it is free and easily shared between developers). I suspect that Razor syntax will eventually replace T4, which is great because using MVC and Razor for code generation would be far easier and more maintainable than leveraging T4 (which is a template language similar to classic ASP.NET).

If your company is interested in custom application development that is well architected and efficiently implemented, or if you are a developer with questions or ideas related to code generation, please submit a request for a free consultation or contact us direct at http://www.SDSElite.com.

Posted in Uncategorized.