| 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 | my-module.ysh # import with 'use'
|
| 56 | mymodule.ysh # also OK
|
| 57 |
|
| 58 | ## YSH Names
|
| 59 |
|
| 60 | Capital Letters are used for types:
|
| 61 |
|
| 62 | Null Bool Int Float Str
|
| 63 | List Dict
|
| 64 | Proc Func
|
| 65 |
|
| 66 | Special shell variables:
|
| 67 |
|
| 68 | PATH IFS
|
| 69 |
|
| 70 | Global variables that are **silently mutated** by the interpreter start with
|
| 71 | `_`:
|
| 72 |
|
| 73 | _error _pipeline_status _reply
|
| 74 |
|
| 75 | As do functions to access such mutable vars:
|
| 76 |
|
| 77 | _group() _start() _end()
|
| 78 |
|
| 79 | Example:
|
| 80 |
|
| 81 | try {
|
| 82 | false
|
| 83 | }
|
| 84 | if (_error.code !== 0) {
|
| 85 | echo 'failed'
|
| 86 | }
|
| 87 |
|
| 88 | ## Appendix
|
| 89 |
|
| 90 | ### Reserved Names
|
| 91 |
|
| 92 | As in Python, names like `__provide__` are reserved by the interpreter.
|
| 93 |
|
| 94 | ### Related
|
| 95 |
|
| 96 | - [Shell Language Idioms](shell-idioms.html)
|
| 97 | - [A Feel For YSH Syntax](syntax-feelings.html)
|
| 98 |
|
| 99 |
|
| 100 | <!--
|
| 101 | `kebab-case` is for procs and filenames:
|
| 102 |
|
| 103 | gc-test opt-stats gen-mypy-asdl
|
| 104 |
|
| 105 | test/spec-runner.ysh
|
| 106 |
|
| 107 | `snake_case` is for local variables:
|
| 108 |
|
| 109 | proc foo {
|
| 110 | var deploy_dest = 'bar@example.com'
|
| 111 | echo $deploy_dest
|
| 112 | }
|
| 113 |
|
| 114 | `CAPS` are used for global variables built into the shell:
|
| 115 |
|
| 116 | PATH IFS UID HOSTNAME
|
| 117 |
|
| 118 | External programs also accept environment variables in `CAPS`:
|
| 119 |
|
| 120 | PYTHONPATH LD_LIBRARY_PATH
|
| 121 |
|
| 122 | -->
|