Your Privacy

This site uses cookies to enhance your browsing experience and deliver personalized content. By continuing to use this site, you consent to our use of cookies.
COOKIE POLICY

Benefits of Code Generators

Benefits of Code Generators
Back to insights

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!

 

Digging In

  • Digital Products

    Unlocking Business Potential: The Power of Custom Application Development

    Like any savvy business leader, you’re likely always on the lookout for tools to give your company a competitive edge. And in doing so, you’ve undoubtedly considered investing in custom application development. But the question is, how do you ensure that such a major investment in a custom web application development provides a strong return on […]

  • Digital Products

    Mastering Legacy Application Modernization: Strategies for Success

    The ironic truth of the business world is that change is the only constant. But this means that failing to keep pace with the competition and its technologies will only end with you falling behind. That’s where legacy application modernization enters the fold. When you modernize legacy applications, your team gains access to new features […]

  • Digital Products

    CTO Confessions Podcast

    In this episode of CTO Confessions, Rob Phillips, the Vice President of Software Engineering at UDig, digs into his journey from a passionate technologist in his youth to a seasoned leader in the tech industry. He shares valuable lessons on transitioning to senior leadership, the importance of understanding and articulating company problems, and the art of empowering teams for high performance.

  • Digital Products

    Navigating the Challenges of On Premise to Cloud Migration

    In today’s rapidly evolving technological landscape, the shift from on premise solutions to cloud-based infrastructure has become a pivotal transformation for organizations seeking to modernize their IT operations. This transition holds the promise of increased agility, cost savings, and enhanced scalability. However, it is not without its set of formidable challenges that organizations must navigate. […]

  • Digital Products

    The Power of Transferrable Skills in Tech Projects

    Every project has its own unique elements that require flexibility to be effective and achieve success. This often requires picking up new pieces of a tech stack, learning a new programming language, or a new project methodology. Fortunately, there are also many transferrable skills that carry over from one project to the next. In my […]

  • Digital Products

    The Four Pillars of Effective Digital Product Development

    In 2020 alone, approximately two billion consumers purchased at least one digital product. From software licenses to mobile apps and tech tools, consumers are becoming increasingly active in the digital product market, a trend that has naturally spurred brands across a wide range of industries to reevaluate their digital product design and development process workflows. […]