AtlatestRepositorysigil-web-styles
sigil-web-styles / tree / build / dev / lib / _pkg / sigil-stdlib / styleformatting.md
2
# Formatting4
> Code layout, indentation, and parenthesis placement.6
## Indentation8
Use **2 spaces** for indentation. Never tabs.10
```scheme11
(define (factorial n)12
(if (zero? n)13
114
(* n (factorial (- n 1)))))15
```17
## Parentheses19
**Never put closing parens on their own line.** Stack them at the end.21
```scheme22
;; Good23
(define (greet name)24
(string-append "Hello, " name "!"))26
;; Bad27
(define (greet name)28
(string-append "Hello, " name "!")29
)30
```32
## Line Length34
Keep lines under **80 characters**. Break long expressions sensibly.36
```scheme37
;; Too long38
(define result (some-function arg1 arg2 arg3 arg4 arg5 arg6 arg7))40
;; Better41
(define result42
(some-function arg1 arg2 arg343
arg4 arg5 arg644
arg7))45
```47
## Function Definitions49
Short functions on one line:51
```scheme52
(define (square x) (* x x))53
(define (add1 x) (+ x 1))54
```56
Longer functions with body indented:58
```scheme59
(define (process-items items)60
(filter valid?61
(map transform items)))62
```64
## Let Bindings66
Align binding values when it improves readability:68
```scheme69
(let ((name (get-name user))70
(age (get-age user))71
(active (user-active? user)))72
(format "~a (~a)~a" name age73
(if active "" " [inactive]")))74
```76
Or use simple indentation:78
```scheme79
(let ((name (get-name user))80
(age (get-age user)))81
(process name age))82
```84
## Cond and Case86
Align clauses with the opening paren:88
```scheme89
(cond90
((null? lst) 'empty)91
((pair? lst) 'list)92
(else 'other))94
(case type95
((a b c) (handle-abc))96
((x y z) (handle-xyz))97
(else (handle-default)))98
```100
## Lambda102
Short lambdas inline:104
```scheme105
(map (lambda (x) (* x 2)) items)106
(filter (lambda (x) (> x 0)) numbers)107
```109
Multi-line lambdas indented:111
```scheme112
(map (lambda (item)113
(let ((name (item-name item)))114
(string-upcase name)))115
items)116
```118
## Match120
Align patterns with the opening:122
```scheme123
(match value124
('() "empty")125
((x) (format "single: ~a" x))126
((x . rest) (format "head: ~a, tail: ~a" x rest)))127
```129
## Blank Lines131
One blank line between top-level definitions:133
```scheme134
(define (foo x)135
...)137
(define (bar y)138
...)139
```141
Group related definitions together, separate groups with blank lines:143
```scheme144
;; Constructors145
(define (make-point x y) ...)146
(define (make-rect x y w h) ...)148
;; Predicates149
(define (point? x) ...)150
(define (rect? x) ...)151
```153
## Comments155
Align comments with code they describe. Add a blank line before a comment that introduces a new section:157
```scheme158
(define (complex-operation x)159
;; First, validate input160
(validate x)162
;; Then process163
(process x))164
```166
End-of-line comments after two spaces:168
```scheme169
(define pi 3.14159) ; Approximation170
```172
## Long Argument Lists174
When a call doesn't fit on one line, break after the operator:176
```scheme177
(create-user178
name: "Alice"179
email: "[email protected]"180
role: 'admin181
active: #t)182
```184
Or align with the first argument:186
```scheme187
(send-request client188
method: 'POST189
path: "/api/users"190
body: data)191
```