Statements
The sections below include the coding conventions with respect to statements.
If Statement
- Always enclose the condition with parentheses in the If statement.
Do’s
if (true) {
...
} else if (false) {
...
}
Don’ts
if true {
...
} else if false {
...
}
- Keep the
else
andelse if
keywords in the same line with the matchingif
orelse if
block’s closing brace separated only by a single space.
Empty Block
- Do not have any empty
if
,else if
, orelse
blocks. - If empty, add an empty line between the opening and closing braces.
Example,
if (inProperSallaryRange) {
} else if (inSallaryRange) {
} else {
}
Match Statement
Match Patterns Clause
- Block indent each pattern clause in its own line.
- Keep a single space before and after the
=>
sign. - If a pattern clause contains only one statement, place it in the same line as the pattern clause enclosing it with curly braces.
Example,
function foo(string|int|boolean a) returns string {
match a {
12 => {
return "Value is '12'";
}
}
return "Value is 'Default'";
}
- If a pattern clause has more than one statement, block indent each statement in its own line.
Example,
match x {
var (s, i) if s is string => {
io:println("string");
}
var (s, i) if s is int => {
io:println("int");
}
}
-
If the pattern body is empty, then keep it as an empty block.
Example,
match x {
var (s, i) if s is string => {}
var (s, i) if s is int => {}
}
Transaction Statement
- Start each optional clause (
onretry
,committed
, andaborted
) in the same line as the closing brace of the matching clause. - If
transaction
,onretry
,committed
, andaborted
blocks are empty, add an empty line between the braces.
Example,
function func1() {
transaction with retries = 2 {
} onretry {
} aborted {
} committed {
}
}