If you are anything like me then you’ve probably been working on code before and thought, I feel like I’ve done this before.  Writing software sometimes requires you to write similar code over and over again.  It can be tedious and sometimes boring.  Some of you might be thinking this is a good time to write an abstraction to keep your application DRY (do not repeat yourself), but sometimes you can’t or shouldn’t.  If writing an abstraction makes the code less clear or harder to work with then you are better off biting the bullet and repeating yourself.  This is where code generators come in handy.  Code generators are tools that write code for you.  It’s a super cool way to solve repetitive code problems that can’t be solved by writing an abstraction.  This may sound like writing code with extra steps, but we will explore all the benefits of using code generators.

Consistency

Consistency is key when writing code.  When you have many different developers working in a codebase or on a codebase for a long time, keeping the code consistent is very important.  You don’t want to open two files with similar purposes and have completely different coding styles in the file.  When the files look too different it requires developers to spend a non-trivial amount of time reading each of the files to understand the content.  This time will add up, especially the larger the application becomes.  Using code generators can help your team keep the code looking consistent across similar files.  Providing your team with a code generator for starting files of a similar purpose goes a long way in keeping everything consistent.  Some code generators will also provide mechanisms for modifying files.

Learning Tool for Junior Developers

The consistency that code generators provide is amazing for junior developers.  When you are bringing in new talent on a team, they will likely not share the opinions and knowledge of your battle-hardened senior developers.  Code generators can be used as a teaching tool for this new talent and show them precisely how to solve some of the repetitive task problems that you work on.  Allowing junior developers to use these generators and make minor alterations to them is a great way to start allowing new talent to have a big impact on the project with minimal oversight.  It can also help them learn the lessons that the senior developers had to learn the hard way.

Speed

Time is money.  It takes a long time to write a software solution to a complex problem and it can take an even longer time to acquire the knowledge required to do so. Instead of writing the code one time and using it only in that one spot, you can write a generator that writes the code for you, and then save time every time you need to solve that problem in the future.  Imagine that you write a code generator that could save you five minutes every time you run it.  And maybe that same generator would save your coworkers five minutes as well. That time savings is going to add up over time as huge cost savings and you are going to be able to launch solutions faster.  Some generators you can write might be so valuable you can use them in many different projects.

Subject Matter Expert in a Bottle

Not every developer is an expert in every language and framework.  Collectively a group of developers will have a lot of different skills and strengths.  Code generators have a unique ability to allow developers with less subject matter expertise to perform like rockstars.  In addition, you can use the generator as documentation for current best practices.  Updating your generator in one spot to account for the latest best practices is easier than trying to teach everyone the best practice on every different framework under the sun.  This allows your senior developers to collaborate once and become a huge force multiplier across the entire development team.

Caveats

There are a few caveats to using code generators.  You don’t want to try and solve every problem through a code generator. As previously stated, writing abstractions is sometimes a better idea than writing a code generator.  If you can write an abstraction and it is easy to read and work with then the abstraction is preferable over writing a code generator.  If you write a clear and easy to use abstraction you can maintain the code in one spot allowing you to make easier modifications to the application.  Changing the code in one spot is better than changing it in twenty spots regardless of how easy it is to generate the code.

Writing and debugging code generators generally takes a little bit more time than writing the code by hand.  For this reason, you want to be sure that the problem you are writing the code generator for is going to come up again.  If not, it would not be worth the time you are going to spend writing the code generator.  If you can only think of one place to use this code generator you may want to hold off on writing the generator until you know that it will be valuable in the future.  If you have to go back and write a generator at least you will have a template on how to start writing the generator in the future.

Getting Started

Writing code generators is kind of intimidating when you don’t know how to get started.  When I started writing code generators, I looked at a bunch of different libraries.  One of the more popular libraries I considered was Yeoman, but I found that it was way too complicated for what I needed.  Instead of going that route I chose Plop.js which is incredibly easy to work with.  Plop.js maintains the code for the generators in the same repository as the project that they will be used in.  This allows the team to easily make modifications to the generators.  They are in many ways a living piece of the software being developed.  Plop.js was designed to work on generating JavaScript, but you can write any language with Plop.js code generators because they use Handlebar template files to build any type of file you desire.  In addition to writing new files, it has an easy way to modify files using regular expressions.  I would highly recommend starting here when you are looking to introduce an easy code generation tool to your toolbox.

Now you know all about code generators and why you might want to use them.  At UDig we love exciting new technology so if you have more reasons to use code generators or you use a different framework to solve your code generation problems please let us know.  We love to hear about how the community likes to work!

You May Also Like These Topics