2020/08/10
error, snag, bug, expert, glue, library, module, service, scriptError is basic data, throw & catch is a way to send event; An Error thrown can be a Snag or Bug, expected or unexpected; Expert code do Snag handling while Glue code don't; code packed as Module/Library/Service/Script required different focus;
Error
, throw
, and try-catch
More thought on Error, after Exot, Error handling, and Bugs May need more sorting, since not all topic is directly & naturally linked. For now may be the ".md" version will be easier to read.
Error
, throw
, and try-catch
Error
is a special kind of event, contains a message and stacktrace.
The keyword throw
cause the error to bubble up the callstack.
If the code thrown Error
is inside the try block of try {} catch (error) {}
,
the catch block will get the error and run,
else if the top callstack is reached, the program will log the error and exit.
Using try-catch
and throw
have effect similar to label
and goto
,
and often the use of goto
is considered bad, unlike the more limited if-else
,
for it can cause the code to skip/jump past hugh amount of code scopes and callstacks,
and most bug/leak happen when the skipped code contain destructor/clear-up.
Interestingly, some may abuse this goto
behavior and use it for additional control flow.
And instead of rely on throw
and rewind the callstack to try-catch
to process Error
,
there's alternative to just return/pass Error
to next function and if-else
to do the handling,
like in most callback (error, result) => {}
and functional programming.
In the end, Error
is still just basic data,
it is us who decided to honor some contract to treat it specially.
And Error
should always be well-defined, classified, can be enumerated easily,
it's a comment prepared for later debugging.
Whatever the method can reduce the complexity is fine,
but since most language choose try-catch
and throw
,
later discuss will also adopt this form of contract.
First a sync of words,
we leave existing words used in code as-is: Error, throw, try-catch
,
and use the following words for the topic:
ENOENT
, 404, bad user input, network lost, legacy protocol...We need Snag to be clearly defined and correctly handled, and try eliminate Bug all the time.
Whether an Error
is Snag or Bug depends on the actual code.
If some check code throw
a well-defined Error
,
and outer code already setup try-catch
to receive it and correctly retry,
this Error
is a Snag.
But if for the same situation, the outer code forget to trap the error,
causing it leaks to higher stack, or even to the top callstack and cause an exit,
the same Error
is a Bug.
So for an Error
end up to be Snag but not Bug,
the good Error
throw
code need to pair with in-time try-catch
code.
And for the record:
Error
will be intentionally be left unhandled to exit the program,
can still be Snagtry-catch
may consume all Error
,
causing unrelated/important-debug Error
get masked as Snag,
and this is still a BugFor the expected and unexpected Error, we use different strategy: (Error handling)
Not all code need Error handling in the first place,
most function with no external resource (IO) involved can be made 100% correct.
And it's simpler to limit and reduce the Snag & Bug scope, less work is better.
On the other side, more checks and Error
do make Bug surface quicker,
keep a balance.
Often to apply Snag handling to IO related code:
try catch
should be enough.And for general code from low to high:
throw
detailed Error
try-catch
for Bug reportAnd in short:
Next we divide the code quality:
try-catch
and let outer code do the work,
simple & quick to write, read, and change,
but may explode on many edge casesBoth quality standard have some use case, and most of the time:
Note that event Glue code should have clear structure and direct logic, and though there's less Snag handled, don't mean it should have Bugs. Messy Bugged code is just failed Bad code, and problem to be fixed.
For different usage, we group code as:
with many wiring code to perform each specific operation/action,
often long-running as separate process or remote,
have some boundary with the user code and require interact with a protocol.
Mostly a repo, may have sub-repo.
Suggest Expert code for data-facing part and whole structure/pattern/framework,Glue code for each protocol/business logic.
Need Comment, if needed add good Tooling and do Test,since most code is wiring Glue code.
only write Expert code for import part for later reuse as Module/Library.
Need some Comment for thought, reference, and the hard/weird part.For most project, 85% Expert code in Library/Module + 15% Glue code on top Service/Script may be a good choice.
So Error
can end up being Snag or Bug,
in both Expert or Glue code,
packed as Module/Library/Service/Script.
Suppose we are creating a repo for:
https://en.wikipedia.org/wiki/Exception_handling#In_software