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. Use spaces only and never mix tabs and spaces
Multiple Lines
Long statements can span multiple lines for better readability:
function NameSupply(
n String,
s U256,
) -> (name String,
supply U256):
return(name: n,
supply: s)
Lines may continue wherever a space could be written. Strings can span multiple lines, but indentation is included in the string:
s = "My spl
it string" // Result: "My split string"
s = "My spl
it string" // Result: "My spl it string"
Comments
Coco supports line comments with //. Multi-line comments require // on each line:
// Multi-line comments need
// a // sign in front of
// each line
x = 5 // inline comment
Unit Test Directives
Special comment prefixes // > and // < are used for testing. They are ignored by the compiler but processed by coco test.
Naming Conventions
| Element | Convention | Example |
|---|---|---|
| File name | snake_case | my_logic.coco |
| Module name | PascalCase | MyLogic |
| Package name | snake_case | my_package |
| Endpoints/Functions | PascalCase | GetBalance |
| Classes & Events | PascalCase | TokenInfo |
| Class methods | snake_case | get_value |
| Variables & Arguments | snake_case | user_balance |
| State & Field names | snake_case | total_supply |
| Constants | ALL_CAPS | MAX_SUPPLY |