Using Formatters to Improve Maintainability

June 07, 2023#Software Development
Article
Author image.

Sarah Dutkiewicz, Senior Trainer

Have you ever looked at code and felt overwhelmed by the inconsistencies in curly brace placement? Have you felt exhausted trying to follow the inconsistent casing in variable names because your code is maintained by multiple developers with different preferences? Have you found it distracting when code is checked in and not aligned in a readable manner? Formatters can help make your code more consistent and improve code readability.

What is a formatter?

Formatters are used to enforce code styles without changing the runtime behavior. They are used specifically in the layout of your code, looking at styles such as:

  • indentation style
  • newline placement
  • curly brace placement
  • parentheses placement
  • variable casing
  • semicolons
  • comments
  • order of using statements

Examples of formatters include:

Why use formatters

Formatters enable you to enforce consistent style throughout your code base. This is especially critical when multiple developers are working in the code and may each have different preferences on code formatting. A shared formatter configuration file can help keep a team of devs consistent across their code.

A benefit of using a code formatter means that you can write the code in your style and let the code formatter clean up your layout when you save your changes. This can happen through webhooks, built into an IDE, or through other extensibility options such as IDE plugins.

The consistency that comes with formatters can also lead to better readability. When there is consistent code layout, it’s easier to notice the exceptions and outliers. Readable code leads to fewer barriers for comprehending code, making it easier to maintain.

Troubles that can arise with formatters

Formatters can help with consistent style throughout code. However, they do have their limitations and pain points as well. They are not tools that analyze for vulnerabilities or performance - formatters are specifically focused on code style. They can be problematic if developers on the same code are using different configurations - though this can be alleviated with a shared configuration file. However, this can lead to some heated conversations if teammates cannot agree on code styles. Formatters may not work for all development teams. They do work well when a team can agree on a code style and trust their tools to help them maintain their style.

How to run formatters

There are many ways to run formatters as part of your development environment. Some options for running formatters include:

  • Configure formatters in your IDE
  • Configure a formatter command to trigger locally as part of a pre-commit Git Hook
  • Run formatting as part of CI/CD

Visual Studio formatting

These are guides for setting up code formatting within Visual Studio:

Visual Studio Code formatting

These are guides for setting code formatting in Visual Studio Code:

Other IDEs

These are some other common IDEs and how to work with formatters in them:

Formatters vs linters

When looking into formatting, you may also see the term linter. Linters and formatters go well together at improving consistency throughout your code. Both analyze code and make suggestions to improve it. Where formatters look at style and can make the changes to the format, linters can assist with enforcing style consistency and preventing code that doesn’t meet the consistency from getting checked into a development branch. Linters can also be used for things such as:

  • Catch security issues
  • Detect bugs
  • Find deferencing null issues
  • Identify other warning and error situations in code
  • Call out code smells

Improve your code in your CI/CD pipeline

We are running a live webinar “Real World CI/CD Tips & Tricks to Boost Your App’s Quality” on June 27, 2023 at 1:00pm US Eastern Time. If you want to learn how to incorporate linters in your CI/CD pipeline to boost your code’s readability and maintainability and improve your app’s quality, sign up today for the webinar. Seats are limited so register before it fills up!


Copyright © 2024 NimblePros - All Rights Reserved