iolivia/fizzbuzz

Did you run your code through static analysis tools?

Closed this issue ยท 3 comments

For example, shellcheck (https://github.com/koalaman/shellcheck) has this to say about fizzbuzz.sh:

$ shellcheck fizzbuzz.sh 

In fizzbuzz.sh line 7:
    if [ `expr $i % 3` == 0 ] && [ `expr $i % 5` == 0 ]; then
         ^-- SC2046: Quote this to prevent word splitting.
         ^-- SC2006: Use $(..) instead of legacy `..`.
          ^-- SC2003: expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].
                                   ^-- SC2046: Quote this to prevent word splitting.
                                   ^-- SC2006: Use $(..) instead of legacy `..`.
                                    ^-- SC2003: expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].


In fizzbuzz.sh line 9:
    elif [ `expr $i % 3` == 0 ]; then
           ^-- SC2046: Quote this to prevent word splitting.
           ^-- SC2006: Use $(..) instead of legacy `..`.
            ^-- SC2003: expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].


In fizzbuzz.sh line 11:
    elif [ `expr $i % 5` == 0 ]; then
           ^-- SC2046: Quote this to prevent word splitting.
           ^-- SC2006: Use $(..) instead of legacy `..`.
            ^-- SC2003: expr is antiquated. Consider rewriting this using $((..)), ${} or [[ ]].

Here's a diff that updates the code to address all the things shellcheck complained about:

$ git diff
diff --git a/fizzbuzz.sh b/fizzbuzz.sh
old mode 100644
new mode 100755
index b870440..a7699f8
--- a/fizzbuzz.sh
+++ b/fizzbuzz.sh
@@ -4,11 +4,11 @@
 for i in {1..100}
 do
 
-    if [ `expr $i % 3` == 0 ] && [ `expr $i % 5` == 0 ]; then
+    if [[ $((i % 3)) == 0 ]] && [[ $((i % 5)) == 0 ]]; then
         echo "FizzBuzz"
-    elif [ `expr $i % 3` == 0 ]; then
+    elif [[ $((i % 3)) == 0 ]]; then
         echo "Fizz"
-    elif [ `expr $i % 5` == 0 ]; then
+    elif [[ $((i % 5)) == 0 ]]; then
         echo "Buzz"
     else
         echo $i

If you want a PR instead, I'll be happy to provide that.

thanks @bknowles, I didn't run any of the code through static analysis since I did most of this in online compilers. I did run it through hackerrank for all the supported languages to verify the correctness ๐Ÿ˜„

thanks for your contributions! ๐ŸŽ‰ ๐Ÿš€

it's a bit tricky to run all the languages through static compilers because:

  • some don't have any tools for that
  • some have tools but are highly configuration reliant (for example javascript/typescript have linters but there's no "standard" way)
  • there's no build/compile in this repo, it's all just source, so if we made everything compliant now we would have to manually make sure it all stays compliant

Yeah, static analysis tools will vary by language, and when you're mixing a wide variety of languages in a single repo as opposed to using a single repo to house an entire project or part of a project, it gets harder to incorporate things like these tools in an automated fashion.

So, it will definitely be totally manual. But, I felt it would be good to at least make an attempt to show people what appears to be current idiomatic code, at least starting with bash.

Thanks again!