Let's say I'm writing a bash script.  It will make 5 calls.  Three of
the calls are expected to return zero on success, one is expected to
return 1 on success, and one whose return code I don't care about.
Here's this script written with set -e:

#!/bin/bash
set -e
call0
call1
call2
call3 || ret=$?
if ((ret != 1)); then
   exit 1
fi
call4 || true

Here's the same script written without the use of set -e, following the
suggestion that all return codes are checked individually

#!/bin/bash
call0 || exit 1
call1 || exit 1
call2 || exit 1
call3
if (($? != 1)); then
   exit 1
fi
call4

How exactly is the second script an improvement over the first?  I'm
seriously asking because I really don't understand.  As far as I can
tell, it is generally agreed upon that calls return zero on success.
Using set -e is equivalent to explicitly making this single simplifying,
and reasonable, assumption.  Clearly there are a couple of circumstances
where this is not true, but it is certainly true in the majority of
cases.  If it's true 90% of the time, then using set -e means we don't
have to explicitly check those 90% of the calls.  If we don't use set -e
then we have to explicitly check those 90% of calls.  AND we still have
to check the return of the remaining 10% whose return we do care about.
As far as I can tell, not using set -e is equivalent to writing a lot
more code for no particular reason.

I'm honestly curious to hear what you guys see as wrong with this
reasoning.

Attachment: pgptg2laRNXiD.pgp
Description: PGP signature

Reply via email to