YSH Style Guide

Here are some recommendations on coding style.

Table of Contents
Your Names
Procs and Funcs Look Different
Variables
Filenames
YSH Names
Appendix
Reserved Names
Related

Your Names

Procs and Funcs Look Different

proc kebab-case() {
  echo hi
}

func camelCase() {
  echo hi
}

Variables

Local variables:

var snake_case = 42

Hungarian for global "constants":

var kMyConst = 42   # immutable

var gMyGlobal = {}  # mutable

For consistency, this style is also OK:

var MY_CONST = 42

Env vars use CAP_WORDS:

var maxProcs = ENV.MAX_PROCS

Filenames

my-script.sh    # runs with /bin/sh and OSH

my-script.bash  # runs with bash and OSH

my-script.osh   # runs with OSH

my-script.ysh   # runs with YSH

my-module.ysh   # import with 'use'
mymodule.ysh    # also OK

YSH Names

Capital Letters are used for types:

Null   Bool   Int   Float   Str
List   Dict
Proc   Func

Special shell variables:

PATH   IFS

Global variables that are silently mutated by the interpreter start with _:

_error   _pipeline_status   _reply

As do functions to access such mutable vars:

_group()   _start()   _end()

Example:

try {
  false
}
if (_error.code !== 0) {
  echo 'failed'
}

Appendix

Reserved Names

As in Python, names like __provide__ are reserved by the interpreter.

Related

Generated on Sun, 10 Nov 2024 17:31:14 +0000