Skip to content
Snippets Groups Projects
CODINGSTYLE.md 2.42 KiB
Newer Older
Olliver Schinagl's avatar
Olliver Schinagl committed
# Alpine Linux coding style

Thank you for taking interest in contributing to our aports repository.
As consistency and readability are so important for the quality of our APKBUILD
and thus the quality of Alpine Linux, we kindly ask to follow these
recommendations.
Olliver Schinagl's avatar
Olliver Schinagl committed

## Language
Simon F's avatar
Simon F committed
Alpine Linux APKBUILD files are inherently just POSIX shell scripts. Please avoid
extensions, even if they work or are accepted by busybox ash. (using keyword
`local` is an exception)
Olliver Schinagl's avatar
Olliver Schinagl committed

## Naming convention
Use snake_case. Functions, variables etc. should be lower-cased with
underscores to separate words.

Simon F's avatar
Simon F committed
Local 'private' variables and functions in global scope should be prefixed
with a single underscore to avoid name collisions with internal variables in
`abuild`.

Double underscores are reserved and should not be used.
Olliver Schinagl's avatar
Olliver Schinagl committed
```sh
_my_variable="data"
```

### Bracing
Simon F's avatar
Simon F committed
Curly braces for functions should be on the same line.
Simon F's avatar
Simon F committed
Markers to indicate a compound statement should be on the same line.
Olliver Schinagl's avatar
Olliver Schinagl committed
```sh
	if [ true ]; then
		echo "It is so"
	fi
}
```

#### while ...; do
```sh
	while ...; do
		...
	done
```

#### for ...; do
```sh
Simon F's avatar
Simon F committed
	for x in foo bar baz; do
Olliver Schinagl's avatar
Olliver Schinagl committed
### Spacing
All keywords and operators are separated by a space.

Simon F's avatar
Simon F committed
For cleanliness sake, ensure there is no trailing whitespace.
Olliver Schinagl's avatar
Olliver Schinagl committed

## Identation
Simon F's avatar
Simon F committed
Indentation is one tab character per level, alignment is done using spaces.
For example (using the >------- characters to indicate a tab):
Olliver Schinagl's avatar
Olliver Schinagl committed
```sh
Simon F's avatar
Simon F committed
build()
Olliver Schinagl's avatar
Olliver Schinagl committed
{
>-------make DESTDIR="${pkgdir}" \
>-------     PREFIX="/usr"
}
```

This ensures code is always neatly aligned and properly indented.

Space before tab should be avoided.

Olliver Schinagl's avatar
Olliver Schinagl committed
## Line length
A line should not be longer than 80 characters. While this is not a hard limit, it
Olliver Schinagl's avatar
Olliver Schinagl committed
is strongly recommended to avoid having longer lines, as long lines reduce
readability and invite deep nesting.

## Variables
Olliver Schinagl's avatar
Olliver Schinagl committed
### Quoting
Quote strings containing variables, command substitutions, spaces or shell meta
characters, unless careful unquoted expansion is required.

Don't quote _literal_ integers.
Olliver Schinagl's avatar
Olliver Schinagl committed

### Bracing
Only use curly braces around variables when needed.
Olliver Schinagl's avatar
Olliver Schinagl committed

```sh
foo="${foo}_bar"
Olliver Schinagl's avatar
Olliver Schinagl committed
```

## Subshell usage
Simon F's avatar
Simon F committed
Use `$()` syntax, not backticks.
Olliver Schinagl's avatar
Olliver Schinagl committed

## Sorting
Some items tend to benefit from being sorted. A list of sources, dependencies
etc. Always try to find a reasonable sort order, where alphabetically tends to
be the most useful one.

## Eval
`eval` is evil and should be avoided.