“There is no poem as lovely as a tree” New opinionated formatter for Bicep templates!

Freek Berson
4 min readJul 1, 2023

Using the same programming language style guide within your team, department, or company is great! It simplifies team efforts to work on the same code and contributes to reusability of code. This applies to any programming language, scripting language and Infrastructure as Code language such as Bicep for Azure.

As great as that is, the challenge with style guides is reaching consensus on a common style to use. Trying to read code written by someone who is using a different style simply slows you down.

From the start, Bicep has made authoring, reading, and digesting infrastructure as code templates much easier compared to ARM templates based on JSON.

The new Bicep formatter option in VSCode, currently into experimental phase, allows opinionated formatting in a super easy way. Let’s dive into this upcoming feature and take an early look at what’s coming soon!

Since the prettyPrinting feature is currently experimental phase, add ‘prettyPrinting’ to the experimentalFeaturesEnabled section in your bicepconfig.json file. In case you’re new to using this config file, follow this guide.

Next, add a new section called formatting. This is allows you to configure your personal preferences regarding Bicep template formatting. For example, note that we set the maximum length to 80, a common maximum character length per line of code.

I my case I also used a preview build of the VSCode extension version 0.18.97. Follow this guide for more information on where to get early Bicep releases.

Lets look at a first example. Note the vertical line in the screenshot below, this is just a visual indicator you can use in VSCode to show where the maximum length of, in our case 80 characters, is situated. You can configure this by adding the below line to your settings.json file: “editor.rulers”: [80]

Consider the following part of a Bicep Template below showing a parameters, and variables section. The variable ‘short’ is clearly not exceeding the 80 characters, the variable ‘long’ is just below 80 characters, the variable ‘toolong’ is just over the 80 characters and the variable ‘waytoolong’ is of course way over our preferred and configured limit.

To format the code based on the 80 character limit we configured in our bicepconfig.json file, simply use the existing formatting option in VSCode you probably already knew (or use the Shift+Alt+F shortcut).

Boom! The variables are now properly trimmed to multiple lines, respecting the 80 character limit we defined! And someone else who prefers any other number of character limit can simply configure and use that in his preferences as well.

Another example is the resource declaration below. It uses a condition and as a result it exceeds our 80 character limit preference.

Simply hit format document option again, and below is the result. The conditional statement is now nicely placed on a new line, respecting the 80 character limit.

Using Bicep modules and loops is another common example resulting in long lines of code. Consider the example below where we use a Bicep module stored locally and use a for statement that loops through an array. Line 36 clearly exceeds our defined limit.

Hitting format document again results in a nicely formatted and easy to read module declaration!

This concludes our tour of the upcoming Bicep formatting feature allowing opinionated formatting based on ‘a pretty printer’.

Looking to get started with Bicep? I authored and published the book Getting started with Bicep: Infrastructure as code on Azure

The quote in the headline came from this source, a great paper by Philip Wadler I can really recommend to read: https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf

--

--

Freek Berson

Empowering hybrid and remote workforces with a digital workspace from any device, anywhere. EUC Enthusiast, Microsoft MVP, 🎤 public speaker, 📚 Author.