smartcontractkit / smartcontractkit/chainlink
[DEVEL] Unnecessary use of $ / ${} with arithmetic variable SH-2004 critical anti-pattern
Open
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 8.2k
- Forks
- 2k
- Avg merge
- 1d 23h
- Merged PRs (30d)
- 202
Description
Description
escription
Use of $ or ${..} on regular variables in arithmetic context is unnecessary, and can even lead to subtle bugs. This is because the content of $((..)) is first expanded into a string, and then evaluated as an expression:
$ a='1+1'
$ echo $(($a * 5)) # becomes 1+1*5
6
$ echo $((a * 5)) # evaluates as (1+1)*5
10
The $ is unavoidable for special variables like $1 vs 1, $# vs #. It is also required when adding modifiers to parameters expansions, like ${#var} or ${var%-}.
Problematic code:
echo $(($n + ${arr[i]}))
Preferred code:
echo $((n + arr[i]))
$/${} is unnecessary on arithmetic variables found here:
$/${} is unnecessary on arithmetic variables found here:
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Open tools/benchmark/job_spec_delete_v2.sh and inspect the arithmetic expressions at lines 134 and 143. Remove the unnecessary variable expansions in those expressions, then review the surrounding shell syntax to confirm the script still uses the intended arithmetic evaluation.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- bash
- Domain
- tooling
- Issue type
- Refactor
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100