Ballerina Style Guide

The Ballerina Style Guide aims at maintaining a standard coding style among the Ballerina community. Therefore, the code formatting tools are based on this guide.

You can follow your own coding style when writing Ballerina source code. Also, plugins and tools can be configured to match your coding style.

Indentation and line length

  • Use four spaces (not tabs) for each level of indentation.
  • Keep the maximum length of a line to 120 characters.

Note: You can configure tools and plugins to use tabs when indenting and to change the number of maximum characters of the line length.

Line spacing

  • Use only a single space to separate keywords, types, and identifiers.

    Do's

    Don'ts

    Few exceptions for this rule are:

    • Do not keep spaces around a type when it is enclosed using angle brackets <string>.

      Example,

    • Do not keep spaces between the type and the opening bracket in the array definition string[].

      Example,

  • If it is a list of values separated by commas, add only a single space after each comma and don't add spaces before the comma.

    Example,

Blank lines

Separate both statements and top level definitions by zero or one blank lines.

Example,

Note: You can configure tools and plugins to change the number of minimum and maximum blank lines used when formatting.

Blocks

  • Opening curly braces of a block should be placed inline.

    Do's

    Don'ts

  • Add a single space before the opening curly braces.

    Example,

  • If a block is empty, do not keep spaces in between the opening and closing braces.

    Example,

  • Indent all the statements inside a block to be at the same level.

  • Indent the closing brace of a block to align it with the starting position of the block statement.

    Example,

Parentheses

  • Do not have spaces after opening parentheses and before closing parentheses.

    Example,

  • To define an empty parentheses, do not keep spaces between the opening and closing parentheses ().

    Example,

Line breaks

  • Have only one statement in a line.

  • When splitting lines, which contains operator(s), split them right before an operator.

    Example,

  • When splitting lines, which contains separator(s), split them right before a separator.

    Example,

  • If there isn't any operator or separator to break the line from, move the whole expression to a new line.

    Example,

  • If a line exceeds the max line length, start from the end of the line and come towards the start of the line until you find a point, which matches the above rules to break the line.

  • Indent split lines with relation to the starting position of the statement or definition.

    Example,

  • However, if you cannot add the type-casting expression or statement with the constrained type in a single line due to it exceeding the max line length,

    • Move the casting type with the operators to a new line.

      Example,

    • Keep the full constrained type name on the same line.

      Example,

      Do's

      Don'ts

Top Level Definitions

Operators, Keywords and Types

Statements

Expressions

Annotations, Documentation and Comments