Estimated time: 4 minutes read

Formatting and linting Solidity source code are essential to ensure quality and security in Smart Contracts. Tools like Prettier and Solhint enable us to apply best practices in style, gas optimization, and error detection in our Blockchain developments

What does formatting and linting Solidity code involve?

Formatting Solidity source code involves effectively applying a set of style rules considered useful within a community of developers. It is one of the key pillars I include in my article on best practices in Solidity.

Linting involves the static analysis of source code to identify errors, bad practices, or deviations from established programming standards, helping to improve the quality, security, and consistency of the code.

Performing both tasks is particularly important in Smart Contract programming due to their immutable nature and the inherent security risks associated with managing digital assets.

Formatting Solidity code with Prettier

Prettier is one of the most widely used code formatters due to its compatibility with numerous programming languages.

It is a tool that automatically rewrites the source code by applying a set of predefined rules.

Support for the Solidity language is provided through the prettier-plugin-solidity plugin. This extension applies specific rules in line with the Solidity style guide.

Installation also requires the generic Prettier package:

yarn add --dev prettier prettier-plugin-solidity

To use it properly, it’s recommended to create a configuration file ‘.prettierrc.js’ in which you enable the use of the plugin and adjust some of Prettier’s default rules to align with Solidity’s style conventions:

module.exports = {
  
    // Adding the plugin to ensure Solidity files are handled
    plugins: ['prettier-plugin-solidity'],
  
    // File-specific configuration using "overrides"
    overrides: [      
      // Solidity-specific formatting rules
      {
        files: '*.sol',
        options: {
          printWidth: 120, // Line width for Solidity files
          singleQuote: false,
          tabWidth: 4, // Indentation size for Solidity files
          useTabs: false,
          parser: 'solidity-parse', // Ensuring the Solidity parser is used
          bracketSpacing: false,
        },
      },
    ],
  };

This reference outlines the set of rules automatically applied by the Solidity plugin.

The basic usage is very simple:

yarn prettier --write --plugin=prettier-plugin-solidity 'contracts/**/*.sol'

With this command, the project’s contracts will be automatically overwritten according to the rules configured in the tool. It’s important to maintain some form of version control to recover the previous code if necessary.

Linting Solidity code with Solhint

Solhint is an open-source Solidity linter that allows you to configure a set of rules to validate compliance in the Smart Contracts of interest.

The rules are classified into five sections and have been programmed based on the criteria and demands of the Solidity developer community: best practices, style guide, gas consumption, miscellaneous, and security.

Although there is a default configuration with the recommended rules, it is always possible to customize it according to the needs of each case. For example, the following ‘.solhint.json‘ configuration file modifies the recommended rule set with some changes and extends it with additional rules:

{
  "extends": "solhint:recommended",
  "rules": {
    "func-visibility": ["warn",{"ignoreConstructors":true}],
    "max-states-count": ["off"],
    "func-param-name-mixedcase": "warn",    
    "private-vars-leading-underscore": ["warn", { "strict": false }],
    "ordering": "warn",
    "modifier-name-mixedcase": "warn",
    "named-parameters-mapping": "warn",
    "gas-calldata-parameters": "warn",
    "gas-increment-by-one": "warn",
    "gas-indexed-events": "warn",
    "gas-length-in-loops": "warn",
    "gas-named-return-values": "warn",
    "gas-small-strings": "warn",
    "gas-struct-packing": "warn"
  }
}

To check the rules, the command can be invoked as follows:

yarn solhint 'contracts/**/*.sol'

Another alternative is to integrate Solhint directly into Hardhat. To do this, you need to install the ‘@nomiclabs/hardhat-solhint‘ plugin, import it into the ‘hardhat.config.js’ file, and run the following command:

yarn hardhat check

By default, the tool only notifies in the console about the lines of code that violate any of the rules, with the configured severity level.

However, for certain rules, it’s possible to automatically fix the code using the ‘- -fix’ modifier.

Additionally, it’s possible to disable rule checks in specific parts of the source code or even in entire files. This can be done using directives in the form of comments within the code.

Conclusion: Automating adherence to best practices

Smart Contract programming in Solidity is a discipline that involves special complexity due to the execution context within the EVM and the type of “immutable” logic at play.

For this reason, it’s particularly important to adhere to a set of best practices and rules that have been established over time based on the experience of the developer community.

Two of the first requirements that should be met are proper formatting of the source code and compliance with basic security, style, and optimization rules in the developed Smart Contracts.

To facilitate this task, two tools, Prettier and Solhint, have been introduced, both widely used in the community and frequently updated.


Do you use any other tools to format and ensure best practices in Solidity programming? Share your experiences.

If you need advice on how to approach a Blockchain project, I can help. Contact me and let’s talk.