OPTIMIZE YOUR DEVELOPMENT WORKFLOW

The Power of Custom File Templates: Simplify Development and Boost Efficiency

Streamline Your Development Workflow: Boost Productivity with Custom File Templates and Naming Conventions

Marc Picone
by MWM

--

[Update 22/03/2023] -> Crafting Custom Path Formatting in IntelliJ File Templates for Android Development

Hi, as developers, we all want to write clean, maintainable code that is easy to understand and modify.

However, we often find ourselves spending more time creating boilerplate code than actually solving problems.

In this article, I’ll share some tips and tricks that have helped me streamline my workflow and create more efficient code. By using templates and custom file naming conventions, you can save time and reduce errors while keeping your code organised and consistent.

Whether you’re a seasoned pro or just starting out, these tips can help make your development process smoother and more enjoyable.

We’ll explore a simple and efficient approach to generating files in a flat architecture.

External resources:

2-How to Use File Templates and Velocity Scripts

In Intellij IDE, you can access the Preferences window and search for “file templates” in the search bar to find the file templates section.

In this section, the “Files” tab is used to implement the file templates, while the “Include” tab is used to store reusable code snippets using the Velocity template language’s #parse directive.

In my case, I created two Velocity scripts for my project. The first one, named “SnakeCaseToCamelCase,” helps to change the feature name from snake case to camel case.

The script is as follows:

The second script, named “ResPath,” retrieves the “Res” folder to create the related layout. It is a bit more complex and involves several Velocity template language features:

3-Files and Folders.

So, with this let’s create the simplest part. In the “Files” tab, just add a new file template. The Name is to retrieve the template when it will be created, for me it will be “CustomView”, the extension could be “java”, “xml”, or others, for me “kt”, and finally the file name give the name and here the package name of the files.

As the first file I want to create is the View one, the first file name looks like this:

This will be the parent file and will create a folder at the root of the selected file. Here the “$NAME” stands for the feature name. This file will be the opened one when creating multiple file templates, so if you are interested in opening another one on creating the file template, pass the selected one as parent.

In order to have the good package name, just add “${NAME}” at the end of the top line.

You could fill the template with whatever you want as a common part.

Just as example, mine look like this, I love the way the “#parse” is used to auto fill the class name and more:

Now that we have the parent file, let’s create the Presenter file template. On the CustomView file template, add a new child template. Here I just have to fill the file extension, always “kt” for me, and the file path. as I want the presenter to be created in the same folder that the view, I enter the following:

Like this, as the folder already exists, it just creates the file with the good name. Simply hardcode the extra “Presenter” information at the end of the parse directive.

And last the Contract the same way, I create a new child template and pass the following in the file name:

Now the last file to create is the layout. I added a new child template and set extension to “xml”. As we already have the ResPath script, we just have to fill the file name with the following:

And that’s all. Now we could create the three files and their folder plus the layout folder and file by just naming the feature name in snake case, and that’s pretty useful!

3-Bonus.

As I use the same name convention for every new file in a package, I could use the $PACKAGE_NAME to retrieve the feature name and change it in camel case.

In the include tab I add a new file template named FolderToCamelCase.

The only thing I have to do here, is to split the package name on dot, loop on the created array and on the last iteration, I am on the feature name.

So my script for this is the following:

If I have to create a repository with this naming convention, I could create a file template and fill the file name like this:

`#parse(“FolderToCamelCase.kt”)Repository`

With this when you create a new file from an existing folder you no longer need to fill nothing, the IDE makes it for you. Coool!

And of course we could create multiple files at the same time with the help of child templates.

I hope the time spent to read this article was useful for you and that you already know what you will template soon!!

Have a nice day!

Enjoy, explore and keep coding !💪

--

--

Marc Picone
by MWM
Writer for

Hello! I'm Marc, Android developer at MWM. I've been creating innovative music, drawing, and creative apps with MWM since 2021. Let's connect and exchange ideas