1 Scripting Coding Convention
Tim Svensson edited this page 2026-04-24 13:07:22 +00:00

Scripting Coding Convention

General

  • Indentation is explicitly made with two spaces instead of using tab.
  • Comments should be short and descriptive.
  • Filenames follow the naming convention PascalCase followed by their extension (e.g., BuildScript.sh).
  • Functions should have a comment that describes what the function does.

To avoid cluttering the script should also try to group belonging commands together and add space(s) between other groups. Example:

# Movies dir
cd ./movies
ffmpeg -i input1.mp4 -c:v libx265 -crf 25 -preset fast -pix_fmt yuv420p10le -c:a libopus -b:a 96k output1.mkv
ffmpeg -i input2.mp4 -c:v libx265 -crf 25 -preset fast -pix_fmt yuv420p10le -c:a libopus -b:a 96k output2.mkv
ffmpeg -i input3.mp4 -c:v libx265 -crf 25 -preset fast -pix_fmt yuv420p10le -c:a libopus -b:a 96k output3.mkv
cd ..

# Shows dir
cd ./shows
ffmpeg -i input.mp4 -c:v libx265 -crf 25 -preset fast -pix_fmt yuv420p10le -c:a libopus -b:a 96k output.mkv
cd ..

Bash

  • The max length of a line should not extend 80 characters.
  • The script should always use the .sh extension.
  • Bash use snake_case for its variable names (e.g., some_variable).
  • Commenting in Bash is done with a "#" character followed by a space.
  • Bash scripts should always start with:
#!/bin/bash
#
# Description of what the script does
  • Functions should use the same naming style as variables (snake_case) and use $1, $2, etc. for parameters.
  • Comparisons don't usually need quotes, but can be used to avoid bugs with variables having spaces in them. Otherwise if-statments only need to end with "fi":
dependency_missing=false

# dependency_missing don't need quotes since it's a boolean
if $dependency_missing; then
  echo "Missing dependencies"
  exit 1
fi

Batch

  • Batch scripts use the .bat extention.
  • Batch use camelCase for its variable names (e.g., someVariable).
  • Commenting in Batch is done with two ":" characters followed by NO space.
  • Functions should use the same naming style as variables (camelCase) and use $1, $2, etc. for parameters.
  • Comparisons in Batch uses quotes even if it isn't a string, variables are put between '!'s or '%'s. Example:
if "!dependencyMissing!"=="true" (
  ::Something happens
)
or
if "%dependencyMissing%"=="true" (
  ::Something happens
)