What's worse? `NullPointerException` or `try (error) {...} catch {// do nothing}`?
Tags: programming
Published: 2024-05-06 19:56:35

Sir Charles Antony Richard Hoare has an [excellent presentation](https://www.youtube.com/watch?v=YYkOWzrO3xg) on why `null` was a bad idea.

There are even programming languages that try to eliminate the very problem of receiving a random `null` value out of blue: [Elm is such language](https://dev.to/marciofrayze/why-is-elm-such-a-delightful-programming-language-2em8#no-null-or-undefined-references).

## In practice
Usually when I encounter a `null` related error the fix were kinda straightforward:

1. Check the stack trace
2. Read the code in the location
3. Either understand it immediately (_"Oh, me stupid donkey, I tried the wrong parent property!"_) or put a debug point in.
4. Debug the code.
5. Fix and cover the error with test cases.

So, on average, I would say these are maybe 20-minute long adventures.

## When you swallow the error on the other hand...
Let's see this code:

```javascript
try {
  const result = await apiCall();
  const unboxedResult = await result.json();

  sendToSomeoneElse(unboxedResult);
} catch {
  // nothing here!
}
```
Now **this** is way worse! I have no clue why `sendToSomeoneElse` was not called. Unless I'm actively developing this particular block of code I would not have any suspicion that there was an error in the first place!

### The legendarily bad `angularjs` template
There was 2 key design mistakes in version 1 of `angularjs` that caused countless hours of fruitless, laborious debugging sessions.

#### 1. Breaking convention of `html`
When you read this: `<div data-something="This is a string!">something</div>` you know that the `data-something` attribute is now having a `"This is a string!"` string.

In first version of `angular` you could write this:
```html
<div ng-repeat="name in names" ng-include src="'template.html'">
```
Note the moment where the convention is broken: `"'template.html'"`

Even though we expect the HTML attribute to be parsed automatically to a `string` value, the `angular` engine differs here: if it is not enclosed in `'` single-quotes, it tries to interpret it as an expression or - simply - as a variable.

## 2. All templating errors were swallowed
Now this is where the real problem started. In case you forgot the single-quotes (and _many, many_ people forgot it), you would now have an invalid expression. However the decision to swallow or templating errors (for whatever reason) meant...

> you have had these innocent looking templates that just did not work as you expected!

I still remember vividly when my colleague from another team decided to escalate the question of _"Why does not this angular snippet work???"_ to the "frontend guild". I only answered it immediately: you missed the single-quotes. The mind of my colleagues were blown, how the hell did I know it so quickly?

The reason was that in my previous workplace, with my previous team I had burnt my fingers with this very thing. We also had no clue what was going on, so:

- I wasted many hours alone.
- I wasted then my team members' time to debug the issue.
- We then escalated to a larger group of devs to figure the issue and wasted their time...
- Until someone, just like me later, knew that this is a typical `angular` error.

So I conclude swallowing errors is a grievous mistake and according to my anecdotic evidence, wastes more time than the well-established `NullPointerException`.