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 not possible to keep the function invocation in a single line due to it exceeding the maximum 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 you do so, make sure each field is in a separate line and is 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 maximum 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.
Example,
<int>id
-
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
-
Follow the formatting guidelines of Record literals when formatting a table block.
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"