Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bash - Flag constants as read-only #17

Open
MrChocolatine opened this issue Jun 28, 2022 · 5 comments
Open

Bash - Flag constants as read-only #17

MrChocolatine opened this issue Jun 28, 2022 · 5 comments

Comments

@MrChocolatine
Copy link

MrChocolatine commented Jun 28, 2022

Global constants

In your coding guidelines:

* 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).

May I suggest to also declare these as readonly for more strictness?

The above example would become:

readonly SCREAMING_SNAKE_CASE="something"

Your usual:

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

would become:

readonly SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Local/scoped constants

On a very similar note, for variables scoped to their function with local:

coding-guidelines/bash.md

Lines 94 to 104 in 5c5931d

Use `local` for variables inside functions unless they need to be global for some reason
(They most likely should not be). This stops execution of functions from polluting the global
namespace. Do this both for constants and other variables.
```bash
function cowsay {
local COW_PREFIX="The cow says"
local message="$1"
echo "$COW_PREFIX: $message"
}
```

For those that should be constants, additionally to having them written in uppercase you could also declare them as read-only with local -r:

function cowsay { 
    local -r COW_PREFIX="The cow says"
    local message="$1" 
    echo "$COW_PREFIX: $message" 
} 
@MrChocolatine MrChocolatine changed the title Bash - Constants as read-only Bash - Flag constants as read-only Jun 28, 2022
@faern
Copy link
Member

faern commented Jun 28, 2022

Looks good to me! We'll incorporate this in our bash coding guidelines one way or another. Thanks.

@MrChocolatine
Copy link
Author

Looks like I was making an update at the same time you commented @faern , I added a section on how to declare local variables as read-only too.

@MrChocolatine
Copy link
Author

@faern
I would be happy to help you incorporate these changes and update your existing Bash scripts, as it would only be refactors.

Let me know if that's something you would be okay with or if you need to talk more internally before doing any change 👍 .

@faern
Copy link
Member

faern commented Jun 28, 2022

Thank you. But I don't think we want too much churn in our bash scripts. We have plenty of scripts where we don't follow our own guidelines perfectly. So I expect it to be a can of worms. I want to open the lid to that can slowly.

I think we first want to figure out exactly what our guidelines should say. There is a small problem with just doing what you wrote exactly. shellcheck prefers:

VARIABLE=$(...)
readonly VARIABLE

Because otherwise the exit code of the subshell is not handled properly. And I don't think we want to bloat all our scripts with extra lines just because of this. In the end, readonly is nice-to-have. But it's far from important, and readability matters. So we need to figure out when exactly we want to enforce readonly.

@MrChocolatine
Copy link
Author

I totally understand 👍 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants