Syntax · 4 of 6
if / else
The only branching construct. There is no switch — chain if/else if.
Plain if
proc check(x) {
if x > 100 {
return "big"
}
return "small"
}
if / else
proc check(x) {
if x > 100 {
return "big"
} else {
return "small"
}
}
if / else if / else
proc tier(spend) {
if spend >= 10000 {
return "platinum"
} else if spend >= 1000 {
return "gold"
} else if spend >= 100 {
return "silver"
} else {
return "bronze"
}
}
Guard-clause style (recommended)
Validate inputs at the top, fail fast, then proceed with the happy path.
proc transfer(from, to, amount) {
if amount <= 0 { abort "amount must be positive" }
if from == to { abort "cannot transfer to self" }
let sender = find_one("accounts", {account_id: from})
if sender == null { abort "sender not found" }
if sender.balance < amount { abort "insufficient funds" }
// happy path here
update("accounts", {account_id: from}, {$inc: {balance: -amount}})
update("accounts", {account_id: to}, {$inc: {balance: amount}})
return {ok: true}
}
Nested if
proc classify(user) {
if user.country == "TR" {
if user.age >= 18 {
return "TR-adult"
}
return "TR-minor"
}
return "non-TR"
}
Combined conditions
proc can_view(user, post) {
if post.is_public || user._id == post.author_id {
return true
}
return false
}
Branching on database state
proc upsert(email, name) {
let existing = find_one("users", {email: email})
if existing == null {
insert("users", {email: email, name: name, signups: 1})
return {created: true}
} else {
update("users", {email: email}, {$inc: {signups: 1}})
return {created: false, signups: existing.signups + 1}
}
}
Returning from inside an if
You can return or abort from any branch — execution stops immediately.
proc handle_role(user) {
if user.role == "admin" { return {can_edit: true, can_delete: true} }
if user.role == "editor" { return {can_edit: true, can_delete: false} }
if user.role == "viewer" { return {can_edit: false, can_delete: false} }
abort "unknown role"
}
Conditional updates
proc adjust_inventory(sku, delta) {
let p = find_one("products", {sku: sku})
if p == null { abort "product not found" }
if delta > 0 {
update("products", {sku: sku}, {$inc: {stock: delta, restocks: 1}})
} else {
if p.stock + delta < 0 { abort "would go negative" }
update("products", {sku: sku}, {$inc: {stock: delta}})
}
return {sku: sku, new_stock: p.stock + delta}
}