OILS / doc / style-guide.md View on Github | oilshell.org

113 lines, 67 significant
1---
2default_highlighter: oils-sh
3---
4
5YSH Style Guide
6===============
7
8Here are some recommendations on coding style.
9
10<div id="toc">
11</div>
12
13## Your Names
14
15### Procs and Funcs Look Different
16
17 proc kebab-case() {
18 echo hi
19 }
20
21 func camelCase() {
22 echo hi
23 }
24
25### Variables
26
27Local variables:
28
29 var snake_case = 42
30
31Hungarian for global "constants":
32
33 var kMyConst = 42 # immutable
34
35 var gMyGlobal = {} # mutable
36
37For consistency, this style is also OK:
38
39 var MY_CONST = 42
40
41Env vars use `CAP_WORDS`:
42
43 var maxProcs = ENV.MAX_PROCS
44
45### Filenames
46
47 my-script.sh # runs with /bin/sh and OSH
48
49 my-script.bash # runs with bash and OSH
50
51 my-script.osh # runs with OSH
52
53 my-script.ysh # runs with YSH
54
55## YSH Names
56
57Capital Letters are used for types:
58
59 Null Bool Int Float Str
60 List Dict
61 Proc Func
62
63Special shell variables:
64
65 PATH IFS
66
67Global variables that are **silently mutated** by the interpreter start with
68`_`:
69
70 _error _pipeline_status _reply
71
72As do functions to access such mutable vars:
73
74 _group() _start() _end()
75
76Example:
77
78 try {
79 false
80 }
81 if (_error.code !== 0) {
82 echo 'failed'
83 }
84
85## Related
86
87- [Shell Language Idioms](shell-idioms.html)
88- [A Feel For YSH Syntax](syntax-feelings.html)
89
90
91<!--
92`kebab-case` is for procs and filenames:
93
94 gc-test opt-stats gen-mypy-asdl
95
96 test/spec-runner.ysh
97
98`snake_case` is for local variables:
99
100 proc foo {
101 var deploy_dest = 'bar@example.com'
102 echo $deploy_dest
103 }
104
105`CAPS` are used for global variables built into the shell:
106
107 PATH IFS UID HOSTNAME
108
109External programs also accept environment variables in `CAPS`:
110
111 PYTHONPATH LD_LIBRARY_PATH
112
113-->