blog

Developing microservices faster with code auto-generation

4 min read

Last edited:  

Developing microservices faster with code auto-generation

Microservices are Bringing Benefits Forward

Microservices are revolutionizing software development, providing lightning-fast speed and unparalleled clarity. By segmenting a system into discrete components with their own individual responsibilities, developers can enjoy clean code that’s easy to maintain - all while developing projects faster than ever before!

A microservice architecture is a helpful foundation for many businesses. However, growing business capabilities can lead to redundant code cropping up between services. To avoid this and ensure optimal efficiency of resources, we must take a smart approach with initially building out the logic.

To create code for multiple services, which would otherwise have taken a great amount of manual creation and considerable tedious labor, we had to take an alternative approach since the Go Language (Golang) lacked generics; the solution was auto-generating boilerplate codes.

No Need for Manual Building

You don’t have to build Golang structs manually, you can automate the process with proper definitions in a YAML/JSON file. With that, we could easily generate them as needed.From this point forward, let’s refer to the YAML/JSON file as our “definition file”.

This definition file sets forth the intricate details of an object and its parent, if applicable. A script then parses this document to create personalized templates for subsequent files using precise values from said definitions.

The ORM’s auto-generating capabilities create structs and functions for various proto messages. These will form the requests and responses of multiple RPC interfaces.

RPC definitions enable automatic generation of Open API spec files, leading to the creation of helper methods for basic CRUD APIs. We can use these specs as a foundation for generating MongoDB query language (MQL) filters on specific fields in the definition file, enabling powerful data querying capabilities!

Auto-Generating Makes Things Easier

As you can see, auto-generating code makes introducing new objects or adding a new field to an existing object very easy, where one has to just update a specification file to generate all the boilerplate code required.

It is also easy to introduce new specifications in the definition file, which helps us to determine different attributes on fields.

With just a few keystrokes, you can enable filtering on any desired field. Simply update the definition file to set the filterable flag and all the code necessary for the applied filters is automatically generated!

What Can Golang Do?

Golang generics provide a powerful tool to simplify code, but they do not eliminate the need for automated struct and proto message generation. Without it, we leave developers to write lengthy boilerplate code by hand. Instead, leveraging automation can drastically speed up development cycles!

With the power of auto-generation, developers can now benefit from similar service structures across different microservices. This creates an efficient working environment for simultaneous services.

The Cons of Auto-Generation

Like everything in development, auto-generating code comes with its own set of problems.

A significant challenge can arise when custom logic is required to be embedded in auto-generated functions. We can easily clog templates with complicated conditions depending on the specific object or service name.
Automated code-generation can create a complex environment for maintainability, with multi language components that make it difficult to onboard new developers.

What Next to do with Auto-Generation

Developers have long been struggling with code maintainability as project size and complexity grew. However, by breaking a system down into smaller, independent components, we can develop projects faster while still enjoying clean code that’s easy to read and maintain.
This is where DevRev comes in - our platform enables developers to segment their systems into discrete components for more efficiency and scalability.

So if you’re looking for an auto-generation solution that will help you enjoy cleaner code and faster development times, consider using DevRev!

Kartikey Kant
Kartikey KantMember of Technical Staff

Member of Technical Staff