brijesh pant
Xebia Engineering Blog
4 min readFeb 20, 2022

--

The coding standards are the heart of any tech organization. To keep the standards high you need to have common tooling across different projects. Also to ensure the same repetitive tasks are not duplicated in different projects, you need to set a standard template around the development that you foresee within the organization. these templates can be customized basis the requirements of the project. This can help in the following ways

1: You can start the project or new functionality in the existing project, right away without wasting the time writing the boilerplate code to initiate the new repositories or the modules

2: Instead of researching from a fresh, you can utilize the existing knowledge from other projects

3: Resource movement from one project to the other can be made easy. If the developer moving into a new project can see the same project structure, it will be easy for him/her to start contributing to the project goals instead of understanding the code structure

In this post, I will share some of my experiences of different projects with different clients and how we made our life easy by defining the templates and generating the code automatically from the given template

Use case1:

In one of my projects, we needed to build multiple microservices.

When a new microservice had to be created, each time we encountered the same problems.

every microservice requires a new module, with dependencies and configuration related to database, elastic search, external API, etc. also it required the Kubernetes configuration files(deployment, secretes, virtual service, etc.) for deployment to different environments.

Every time a new micro-service has to be created, our developers tend to duplicate the existing module and then modify the module according to their use cases. because of these copy and past thingies often the modules ended up having the useless and the duplicate code and the dependencies. Also while deploying the services it often required multiple fixes before a successful attempt. Essentially creation and minimal deployment of a new micro-service required at least a day or two and off-course followed by the actual development time for specific functionality.

Learning: We knew every new service requires mostly the same set of configurations, what if we could have automated tasks that can generate a minimal code from a template for a new module. So we thought of creating a template-based Gradle task, which can let us create a new service, with a dummy API with automatic deployable configuration.

We needed to have a spring boot-based module, with some varying dependencies. All of the micro-services comprised of one or more requirements such as connecting to DB, audit logging, updating documents on elastic, and connecting to external APIs.

How template could be of help:

We defined a Gradle-based task that runs the code to generate the modules. Based on the arguments, it can generate a new module.

We created different modules for generic configurations for audit logging, external API access, error handling, elastic connectivity, etc. In our Auto generator, we added the configuration to add the specific modules based on the requirements(passed as the argument )

In our template, we defined the structure of a module comprising all the code that was required to create a new service. all the dynamic parts such as file names, module names, deployment configuration, etc., we kept as the placeholder.

whenever we needed to create a new service, it was just away from the execution of a Gradle task. you could deploy it right away. This helps us reduce the dev effort for at least 1 day for each service implementation.

Use case2: In one of my other projects, we needed to develop the modules for micro-services, Kafka consumer, producer, AWS lambda, etc. We could encounter the same problem. But this time we did start with the same solution and already defined different templates for each of our use cases. based on the requirement the Gradle task could generate the different modules. As a developer you need to just implement the functionality of all the configurations, and set up was already taken care of by the templates.

Conclusion: Apart from the functional requirements there could be more quality check requirements in the projects such as architectural styles, code coverage, check styles, etc. if at the organization-level same practices have to be followed, it is certainly a good idea to have the template-based code generation. which can generate the code for any project within the organization.

In the next post, I will show you how we can create the code from the template.

stay tuned and happy coding…..

--

--