Operators, keywords, and types

The sections below include the coding conventions with respect to operators, keywords, and types.

Keywords and Types

  • Do not keep spaces between the type and the pipe operator when it is in a union type (e.g., string|int).

Example,

Copy
type method "POST"|"GET"|"PUT";
    
int|string variable = 0;
  
function getValue(string key) returns string|error {
    ...
}
  
function getName() returns string|error {
    string|error valueOrError = getValue("name");
    ...
}
  • Do not keep spaces between the type and the optional operator ?.

Example,

Copy
string? name;
  • Avoid line breaks inside constrained types.

Do's

Copy
map<int|string> x; // map reference type

Don'ts

Copy
map<
    int
    |
    string
> x;

Operators

  • Keep only a single space before and after the = operator.

Example,

Copy
int a = 0;
  • Do not keep spaces around the semicolon ;.
  • Do not keep spaces between the unary operator and the expression.

Example,

Copy
a = -a;
  • Keep a single space before and after any binary or ternary operator.

Example,

Copy
var fullName = firstName + lastName;
  
string? name = isNameAvailable() ? getName() : "Unknown";
  
var elvisOperator = name ?: "Unknown";
  • Keep a single space before and after a compound operator such as -= and +=.

Example,

Copy
name += lastName;

"Star"

"Watch"

Previous