Updated Template (markdown)

koalaman
2017-07-10 23:18:44 -07:00
parent 9135679dcd
commit 33087384ae

@@ -3,18 +3,43 @@
### Problematic code:
```sh
# Simple example of problematic code
#!/bin/sh
myfunction
myfunction() {
echo "Hello World"
}
```
### Correct code:
```sh
# Simple example of above code, only fixed
#!/bin/sh
myfunction() {
echo "Hello World"
}
myfunction
```
### Rationale:
(An explanation of why the code is problematic and how the correct code is an improvement)
You are calling a function that you are defining later in the file. The function definition must come first.
Function definitions are much like variable assignments, and define a name at the point the definition is "executed". This is why they must happen before their first use.
This is especially apparent when defining functions conditionally:
```
case "$(uname -s)" in
Linux) hi() { echo "Hello from Linux"; } ;;
Darwin) hi() { echo "Hello from macOS"; } ;;
*) hi() { echo "Hello from something else"; } ;;
esac
hi
```
### Exceptions:
(Cases where the user may choose to ignore this warning, if any.)
None.