| 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 |     _status   _pipeline_status   _reply
 | 
| 71 | 
 | 
| 72 | As do functions to access such mutable vars:
 | 
| 73 | 
 | 
| 74 |     _group()   _start()   _end()
 | 
| 75 | 
 | 
| 76 | Example:
 | 
| 77 | 
 | 
| 78 |     try false
 | 
| 79 |     if (_status !== 0) {
 | 
| 80 |       echo 'failed'
 | 
| 81 |     }
 | 
| 82 | 
 | 
| 83 | ## Related 
 | 
| 84 | 
 | 
| 85 | - [Shell Language Idioms](shell-idioms.html)
 | 
| 86 | - [A Feel For YSH Syntax](syntax-feelings.html)
 | 
| 87 | 
 | 
| 88 | 
 | 
| 89 | <!--
 | 
| 90 | `kebab-case` is for procs and filenames:
 | 
| 91 | 
 | 
| 92 |     gc-test   opt-stats   gen-mypy-asdl
 | 
| 93 | 
 | 
| 94 |     test/spec-runner.ysh
 | 
| 95 | 
 | 
| 96 | `snake_case` is for local variables:
 | 
| 97 | 
 | 
| 98 |     proc foo {
 | 
| 99 |       var deploy_dest = 'bar@example.com'
 | 
| 100 |       echo $deploy_dest
 | 
| 101 |     }
 | 
| 102 | 
 | 
| 103 | `CAPS` are used for global variables built into the shell:
 | 
| 104 | 
 | 
| 105 |     PATH  IFS  UID  HOSTNAME
 | 
| 106 | 
 | 
| 107 | External programs also accept environment variables in `CAPS`:
 | 
| 108 | 
 | 
| 109 |     PYTHONPATH  LD_LIBRARY_PATH
 | 
| 110 | 
 | 
| 111 | -->
 |