Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Numerous tools exists to automatically format code such that it follows a certain style. Automatic formatting enables higher code quality, especially when you are collaborating in a team and other people need to look at the code you’ve written. Many developers and organisations maintain standards of code formatting like 2-space or 4-space indentation. Using these is highly recommended since the probability of finding bugs (if any) increases multifold.

EditorConfig is a language independent tool that helps maintain consistent whitespace styles for multiple people working on the same project across various editors. Most editors support EditorConfig either natively or through a plugin. Almost all widely used IDEs and text-editors support automatic code formatting upon typing. For example: JetBrains IDE Suite and VSCode.

In addition to that, there are many language specific tools for automatically formatting code according to a particular style. Note that editors often support using these tools directly from the editing environment.

LanguageFormatter Tool
C/C++GNUIndent, GreatCode
PythonBlack, yapf
Javascriptbeautifier.io
JavaGoogle Java format, JIndent
MATLAB/OctaveMISS_HIT
PHPphpStylist
PerlPerlTidy
RformatR
Shell/BashShellIndent
CSSCSSTidy
HTMLTidy

Quick Tip: If you use VS Code as your primary text editor, you can enable automatic code formatting right into your browser. Open your preferences page in JSON mode and add the following line:

"editor.formatOnSave": true,

Linting Tools

Linting tools, also known as linters, analyze your code to find potential errors, style violations, and other issues without running the code. These tools help maintain code quality and consistency across your projects.

lintr (R)

lintr is an R package that checks your code using a variety of style guidelines. It can be installed from CRAN. The function lint takes a filename as an argument and a list of ‘linters’ that it should check your code against. These range from whitespace conventions to checking that curly brackets do not have their own lines. The output provides a list of markers with recommendations for changing the formatting of your code line-by-line, meaning it is best used early and often in your project.

lintr output showing recommendations to add space, remove commented code, remove training whitespace, have character size per line less than 80 where needed in the input code.

Figure 1:An example of how the lintr output may look like for an input file with R code.

For more details, please visit the GitHub repository.

Autopep8 (Python)

Autopep8 is a Python module that can be run from the terminal and automatically formats a file to pycodestyle (formerly called pep8) guidelines.
It is available on pypy and can be installed using pip.

# Install autopep8
pip install --upgrade autopep8

You can modify a file in place by running the following command:

autopep8 --in-place --aggressive --aggressive <filename>

To some extent, the module can also be used on R scripts!

Black (Python)

Black is an auto-formatting package for Python. This means that it will automatically change your code to adhere to certain guidelines, like spaces around operators and removing unnecessary whitespace. It is also consistent, so that the code that you and your collaborators work on will look the same once black formats it. It does not change what the code does. This can reduce the time spent making the above changes to the code.

Static Code Analysis Tools

Static code analysis tools examine code and detect software vulnerabilities before your code is executed or the project is built and deployed. In addition to finding bugs, many of these tools can also help maintain a consistent coding style.

Some of the most widely used static analysis tools are mentioned in the table below:

LanguageStatic code analysis tool
C/C++Cppcheck, cpplint
PythonPylint, prospector
JavascriptESLint, JSlint, JSHint
JavaCheckstyle, FindBugs, PMD
PerlPerlTidy
Rlintr
Shell/Bashshellcheck

Online Services Providing Software Quality Checks

There are several web services that analyse code and make the quality of the code visible. Usually these services run one or more static code analysis tools that can also be used from the command line or integrated into your editor on your own computer. Using a code quality service that integrates with a GitHub/GitLab repository is highly recommended, as it can detect and communicate quality issues in pull requests.

Code quality analysis services are websites that often offer the following features:

For a list of choices see shields.io or this list of services that are free for open source projects.