Coding Style
Indentation
Coco uses Python-style indentation to define code hierarchy. A colon (:) at the end of a line indicates that an indented block follows.
// Standard Block (Recommended: 4 spaces)
if x > 5:
throw "Too much"
else:
emit "Phew"
// One-Liner (Allowed for single statements)
if x > 5: throw "Too much"
else: emit "Phew"
Key Rules:
- Consistency: All lines in a block must use the exact same number of spaces
- Tabs vs. Spaces: A TAB counts as 1 space. To avoid errors, use spaces only and never mix tabs and spaces
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 |