#Part 8. Language In Depth
#Part 8 Section 1. Functions, Parameters, And Returns
Functions declare parameter types and an optional return type. Keep functions narrow: validate at the boundary, perform one operation, and return a value the caller can check.
fn clamp_score(i256 value, i256 minimum, i256 maximum) -> i256 {
if (value < minimum) { return minimum; }
if (value > maximum) { return maximum; }
return value;
}
fn main() {
i256 score = clamp_score(145, 0, 100)
println(score)
}
Namespaced library functions use the library name in the function declaration, such as fn CB-TextTools.slug(...). Application-private helpers should not imitate a library namespace.
#Part 8 Section 2. Strings And Arrays
Strings are immutable values. Use string.trim, string.lower, string.upper, string.starts_with, string.ends_with, string.contains, string.index_of, string.substring, string.replace, and string.split instead of hand-written byte loops. Check the result of string.index_of before slicing because a missing value returns a negative index.
Arrays are ordered and bounds-checked. Use len(items) before indexing. Use array.push, array.join, array.slice, and the other CB-Array helpers for collection work.
library CB-Array
library CB-String
fn first_nonempty(array rows) -> string {
i256 index = 0
while (index < len(rows)) {
string row = string.trim(rows[index])
if (len(row) > 0) { return row }
index += 1
}
return ""
}
Do not use an array as an unbounded cache. Decide its maximum size and discard or persist old entries deliberately.
#Part 8 Section 3. Loops And Responsiveness
Every loop needs a visible exit condition. In a graphical application, the main loop must pump events, update time-based work, draw only when dirty, and yield briefly when idle.
while (true) {
handle_events()
update_app()
if (frame_dirty) {
draw_app()
swap_buffers()
frame_dirty = false
} else {
time.sleep(8)
}
if (should_close()) { break }
}
Never put an unbounded network request, parser, or crawl in the render path. Break background work into bounded steps and display its state.
#Part 8 Section 4. Error Boundaries
Use three layers of error handling:
1. Validate input before work begins. 2. Use _or helpers for expected malformed values. 3. Use try and catch around I/O, package loading, and network boundaries.
An error message should say what failed, which resource was involved, and what the user can do. Never display access tokens, passwords, encryption keys, or full private paths in a production error.
#Part 8 Section 5. Comments And Naming
Use # for a comment. Explain why a constraint exists rather than repeating the next line. Prefer names such as next_poll_ms, maximum_document_bytes, and active_tab over abbreviations that hide units or state. Include units in names for time, memory, pixels, and byte counts.
