Skip to content

Commit

Permalink
Begin a style guide
Browse files Browse the repository at this point in the history
A style guide is a must for the shell, and the existing ones are
not good enough for us to adopt, since they were not designed with
portability in mind.

We'll begin a new one.
  • Loading branch information
alganet committed Jul 7, 2024
1 parent ce0614a commit b9c13c0
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions doc/STYLE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# coral style guide

### 4-width tab indents

<font color=green>**RIGHT**</font>: Tabs configured to width 4.

<font color=red>**WRONG**</font> Spaces, tabs configured to width different than 4.

_Rationale_: Shell script indented heredocs `<<-` require the use of tabs. Failure to use tabs might yield extra spaces when reading the contents of these structures.

### Space after function name declaration

<font color=green>**RIGHT**</font>: Space between name and `()`
```sh
mymod_myfn () {
_print hello
}
```

<font color=red>**WRONG**</font> No space between name and `()`
```sh
mymod_myfn() {
_print hello
}
```

_Rationale_: However, in sh, we call functions as `foo arg`, not `foo(arg` so we must declare it with an extra space to leverage tools like grep and IDE search.

### No function keyword

<font color=green>**RIGHT**</font>: Space between name and `()`
```sh
mymod_myfn () {
_print hello
}
```

<font color=red>**WRONG**</font> No space between name and `()`
```sh
function mymod_myfn {
_print hello
}
```

_Rationale_: This syntax is not portable.

### No inline functions

<font color=green>**RIGHT**</font>: Function declaration and closing on separated lines
```sh
mymod_myfn () {
_print hello
}
```

<font color=red>**WRONG**</font>: Inlined function
```sh
mymod_myfn () { _print hello; }
```

_Rationale_: Most shells don't support reflection on the currently defined functions. _coral_ uses a shallow parser to perform such reflection once before runtime (for instance, on unit tests). This parser assumes a strict formatting of the source code.

### Functions live at indentation zero

<font color=green>**RIGHT**</font>: Functions are defined on indentation level zero
```sh
mymod_myfn () {
_print hello
}
```

<font color=orange>**OK**</font>: Declaration is on its own line and at indentation zero.
```sh
mymod_myfn () {
_print hello; }
```

<font color=red>**WRONG**</font>: Indented function declaration
```sh
mymod_myfn () {
_print hello
}
```

_Rationale_: Most shells don't support reflection on the currently defined functions. _coral_ uses a shallow parser to perform such reflection once before runtime (for instance, on unit tests). This parser assumes a strict formatting of the source code.

0 comments on commit b9c13c0

Please sign in to comment.