# die() - Cool Constructs 1
       
       2018.08.31/21:32
                           ... written by lambda
       
       ## Disclaimer
       
       'Cool Constructs' is a series of phosts in which I document
       script snippets/patterns - sort of. It is mainly for me so
       that I remember the construct and know where to search for
       it.
       
       It's not only full of personal remarks, it is - on the whole
       - a sole personal remark... of the bloody beginner that is
       me.
       
       If you know how to program, it will, very likely, at least
       bore you.  If it does, skip the post. If it doesn't: enjoy!
       
       
       ## Die! Die! Die!
       
       In a run script for a git-daemon, I stumbled over a shell
       function named die(), which its author qsuscs used in
       combination with the test(1) utility.
       
       In case of a failing condition evaluation the test(1)
       program returns an exit status of 1 (false), but it returns
       no message to the user. qusucs used die() to make displaying
       a crafted error/help message easier, while still providing
       an exit status of 1.
       
       
       ## die()
       
       ### Meta
       
       Script: uberspace/service/git-daemon/run
       Author: qsuscs
       Source: git://qsuscs.sculptor.uberspace.de:63913/uberspace.git
       
       ### Code Snippet
       
         # helper function
         die() {
             echo "$1"
             exit 1
           }
       
           [ -x "$GIT" ] || die "\"$GIT\" not found!"
       
       
       ## Thoughts I
       
       I favour printf(1) before echo(1) or the builtin 'echo',
       like to put a frightning [!] in front of my error messages
       and like to keep the function restricted to one line, if
       possible.
       
       I like to have a redirection that funnels the STDOUT of
       printf(1) to STDERR, the system's data stream/file for
       error messages.
       
       The 'exit 1' needs to stay, because otherwise printf's 
       exit code of 0 would give the contextually unwanted
       information of a successful command evaluation - which is 
       true for printf(1), but not for the command that tiggered 
       the execution of die().
       
       Additionally - nitpicking -  I want my comments to describe
       what the function does, not was the function is.
       
       So I altered the die() slightly.
       
       
       ## Alternate Version
       
         # Display error message and exit accordingly.
         die() { printf '%s\n' "[!] $1" >&2; exit 1; }
       
       
       ## More Ponderings
       
       The original die() is definitely faster and way more simpler
       than the alternate version, which uses the more complex
       printf(1). So why use it at all?
       
       Here is the thing: If you'd use the original die() instead
       of echo/echo(1), you'd save the hassle of explicitly writing
       'exit 1'.
       
       On the side of code complexity however, using the original
       die() is neither better nor worse than using echo/echo(1).
       The syntax is the same. Also, there is no nesting you might
       need to get rid of.
       
       Example:
         [ -x "$GIT" ] || echo "\"$GIT\" not found!"; exit 1
       vs.
         [ -x "$GIT" ] || die "\"$GIT\" not found!"
       
       
       On the other hand, if you like to/have to use printf(), using
       a construct like die() saves (slightliy more) chars and adds
       simplicity to the code. Again, there is no nesting, but there
       is printf's format specifier (and the redirecion, if you like
       to separate diagnostic messages from other output).
       
       Example:
         [ -x "$GIT" ] || die "\"$GIT\" not found!"
       vs.
         [ -x "$GIT" ] || printf '%s\n' "\"$GIT\" not found!" >&2; exit 1
       
       
       ## Conclusion
       
       Using a die() limits mindless replication in any case and is
       therefore always useful.
       
       Also, it seems to me that it might be especially beneficial
       when using printf(1), because it simplifies the code and thus
       minimizes the risk of introducing errors by repeatedly
       writing only slightly more complex and longer calls.
       
       
       ## Thanks
       
       Thanks to qsuscs for opening his script to the public so that
       others could use it, learn from it and get inspired by it and
       are allowed to alter it.