Coding Style
Indentation
Coco uses indentation for code hierarchy, indented blocks are internal to the higher-level statement, e.g.
if x > 5: // colon (:) at the end of the line means an indented block follows
throw "Too much" // indented block as the body "if" statement
else: // de-dent to the same level as "if"
emit "Phew" // block under "else"
Suggested style is using 4-space indentation, but any number of spaces or TAB characters counts as indents. The only requirement for the next line to be at the same level as the previous, is to have the same number of spaces (TAB counts as a single space, so mixing spaces and tabs may lead to ugly results).
When lower-level blocks contain only a single line, new-line after : can be omitted and the above example can be written as:
if x > 5: throw "Too much"
else: emit "Phew"
Multiple lines
To improve readability, developers can write long statements in multiple lines, e.g.
function NameSupply(
n String,
s U256,
) -> (name String,
supply U256):
return(name: n,
supply: s)
Lines may continue in the next lines whenever the statement or expression is not ended, i.e. anywhere where a space could be written. Strings can be split to multiple lines, but they can't be indented as indents are included in the string.
s = "My spl
it string" // My split string
s = "My spl
it string" // My spl it string
Comments
Only line comments // are supported by Coco, so multi-line comments need to have // in front of each line. // and anything after it on the same line is ignored by compiler.
// We have to put a // sign in front of
// each line in multi-line
// comment
x = 5 // this is a comment
Unit test directives
One special case of comments are unit tests directives for coco test command, so // > and // < are ignored by compiler, but used for testing.
Naming Conventions
There are no hard rules for naming endpoints/functions, variables and classes in Coco. This guide uses the following naming style:
| element | case |
|---|---|
| file name | snake_case.coco |
| coco module name | PascalCase |
| package name | snake_case |
| endpoints/functions, classes and events | PascalCase |
| class methods | snake_case |
| variables, function arguments and return values | snake_case |
| state and class field names | snake_case |
| constants | ALL_CAPS |