Of all the applications I have worked on over the years a code generator is the most challenging.  Likewise, it is the most rewarding and painful sort of project.  This is not something for the faint of heart.  However, it is an incredible learning experience and often will help elevate you to that next level.

Reinforce Best Practices

Do not misunderstand me; a code generator can create some ugly code.  However, building an application that creates source will often push you to best practices.  Your head will explode if you try to debug sloppy code.  Thus, the time you spend in formatting the generated source and making it easy for a human to read will be a good investment.

There is also the end-user to keep in mind.  Whether you are generating source for personal use or as part of a commercial application, it pays to make that code top-notch.  There is no sense in building an application that spits out source code you would be embarrassed to share, or that will immediately fail a code review.

The Incremental Approach

The concept of an application building an application can be overwhelming.  Instead of trying to tackle the code generator features as a whole, take it a piece at a time.  You will have some simple tasks early on that can help you get used to the idea of generating code.  This simplicity is often tasks like creating a formatted template or header file.  The dynamic portions of that output typically are minimal and amount to little more than placing some strings in the right place.

These early steps are often a perfect way to define and test your approach to code generation.  You might try to build everything on the fly, or it might be okay to fill in some template files.  These are not mutually exclusive approaches so you may find the first steps in splitting out the pieces that can be a snippet or template from those that need to be entirely generated.

More Common Than You Think

Full-blown application generating applications are typically vast and enterprise-grade solutions.  On the other hand, we can find needs to create code (or something like it) on a regular basis.  This may be dynamic SQL for a database function, a configuration file generator, or even small tasks like a moderately intelligent log writer.  Generating content that will be validated by a parser or compiler is one of the more daunting coding challenges.  Nevertheless, it is one we must overcome to become a better developer.

Rob Broadhead

Rob is a founder of, and frequent contributor to, Develpreneur. This includes the Building Better Developers podcast. He is also a lifetime learner as a developer, designer, and manager of software solutions. Rob is the founder of RB Consulting and has managed to author a book about his family experiences and a few about becoming a better developer. In his free time, he stays busy raising five children (although they have grown into adults). When he has a chance to breathe, he is on the ice playing hockey to relax or working on his ballroom dance skills.

Leave a Reply