1 | ---
|
2 | default_highlighter: oils-sh
|
3 | ---
|
4 |
|
5 | YSH Style Guide
|
6 | ===============
|
7 |
|
8 | Here 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 |
|
27 | Local variables:
|
28 |
|
29 | var snake_case = 42
|
30 |
|
31 | Hungarian for global "constants":
|
32 |
|
33 | var kMyConst = 42 # immutable
|
34 |
|
35 | var gMyGlobal = {} # mutable
|
36 |
|
37 | For consistency, this style is also OK:
|
38 |
|
39 | var MY_CONST = 42
|
40 |
|
41 | Env 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 |
|
57 | Capital Letters are used for types:
|
58 |
|
59 | Null Bool Int Float Str
|
60 | List Dict
|
61 | Proc Func
|
62 |
|
63 | Special shell variables:
|
64 |
|
65 | PATH IFS
|
66 |
|
67 | Global variables that are **silently mutated** by the interpreter start with
|
68 | `_`:
|
69 |
|
70 | _error _pipeline_status _reply
|
71 |
|
72 | As do functions to access such mutable vars:
|
73 |
|
74 | _group() _start() _end()
|
75 |
|
76 | Example:
|
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 |
|
109 | External programs also accept environment variables in `CAPS`:
|
110 |
|
111 | PYTHONPATH LD_LIBRARY_PATH
|
112 |
|
113 | -->
|