Expressions

The sections below include the coding conventions with respect to expressions.

Function Invocation

  • Do not keep spaces between the function name and opening parentheses (.

  • If it is unable to keep the function invocation in a single line due to it exceeding the max line length, split it into a new line based on the best practices given in line breaks.

Example,

setAgeForEmployee(employeeName,
    employeeID);

Record Literal

  • If empty, keep it as an empty block.

Example,

Person p = {};
  • In a record literal, arrange the fields in a single line. Then, add a space after the comma and leave no spaces between the braces and fields.

Example,

Person p = {name: "john", age: 20};
  • Do not keep any spaces between the key and the colon. Also, keep only one space between the colon and the value.

Example,

Person person = {
    name: "john" // in this field key is the "name" and value is "john".
};
  • You can define the fields in new lines. If so, make sure all the fields are in a separate line and they are block-indented.

Do’s

Person p = {
    name: "john",
    age: 20
};

Don’ts

Person p = {name: "john",
    age: 20};
  
//Or
  
Person p = {
name: "john",
age: 20
};
  
//Or
  
Person p = {
    name: "john",
    age: 20};

Map Literal

  • For map literals, follow the same formatting guidelines as record literals.

Example,

// Inline map literal.
map<string> mapOfString1 = {name: "john", id: "0"};
  
// Mulitline map literal.
map<string> mapOfString2 = {
    name: "john",
    id: "0"
}

Tuple

  • Always, place a tuple in a single line.

Example,

[string, int] tuple = ["john", 20];
  • If a tuple exceeds the maximum line length limit, move the whole tuple to a new line and indent with four spaces from the starting position of the statement or definition.

Example,

[string, int] tuple = 
    [nameOfEmployee, ageOfTheEmployee];

Array Literal

  • Place simple arrays in a single line.
  • Do not keep any spaces between the opening bracket, value, and the closing bracket.

Example,

string[] names = ["john", "doe", "jane", "doe"];
  • If an array cannot be placed on a single line due to it exceeding the max line length, split each value in the array to its own block-indented line.

Example,

string[] names = [
    "john",
    "doe",
    "jane",
    "doe"
];

Type Casting

  • Do not keep spaces between the type and the angle brackets (i.e., <string>).
  • Do not keep spaces between the closing angle bracket and value reference, which will be casted.

Example,

string name = <string>person.name;
  • Avoid line breaks in type casting.

Do’s

<string>

Don’ts

<
    string
>

Table Literal

Example,

type Employee record {
    readonly int id;
    string name;
    float salary;
};

type EmployeeTable table<Employee> key(id);

public function main() {

    EmployeeTable employeeTab = table [
        {id: 1, name: "John", salary: 300.50},
        {id: 2, name: "Bella", salary: 500.50},
        {id: 3, name: "Peter", salary: 750.0}
    ];
}

"Star"

"Watch"