How To Code Generation For Any Programming Language

Umut Boz
KoçSistem
Published in
5 min readNov 10, 2021

How can we generate code any programming language? You can find the answer to this question in this article. In this study, the methods of how we can do it will be explained step by step.

Sometimes when developing code, we want to speed up the development processes. Sometimes we do repetitive tasks. To follow a rule pattern we may have to use some patterns such as application development architectures, quality rules and various disciplines etc. Let’s Look at What We Did During Development.

  • New business processes
  • Infrastructure codes
  • Technology dependencies
  • Codes of the libraries used
  • User Interface developments / User Interface Codes / User Interface Structures,
  • Reporting, data analysis

Generally, the works we do while developing applications fall into these categories. Which of these categories can I shorten or speed up?

New business processes

The probability here is lowest. New business can be developed mostly with customer demands. In this case, the code you write may change most of the time, except in some cases. In these conditions, code duplication is for the same project level. However, if you are doing more than one project, you can use these improvements in other projects. Although This not 100% Effective. We can adapt it by changing it in other projects such as login, authentication scenario, add cart etc. You may not always be able to use the same technology. things can change as there is customer influence involved.

Infrastructure codes & Technology Dependencies

These code and structures are often repetitive. While developing a project, we often repeat it to comply with some rules. Templates can be used here for repetitive work. Or you can create these templates with code generation.

User Interface developments / User Interface Codes / User Interface Structures

In general, these businesses also have a customer effect. A basic structure is often the same. For instance, list screens are available in every application at mobile app or web app. The customer wants the card to look different. Most of the time it’s unique, yes. But still still a list screen :) 📲 Data source may change, UI may change. We can start with a basic list understanding. You change the variables within the application according to your application. Similarly, you can create these templates for yourself with code generation.

Reporting, Data analysis

The data is different, but the tools used vary according to the needs, but they are often the same. Instead of copying the solutions you have done in different places, you can create a general solution document such as classification project, regression script etc. In these scripts, you can prepare step-by-step templates for processes such as data extraction, data manipulation process, standard used graphics, data analysis, data prepare, estimation. You can start with these generated scripts instead of copying and moving them.

🧑‍🦯 Actually you can use just a light when you are entering the tunnel. All the things I mentioned above were to raise awareness. Now let’s talk about how to do this 🏁.

Mustache

Mustache is described as a “logic-less” system because it lacks any explicit control flow statements, like if and else conditionals or for loops; however, both looping and conditional evaluation can be achieved using section tags processing lists and lambdas.

We will use basic default mustache files for code generation. If there are dynamic values to change, we will put it between {{in_here}}.

class A{
function show({{param}}){
.... any codes
}
}

The above work makes no sense. Illustrated to understand brackets {{ param}}.

The code can be generated by sending the {{param}} value dynamically with the code, or you can send it as a fixed value. How so!🤔

Let’s take a closer look at🥸

This is an android activity class prepared for data listing. Let’s think of it as a prepared mustache file called list_activity_mustache.

{{package_name}} = “com.myapp”, {{activity_name}} = “ListActivity”

I can create this ready-made code by changing the package field in my own application. We can use more than one of these mustache {{}} ‘s. Creating these mustache files is a good job of analysis and editing. It is necessary to foresee changeable structures and possibilities.🧙‍♂️

How

I have developed a library written in python programming language that uses these mustache structures. The goal is to easily implement these mustache structures and provide dynamic code generation.

You can easily install this package via the pip installer.

pip install codegenlib

Basic Example

Let’s perform a simple example using the mustache file we looked at above.

TemplateFile Object

We statically change the fields in the mustache file to the dict property in the TemplateFile object.

We define the file that we will generate with the output property.

The name property defines which is mustache file.

TemplateModule Object

The name property defines where is folder of mustache file(s).

The mustache_folder parameter shows name property of parent folder.

the project file structure:

root/
├── code_example.py #running script
├── output/#output folder

└── list_activity.kt #generated file
├── modules/android-kotlin-list/

└── list_activity_mustache

The template_files parameter takes files to generate.

generated output:

Let’s improve our example

Example V2

Our files, consisting of activity class and interface xml file, adapter class that binds the data, and list row item xml file and generated with parameters from outside. You can see these files below.

Now let’s look our script that generates these codes.

We can programmatically send the keys we use in the mustache file. We can send it according to the any loop or any rule set.

On the other hand you can reference another mustache file within a mustache file. Or with the content property, you can programmatically send the content dynamically without using a mustache file.

class TemplateFile(Base):    name = ""    outputFile = ""    dict = None    isChildTemplate = False    parentMustache = ""   childTemplateFiles = []   content = ""

For more detailed information, you can review the codegenlib project.

With the json module in the codegenlib library, you can simply generate code with less python code.

➡️ For this sample study, I share it here

As a result, whichever file extension you want to give, you will generate code for that programming language. You can design your own solutions.

In this way, you will be able to perform tasks that you repeat frequently. Relief from code dependency occurs. And you will have the opportunity to devote more time to business processes.

🛎️ Please, Send your designed solutions as pull requests in codegenlib 🚀

🚧We can multiply samples. Let’s leave it here for now👋

Resources

--

--