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 each argument to its own block-indented line.
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>json.name;
- Avoid line breaks in type casting.
Do’s
<string>
Don’ts
<
string
>
Table Literal
- Follow record literals formatting when formatting a table block.
Example,
table<Employee> employee1 = table {
{key id, name, address}
};
table<Employee> employee2 = table {
{
key id,
name,
address
},
[{"1", "test", "No:123 hty RD"}, {"1", "john", "No:123"}]
};
table<Employee> employee3 = table {
{id, name, address},
[
{"1", "john", "No:123"},
{"2", "jane", "No:342"}
]
};
table<Employee> employee4 = table {
{id, name, address},
[
{
"1",
"john",
"No:123"
},
{"2", "jane", "No:342"}
]
}
"Star"
"Watch"