err != nil Is GOOD? (And Why)

ThePrimeTime
23 Dec 202307:19

Summary

TLDRThe speaker discusses the common misconceptions around error handling in programming languages, specifically comparing JavaScript and Go. They argue that while Go requires explicit error handling, which may seem tedious, it leads to more reliable code. JavaScript, on the other hand, often hides errors, resulting in potential unhandled exceptions and lower reliability. The speaker emphasizes the importance of recognizing language weaknesses and considering error handling as a crucial part of programming, rather than dismissing it as annoying.

Takeaways

  • πŸ˜• The speaker is addressing a common frustration with error handling in JavaScript, particularly when comparing it to Go's approach.
  • πŸ” They introduce a hypothetical JavaScript scenario with async functions 'Foo' and 'Bar' to illustrate the point.
  • πŸ€” The speaker suggests that while Go's error handling might seem cumbersome, it encourages developers to think about potential errors upfront.
  • πŸ‘Ž The script points out that JavaScript's error handling can be more frustrating due to its implicit nature, often leading to overlooked errors.
  • πŸ“ The transcript discusses the technical differences in how Go and JavaScript handle errors, with JavaScript requiring additional checks for null or undefined values.
  • πŸ› οΈ The speaker emphasizes the importance of proper error handling in JavaScript to avoid runtime issues like unhandled promise rejections.
  • πŸ€– The comparison between Go and JavaScript is used to highlight that error handling, while annoying, is crucial for reliable code.
  • 😌 The speaker disagrees with the common sentiment that Go's error handling is bad, arguing that it's just more explicit about potential issues.
  • πŸ“ˆ They mention that JavaScript services often have lower reliability due to insufficient error handling practices.
  • 🌐 The script touches on the broader topic of programming language design and how it influences developer habits and code reliability.
  • πŸ“š The speaker concludes by encouraging developers to recognize the weaknesses of the languages they use and to program with awareness of those limitations.

Q & A

  • What is the main topic of the video script?

    -The main topic of the video script is the comparison of error handling in JavaScript and Go, with a focus on the challenges and perceptions of error handling in these languages.

  • What does the speaker suggest about error handling in Go?

    -The speaker suggests that Go's error handling is often criticized as being verbose and annoying, but it forces developers to think about potential errors upfront, which can lead to more reliable code.

  • What is the speaker's view on JavaScript error handling?

    -The speaker believes that JavaScript error handling is often overlooked or 'hidden', leading to less reliable services and more difficulty in correctly programming, especially when handling all error conditions.

  • What is the issue with JavaScript's 'await' keyword as discussed in the script?

    -The issue with JavaScript's 'await' keyword is that it can throw an error at any point, and developers may not always handle these errors, leading to potential runtime issues.

  • Why does the speaker find the process of error handling in JavaScript tedious?

    -The speaker finds JavaScript error handling tedious because it often requires additional code for error checks and handling, which can complicate the flow of the code and the function signatures.

  • What is the speaker's opinion on Rust's error handling?

    -The speaker appreciates Rust's error handling, mentioning that its use of the Result object and the monadic approach to results allows for more flexibility and better error management.

  • What is the 'process.unhandledRejection' mentioned in the script?

    -'process.unhandledRejection' is an event in Node.js that is emitted when a Promise is rejected and no error handler is attached to it, indicating an unhandled error scenario.

  • What does the speaker mean by 'raw dog Jon parse'?

    -The phrase 'raw dog Jon parse' is a colloquial and humorous way of saying that many developers parse JSON without handling potential errors, which is not a good practice.

  • What is the speaker's advice regarding comparing error handling across different programming languages?

    -The speaker advises not to quickly dismiss another language's error handling as bad just because it looks worse at first glance. Instead, understand the differences and recognize the weaknesses in each approach.

  • Why does the speaker still write TypeScript despite the issues discussed?

    -The speaker acknowledges the weaknesses of TypeScript but continues to use it because they recognize its benefits and are aware of its limitations, allowing them to program effectively.

  • What is the speaker's final message about error handling in programming?

    -The speaker's final message is that error handling is an essential part of programming that should not be ignored or considered annoying. It is better to handle errors upfront to ensure more reliable and robust code.

Outlines

00:00

πŸ” Addressing Error Handling in JavaScript vs. Go

The speaker discusses common frustrations with error handling in Go, specifically the annoyance around checking if errors are nil. Using a JavaScript test file for comparison, they highlight how error handling differs between Go and JavaScript. In Go, every function call must explicitly handle errors, which is seen as tedious but necessary for robust code. JavaScript's async/await error handling is compared, noting that it often leads to unpredictable behavior and additional error states. The speaker argues that while Go's approach seems cumbersome, it ultimately results in more reliable code because it forces developers to handle errors explicitly.

05:01

πŸ”„ The Challenges of JavaScript Error Handling

The speaker continues to explore the difficulties of error handling in JavaScript. They highlight how JavaScript often hides errors, leading to unhandled rejections that need to be caught at a higher level, which complicates the error recovery process. The speaker praises Rust's approach to error handling with its Result object and monadic patterns, suggesting it offers a better balance. However, they conclude that despite Go's error handling being annoying, it is more reliable than JavaScript's. They emphasize the importance of recognizing the weaknesses of each language, advocating for informed choices rather than dismissive judgments.

Mindmap

Keywords

πŸ’‘Error Handling

Error handling is the process of responding to the occurrence of an error within a program. In the video, the speaker discusses the complexities of error handling in JavaScript compared to Go, emphasizing that Go forces developers to consider errors at the time of writing code, which can lead to more reliable software. The speaker uses the phrase 'go error handling' to illustrate the proactive approach Go takes, contrasting it with JavaScript's more reactive and often overlooked error management.

πŸ’‘Async Functions

Async functions are a type of function in JavaScript that allow asynchronous operations to be written in a synchronous manner. The video script mentions async functions like 'Foo' and 'Bar', which are used to demonstrate the process of handling asynchronous operations and the potential for errors when using 'await' within these functions.

πŸ’‘Await

The 'await' keyword is used in conjunction with async functions to pause the execution of the function until a Promise is resolved or rejected. In the script, the speaker discusses the use of 'await' in front of function calls like 'Foo' and 'Bar', and how it can lead to errors if not properly handled.

πŸ’‘TypeScript

TypeScript is a superset of JavaScript that adds static types to the language. The speaker mentions TypeScript, indicating that despite recognizing its weaknesses, they still write TypeScript code. TypeScript is relevant in the context of the video as it provides a way to add type safety and potentially reduce runtime errors in JavaScript code.

πŸ’‘Go

Go, often referred to as Golang, is a statically typed, compiled programming language designed at Google. The video contrasts Go's error handling with JavaScript's, suggesting that Go's approach is more explicit and upfront about potential errors, which can lead to more robust and reliable code.

πŸ’‘JavaScript

JavaScript is a high-level, interpreted programming language commonly used for client-side web development. The script discusses JavaScript's error handling mechanisms, or lack thereof, and how they can lead to less reliable code compared to languages like Go.

πŸ’‘Promises

Promises in JavaScript are objects that represent the eventual completion or failure of an asynchronous operation. The speaker mentions that the return value of async functions can be a promise, which adds complexity to error handling and the function's signature, as seen in the script's discussion about handling 'null' or error cases.

πŸ’‘Try-Catch

Try-catch blocks are used in JavaScript to handle exceptions. The video script mentions the need for try-catch blocks when calling async functions to handle potential errors that might be thrown, illustrating the reactive nature of JavaScript's error handling.

πŸ’‘Reliability

Reliability in software development refers to the ability of a system to perform its required functions under stated conditions for a specified period of time. The speaker argues that Go's approach to error handling contributes to higher reliability compared to JavaScript, where errors are often overlooked or handled inadequately.

πŸ’‘Rust

Rust is a systems programming language that focuses on performance, safety, and concurrency. The script briefly mentions Rust as an example of a language that handles errors in a way the speaker finds more appealing than JavaScript, specifically referencing Rust's 'Result' object for error handling.

πŸ’‘JSON Parse

JSON parse is a JavaScript function used to convert a JSON string into a JavaScript object. The speaker uses JSON parsing as an example of a common operation in JavaScript that requires error handling, as it can throw an error if the JSON is invalid, highlighting the need for proactive error management.

Highlights

The speaker introduces a comparison between error handling in Go and JavaScript, specifically addressing the common misconceptions about Go's error handling being cumbersome.

A test file named 'test.js' is used to demonstrate the differences in handling asynchronous functions in JavaScript compared to Go.

The concept of 'await' in JavaScript is discussed, highlighting the uncertainty of error handling when using 'await'.

The speaker suggests that writing Go in a JavaScript file is not practical, but uses it as a starting point for the discussion.

The equivalence of error handling between Go and JavaScript is explored, emphasizing the need for special error handling in both languages.

The issue of variable definitions and types in JavaScript is discussed, especially when dealing with null or undefined values.

The speaker criticizes the complexity of error handling in JavaScript, particularly when dealing with promises and null values.

A comparison is made between the error handling in Go and JavaScript, with the assertion that JavaScript tends to have lower reliability due to poor error handling practices.

The speaker argues that Go's error handling, despite being seen as verbose, forces developers to think about errors proactively.

The concept of 'process.unhandled rejection' in JavaScript is mentioned, illustrating the potential for top-level errors that are difficult to manage.

The speaker shares personal thoughts on the tediousness of error handling in JavaScript and the importance of recognizing its weaknesses.

The transcript touches on the use of TypeScript and the speaker's recognition of its limitations.

The speaker emphasizes the importance of not being ignorant about the weaknesses of programming languages and to understand the trade-offs.

A critique of the common complaint about 'if err equals nil' in Go, suggesting that the speaker does not find it as bad as others might.

The transcript concludes with the speaker's belief that contributing to Go is easier and more reliable due to its upfront error handling requirements.

The speaker reflects on the frequency of errors in code, suggesting that JavaScript's error handling often makes them less noticeable.

The final thoughts include a comparison between the error handling in Go and JavaScript, with a preference for Go's approach due to its explicitness.

Transcripts

play00:00

we're going to do something different

play00:01

here okay I know this is going to be a

play00:02

little bit different but I wanted to

play00:03

talk about a small difference that I see

play00:05

all the time because I see a lot of

play00:07

people always getting super hot and

play00:09

bothered about go air handling if air

play00:11

equals nil and I want to just take a

play00:13

little moment and address it just for a

play00:14

second okay so I'm just going to have

play00:16

this test file and we're going to call

play00:17

it test.js okay and let's say I have an

play00:19

async function you know Foo right and

play00:22

then I have another one uh bar okay and

play00:26

I wanted to do something here with that

play00:27

so I have async function main this and I

play00:30

could go await Foo and I could go await

play00:33

bar now in goand what would this look

play00:36

like this would look something like this

play00:38

uh right Main and it would look

play00:39

something like this where I'd actually

play00:41

have this right and it would look

play00:44

something you know right can we agree

play00:47

this is effectively what it would look

play00:49

like

play00:50

yeah yeah it would look something like

play00:53

this right and that's kind of annoying

play00:56

can we all agree that we don't like that

play00:59

we we like this typically what people

play01:02

always say about go is this

play01:04

sucks this is awesome but there's kind

play01:07

of a lie that you're being told right

play01:09

here okay first off you have some

play01:11

options here right you have some real

play01:13

options here obviously writing go in a

play01:15

Javascript file doesn't you know it

play01:17

doesn't count but just deal with it okay

play01:19

you could do something like

play01:20

this

play01:23

but which one errored remember anytime

play01:27

there's an await an error can be thrown

play01:30

you don't know you just simply don't

play01:33

know so that means if you wanted to

play01:34

create the equivalent behavior from go

play01:37

to Java Script what you would have to do

play01:39

is something like

play01:50

this this would be the equivalent it

play01:52

gets even worse though because if you

play01:54

had a value come out of Fu and bar right

play01:57

if you had value air uh uh Fu Val uh

play02:02

right here oh no no wait go does not go

play02:04

does not do that if you had these two

play02:06

things right here right BAR value and Fu

play02:08

air right and then I could do something

play02:10

like this return uh oh gosh I have these

play02:12

things backwards just deal with it just

play02:14

deal with it let's just say we had

play02:17

something like this to make the same

play02:20

thing happen in this you would actually

play02:22

have to do it a bit different right if

play02:23

you had to do some sort of special error

play02:24

handling right uh some special error

play02:28

handling right whatever that special air

play02:30

handling is it doesn't really matter

play02:31

what it is we'd have to do the same

play02:33

special air handling same special air

play02:35

handling I don't know return uh return

play02:37

null right we'd have to return null in

play02:39

both these situations not only that we'd

play02:41

have to have let Fu Val be right here

play02:43

which means that we're going to have to

play02:45

have some sort of type number or null

play02:49

right because now you have to have like

play02:50

null or you'd have to do undefined right

play02:52

you'd have to do some sort of definition

play02:54

going on here Fu Val equals this you'd

play02:57

have to Define it on the outside and

play02:59

then make it go on the inside you're

play03:01

going to have to do the exact same thing

play03:03

right here right it's like that's kind

play03:04

of annoying right that's kind of

play03:07

annoying I don't want to have to do that

play03:09

equals this and then you can return Fu

play03:12

plus bar Val but here's the worst part

play03:13

is now what does your return statement

play03:15

look like well your return statement is

play03:17

promise or

play03:19

null right it like changes the signature

play03:22

these errors not only have huge change

play03:25

in how your flow of code works it also

play03:27

has a huge change in what your signature

play03:29

is going to

play03:30

due to like the return value so now your

play03:32

outside when calling it also has to

play03:35

perform the exact same thing it not only

play03:37

has to do a try catch for potential

play03:39

errors thrown just in case there's an

play03:41

error thrown because they don't know if

play03:42

you handle every error and maybe you

play03:43

don't handle every single error but on

play03:46

top of it they also have to handle like

play03:48

this null case that you might have to

play03:51

handle right so now it went from a

play03:53

two-state problem to a three-state

play03:55

problem and down here in the goand it's

play03:57

not cuz what would what would the

play03:59

function look like the function well

play04:00

based on whatever this is its return

play04:03

value looks something like error number

play04:05

right or int

play04:07

right and so it's going to have the same

play04:09

it it has the exact same like experience

play04:12

no matter what and so you go into the

play04:14

situation without all that it's like I'm

play04:16

just saying when people say go air

play04:19

handling is bad you got to understand

play04:21

the

play04:22

difference what are you comparing it to

play04:25

there's a reason why JavaScript Services

play04:27

tend to have extremely low

play04:30

n reliability right they're not coming

play04:33

in with 59 they're just not because you

play04:35

have to have an intense amount of air

play04:37

handling everywhere and nobody does that

play04:41

nobody does that every time you Json

play04:44

every time you Json parse you need to

play04:46

handle a potential error so many people

play04:48

just raw dog Jon pars I'm just saying

play04:51

when people say go air handling sucks I

play04:54

simply disagree I say it just forces you

play04:57

to think about errors when errors can

play05:01

happen JavaScript they just hide the

play05:03

errors error is not going to happen you

play05:06

don't worry you know the worst part

play05:07

about this whole thing is that at some

play05:09

point you have something like this

play05:10

process. un unhandled rejection right

play05:13

you get some sort of top level error

play05:16

Catcher And you have to recover from

play05:18

that you have to reset some State

play05:21

somewhere

play05:24

what I mean good luck that's difficult

play05:28

anyways these are just some thoughts I

play05:29

have about air handling in this because

play05:31

I hear this all the time like do I like

play05:32

doing this I don't like doing this I

play05:34

think it's tedious and I think it's

play05:35

annoying I think rust does a much better

play05:37

job with the result object I think the

play05:39

monatic approach of results and being

play05:41

able to morph them an and then being

play05:43

able to do a okay filter map and all the

play05:45

different combinations you can do is

play05:48

absolutely fantastic but at the end of

play05:50

the day this annoyingness right here is

play05:53

just significantly better than whatever

play05:55

this annoyingness looks like it is very

play05:58

very very difficult

play06:00

to

play06:01

correctly program JavaScript so that you

play06:04

handle all err conditions anyways there

play06:06

you go just my thoughts JS errors are

play06:09

very difficult it's very tedious the

play06:11

problem is I just don't want you to fall

play06:12

into this habit of thinking that some

play06:13

other language is bad because it

play06:15

immediately looks worse on the outside I

play06:17

will tell you by that at the end of the

play06:19

day you will find contributing to go

play06:22

easier and more reliable because it lets

play06:24

you know ahead of time ah I got some

play06:27

errors I need to handle I'll take care

play06:28

of these errors

play06:30

annoying but man wow there's a lot of

play06:31

Errors I didn't realize how often code

play06:33

that I write can error this seems kind

play06:35

of wild I don't remember my code ever

play06:37

ering that much with JavaScript oh it

play06:39

does it's no different it's identical

play06:44

you just don't know you're dead like

play06:47

that's the only difference all right the

play06:49

name is I still write typescript all the

play06:52

time I'm not saying don't write it I I'm

play06:54

just saying I I don't love it I

play06:56

recognize its weaknesses okay and you

play06:58

should recognize the weaknesses too and

play06:59

then you can continue to program it but

play07:01

I don't want you to be someone that's

play07:02

just simply ignorant or say stupid

play07:04

things like if air equals nil is just

play07:05

the worst is it the worst is it the

play07:10

worst I don't know I don't

play07:15

know I don't know a jet

Rate This
β˜…
β˜…
β˜…
β˜…
β˜…

5.0 / 5 (0 votes)

Related Tags
Error HandlingJavaScriptGoCode ReliabilityAsync FunctionsTypeScriptProgrammingLanguage ComparisonSoftware DevelopmentConcurrency