From 54dfac942fe3b4ade25b00284deac4655d1db8d0 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Tue, 28 Jun 2022 16:38:31 +0200 Subject: [PATCH 1/2] Add readonly criterion to constants description Clarify that we should use readonly when a variable is constant as well as on a best-effort basis when the variable is not constant but should not be modified after declaration. --- bash.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bash.md b/bash.md index d7c4c55..d94ed63 100644 --- a/bash.md +++ b/bash.md @@ -58,8 +58,7 @@ prefer to make the argument mandatory and pass `.` instead. * Constant variables use `SCREAMING_SNAKE_CASE` names. This means variables that are assigned once and then never modified, and that only depend on - other constant variables. The exception to "never modified" is where multiple statements are - used to compute the content of the "constant" (see `COMPILE_FLAGS` in example at the end). + other constant variables. * Non-constant variables use `snake_case` names. @@ -70,6 +69,10 @@ prefer to make the argument mandatory and pass `.` instead. Environment variables and arguments to the script are considered constants. But arguments in functions are not constants. +Constant variables should always have `readonly` applied to them. Non-constant variables can +also have `readonly` applied to them assuming they are never modified and using `readonly` is +not too verbose. + ## Conditions and branching Use `[[` for conditions, not `[`. From dc43497bfd0a0dabdaa7e4a8ec9ec5f0fcda76df Mon Sep 17 00:00:00 2001 From: Jonathan Date: Tue, 28 Jun 2022 16:43:23 +0200 Subject: [PATCH 2/2] Make `COMPILE_FLAGS` variable into snake_case We no longer consider this to be a good example of a constant variable. Update example to reflect this. --- bash.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bash.md b/bash.md index d94ed63..400321b 100644 --- a/bash.md +++ b/bash.md @@ -216,10 +216,10 @@ SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" cd "$SCRIPT_DIR" # Base compile flags valid all the time -COMPILE_FLAGS="disable-foo enable-bar" +compile_flags="disable-foo enable-bar" # Platform specific compile flags if [[ "$(uname -s)" != "Darwin" ]]; then - COMPILE_FLAGS+=" disable-apple" + compile_flags+=" disable-apple" fi # Script arguments count as constants