BAD:
function MyFunction() {
cd some_random_directory
# Do something useful
}
GOOD:
function MyFunction() {
# Change to the necessary directory
pushd some_random_directory
# Do something useful
# Now pop the directory off the stack
cd -
}
If you have conditional statements, then make sure you catch them too:
BAD:
function MyFunction() {
# Change to the necessary directory
pushd some_random_directory
# Do something useful and test something
if [ "${test}" == "value" ]; then
return # Oops, this function left us somewhere unexpected
fi
# Now pop the directory off the stack
cd -
}
GOOD:
function MyFunction() {
# Change to the necessary directory
pushd some_random_directory
# Do something useful and test something
if [ "${test}" == "value" ]; then
# Don't forget to pop the directory off the stack
cd -
return
fi
# Now pop the directory off the stack
cd -
}
Basically, preserve an expected and known state between function calls, just like any modern other programming language.
No comments:
Post a Comment