how to never write bug

Fireship
18 Oct 202207:19

Summary

TLDRThis humorous video script offers seven universal debugging tips for developers, emphasizing the importance of reading documentation, understanding error messages, and using tools like Google and logging. It humorously touches on the rarity of bugs in hardware and the laws of quantum superposition, before diving into practical advice on using debuggers, reproducing issues, automated testing, and static analysis tools. The script concludes with a bonus tip: taking a break can lead to breakthroughs in solving stubborn bugs.

Takeaways

  • 😇 The ideal of an efficient programmer is to avoid bugs, but it's nearly impossible due to various factors.
  • 🔍 Most of the time, issues arise from one's own code or team members' contributions, with only a tiny fraction due to external libraries or hardware.
  • 📚 Reading documentation is crucial to understand what functions and methods actually do, rather than assuming based on their names.
  • 🤔 Error messages can be cryptic, but they are essential to understand and should guide debugging efforts instead of ignoring them.
  • 🔍 Google is a powerful tool for translating error messages and finding solutions, but it requires careful use to avoid overwhelming results.
  • 📝 Logging is a straightforward debugging method that involves printing data to verify the application's behavior.
  • 🐞 Debuggers are advanced tools that allow for code execution control and inspection, making it easier to pinpoint and understand bugs.
  • 🔬 Creating a minimal reproduction of a bug can help in understanding and solving the issue, and it's also useful for seeking help from others.
  • 🛠 Automated testing and test-driven development can prevent regressions and provide confidence when making changes to the code.
  • 🧩 Static analysis tools like TypeScript or ESLint can catch bugs before they become problems by enforcing code quality and type safety.
  • 💆‍♂️ Sometimes, taking a break from a stubborn bug can lead to a fresh perspective and a quicker resolution.

Q & A

  • What is the primary key to becoming an efficient programmer according to the video?

    -The primary key to becoming an efficient programmer, as mentioned in the video, is to never write any bugs, although it acknowledges that this is virtually impossible.

  • What does the video suggest is the most common cause of code not working as expected?

    -The video suggests that 99% of the time, the code not working as expected is the programmer's own code to blame.

  • What should a programmer do when encountering a bug that is not their own code's fault?

    -When a bug is not the programmer's own code's fault, they can either open an issue on the GitHub repository of the library causing the issue, fix it themselves and open a pull request, or build their own new library from scratch.

  • What does the video imply about the rarity of bugs that are due to the programming language or compiler itself?

    -The video implies that bugs due to the programming language or compiler itself are quite rare, happening only 0.0099% of the time.

  • What is the video's humorous take on the possibility of bugs being due to the laws of quantum superposition?

    -The video humorously suggests that bugs that are due to the laws of quantum superposition are extremely rare and can be considered as 'God's will' that the code shall not work, happening only 0.00099% of the time.

  • What is the first debugging tip provided in the video?

    -The first debugging tip provided in the video is to read the documentation, as programmers often guess what a method or function is doing based on its name without actually checking the documentation.

  • Why is it important to understand error messages and stack traces when debugging?

    -Understanding error messages and stack traces is important because it eliminates the guesswork in debugging and helps pinpoint the exact moment a bug occurs.

  • What is the role of Google in translating and understanding error messages during debugging?

    -Google is a powerful tool that can help translate and understand error messages by searching for keywords and relevant information, but it requires the programmer to parse the error and remove anything unique to their project for better search results.

  • What are some of the benefits of using logging for debugging?

    -Logging allows developers to print out different pieces of data from their application to manually verify that it's running as expected. It's a simple method that works and can be enhanced with various logging features like console count, timers, and table formatting.

  • How can debuggers in modern IDEs and editors help in debugging complex code?

    -Debuggers in modern IDEs and editors allow developers to observe and execute their code at scale, traverse through stack frames, pause execution at any time, and set up breakpoints without modifying the code, which makes debugging more efficient and less error-prone.

  • What is the purpose of creating a minimal reproduction of a bug?

    -Creating a minimal reproduction of a bug helps in isolating the issue and understanding the root cause. It also provides an example that can be shared with other programmers for assistance if the problem cannot be solved independently.

  • What is the significance of automated testing in the debugging process?

    -Automated testing is significant in the debugging process as it helps ensure that new code changes do not introduce bugs, provides confidence in the code's functionality, and aids in identifying and fixing existing bugs by writing tests that define the expected behavior.

  • How can static analysis tools like TypeScript or ESLint help prevent bugs in code?

    -Static analysis tools like TypeScript or ESLint help prevent bugs by analyzing the code without executing it, identifying potential issues, enforcing a type system, and improving code quality, thus making it more difficult to introduce errors.

  • What is the bonus tip provided by the video for dealing with stubborn bugs?

    -The bonus tip provided by the video is to take a break from the computer, go outside, get some exercise, and enjoy the sun. This can help refresh the mind and often leads to solving the bug upon returning to it.

Outlines

00:00

😅 The Humorous Reality of Programming Bugs

This paragraph humorously addresses the inevitability of encountering bugs in programming. It suggests that most of the time, issues arise from one's own code or from teammates, but occasionally they stem from external libraries or even the programming language itself. The speaker humorously touches on the idea that in extremely rare cases, hardware or quantum superposition might be to blame. The paragraph introduces seven universal debugging tips, emphasizing the importance of reading documentation to avoid misunderstandings about functions and methods. It also highlights the value of understanding error messages and using tools like Google for troubleshooting.

05:02

🔍 Advanced Debugging Techniques and Taking Breaks

The second paragraph delves into advanced debugging strategies, starting with the use of logging to print out data and verify the application's behavior. It then discusses the utility of debuggers in modern IDEs, which allow developers to pause execution, inspect variables, and avoid code pollution from excessive console logs. The speaker also recommends creating minimal reproductions of bugs to better understand and share them with others. Additionally, the paragraph touches on the challenges of Heisenberg bugs that cannot be reproduced and the importance of regression testing to catch new issues. The speaker introduces the concept of static analysis tools like TypeScript and ESLint to prevent bugs before they occur. Finally, the paragraph humorously concludes with a bonus tip to take a break, suggesting that stepping away from a problem can often lead to a swift resolution upon return.

Mindmap

Keywords

💡Efficient Programmer

An efficient programmer is someone who can write code with minimal errors and quickly solve problems when they arise. In the video, the concept is introduced with humor, suggesting that the key to this is to never write bugs, which is virtually impossible. The script humorously highlights the various reasons for bugs, from personal code issues to external factors like libraries or hardware.

💡Bugs

Bugs refer to errors or faults in a program that cause it to behave in unintended ways. The video script discusses the inevitability of bugs and the various sources they can come from, such as one's own code, team members' code, or external libraries. It also humorously touches on the rare cases where hardware or even 'God's will' might be to blame for a bug.

💡Documentation

Documentation in programming refers to the written instructions or comments that explain how a program works. The script emphasizes the importance of reading documentation to understand what functions and methods do, using the example of a 'get random number' function that always returns the number 3, illustrating a common misunderstanding that can be avoided by reading the documentation.

💡Error Messages

Error messages are the notifications programmers receive when something goes wrong in their code. The video script advises new programmers not to ignore these messages but to learn how to understand and act upon them, which can guide them toward solving the problem more effectively.

💡Google

Google is highlighted in the script as a powerful tool for translating and understanding error messages. It suggests that by using specific keywords and including the name of the programming language or framework, programmers can more effectively search for solutions to their problems.

💡Logging

Logging is the process of recording events that happen when a program runs. The script describes it as a simple but effective debugging technique where developers print out different pieces of data to verify the program's behavior. It mentions various console methods in JavaScript that can be used for more sophisticated logging.

💡Debuggers

Debuggers are tools that allow programmers to step through their code, observe variable states, and identify where things go wrong. The video script explains how debuggers can be more efficient than logging because they do not require altering the code with print statements and can be used to set breakpoints and inspect the code's execution flow.

💡Reproduction

Reproduction in the context of debugging refers to the ability to consistently recreate a bug with a minimal amount of code. The script suggests that creating a reproduction can help in understanding and solving the problem, and it also serves as a clear example that can be shared with others for assistance.

💡Automated Testing

Automated testing involves writing code that checks other parts of the code to ensure they function correctly. The video script discusses how automated tests can prevent regressions and provide confidence when making changes, as well as aiding in the debugging process by clarifying the intended behavior of the code.

💡Static Analysis

Static analysis is a method of examining code without executing it to find potential issues. The script mentions TypeScript and ESLint as tools for static analysis, which can prevent common bugs by enforcing a type system or identifying problematic patterns before the code is run.

💡Regression

A regression in programming is when a new change in the code causes a previously working feature to stop working. The video script describes regressions as a frustrating type of bug that can be mitigated by using automated testing to ensure that changes do not break existing functionality.

💡Break

Taking a break is suggested in the script as a bonus debugging tip. It humorously recounts personal experiences where stepping away from a problem and returning with a fresh perspective led to a quick solution, emphasizing the importance of rest and relaxation in the problem-solving process.

Highlights

The key to becoming an efficient programmer is to avoid writing bugs, but it's nearly impossible.

99% of the time, code issues are due to the programmer's own mistakes.

0.99% of the time, issues arise from teammates' code.

0.0099% of the time, bugs are due to third-party libraries from npm.

For 0.00099% of the time, the programming language or compiler is to blame.

Hardware issues are blamed for 0.00001% of the time.

0.000099% of the time, code incompatibility with quantum superposition is the issue.

Seven universal debugging tips are discussed for developers.

Reading documentation is crucial to understand what methods or functions are doing.

Error messages should be understood rather than ignored or changed blindly.

Google is a powerful tool for translating and understanding error messages.

Logging is an effective way to debug by printing out data from the application.

Debuggers in IDEs allow for observing and executing code at scale.

Creating a minimal reproduction of a bug can help in understanding and solving it.

Automated testing and test-driven development can prevent regressions and catch bugs early.

Static analysis tools like TypeScript or ESLint can prevent bugs before they occur.

Taking a break can often lead to solving a bug that was previously difficult to tackle.

The humorous suggestion of the only programming language in hell being Java.

Transcripts

play00:00

the key to becoming an efficient

play00:01

programmer is to never write any bugs

play00:04

unfortunately that's virtually

play00:05

impossible when your code doesn't work

play00:07

as expected 99 of the time it's your own

play00:10

code to blame you cannot blame all me

play00:12

however about 0.99 of the time it's the

play00:15

pulse of somebody else on your team and

play00:17

you can get pissed off at them

play00:18

accordingly

play00:20

but the other .0099 of the time it's a

play00:24

bug in some shady Library you downloaded

play00:26

from npm which means you'll have to open

play00:27

up an issue on its GitHub repo or fix it

play00:30

yourself and open a pull request or just

play00:32

build your own new library from scratch

play00:33

however the other point zero zero zero

play00:36

zero nine nine percent of the time it's

play00:37

the programming language or compiler

play00:39

itself to blame in which case you'll

play00:41

need to get in touch with some people

play00:42

much smarter than yourself who determine

play00:44

it's not a bug with a compiler but it's

play00:45

an edge case that happens

play00:48

.00099 of the time and the actual

play00:50

Hardware is to blame like one transistor

play00:52

was placed just a few nanometers to the

play00:54

left and that's why your website doesn't

play00:55

look quite right on a Blackberry but the

play00:57

hardware people say it's not their fault

play00:58

it's actually just an edge case that

play01:00

happens point zero zero zero zero zero

play01:02

zero one percent of the time where your

play01:04

code is just not compatible with the

play01:05

laws of quantum superposition due to a

play01:07

defect in the simulation in which we

play01:09

live in other words it's God's will that

play01:11

thou code shall not work luckily bugs

play01:13

like this are pretty rare and in today's

play01:15

video we'll look at seven universal

play01:16

debugging tips that every developer

play01:18

should be familiar with the first tip is

play01:20

to read that may seem stupidly obvious

play01:22

but programmers want to get things done

play01:24

quickly and tend to be overconfident in

play01:26

their understanding of what the hell the

play01:27

computer is actually doing what will

play01:29

often happen is you'll guess what a

play01:31

method or function is doing based on its

play01:33

name but remember there are only two

play01:35

hard problems in computer science naming

play01:37

things and coming up with new original

play01:39

jokes for YouTube tutorials like there

play01:41

might be a function called get random

play01:43

number you automatically think it's

play01:44

always going to return a different

play01:45

number but when you call it it returns

play01:47

three every time it's not a bug because

play01:49

had you read the documentation you know

play01:51

that 3 is a random number that was

play01:52

chosen by the developer in advance who

play01:54

guaranteed that it was 100 random when

play01:56

he chose it with the roll of a die

play01:57

reading documentation in advance is

play01:59

important but developers also tend to

play02:01

skip right over error messages when

play02:03

first learning to code you get these big

play02:05

cryptic error messages that seem totally

play02:06

overwhelming so instead of reading it

play02:08

you just try to change up your code

play02:09

until the message goes away that might

play02:11

work sometimes but you'd be much better

play02:13

off learning how to understand the error

play02:15

messages and go through a stack Trace

play02:17

because that will eliminate the

play02:18

guesswork now most of the time even

play02:19

after you read the error you'll still

play02:21

have no idea what it's talking about

play02:22

luckily there's a very powerful tool

play02:24

that can help you translate it called

play02:25

Google but when it comes to error

play02:27

messages you don't want to just paste

play02:29

the entire error message with the full

play02:30

stack Trace into Google Googling as a

play02:32

software engineer is an art form you'll

play02:34

want to First parse the error and remove

play02:35

anything that's unique to your actual

play02:38

project Google is pretty good at

play02:39

ignoring things that are unnecessary but

play02:41

the more you can do to point it in the

play02:42

right direction the better if the

play02:43

keywords on the air are pretty generic

play02:45

you might also want to add the name of

play02:46

your language or framework to the

play02:48

beginning just to limit the number of

play02:49

results I couldn't imagine living

play02:51

without Google and stack Overflow but at

play02:53

some point you'll hit a wall and have to

play02:54

do some good old-fashioned honest

play02:56

programming the easiest way to approach

play02:58

debugging is with logging where you

play03:00

simply print out different pieces of

play03:01

data from your application to manually

play03:03

verify that it's running as expected

play03:05

it's not very sophisticated but it works

play03:07

a JavaScript beginner might use console

play03:09

log to print out every single variable

play03:11

but your logger probably has more tricks

play03:13

up its sleeve like JavaScript has

play03:15

console count to keep a running counter

play03:17

in your code or timer to measure time

play03:19

and table to format things in a more

play03:21

user-friendly way you can also log at

play03:23

different levels like info warning and

play03:25

error to organize the priority of this

play03:27

data and use console dir to display it

play03:29

as a hierarchy the bottom line is that

play03:31

logging is a perfectly acceptable way to

play03:33

debug and you should take some time to

play03:34

learn the full API of a logger in your

play03:36

language to maximize its efficacy the

play03:38

problem with logging though is that it

play03:40

gets harder and harder to do as your

play03:41

code grows larger and more complex

play03:43

luckily modern Ides and editors have

play03:45

tools that can help you observe and

play03:47

execute your code at scale these tools

play03:49

are called debuggers and there's one

play03:51

integrated directly into vs code it

play03:53

allows you to Traverse through frames in

play03:54

the coal stack which means you can

play03:55

pinpoint the exact moment a bug occurs

play03:57

it's possible to pause the execution of

play03:59

your code at any time using a debugger

play04:01

statement or better yet you can use the

play04:03

IDE to set up breakpoints that don't

play04:05

require any modifications to the actual

play04:07

code in addition it eliminates the need

play04:09

to pollute your code with console logs

play04:11

everywhere because instead you can just

play04:12

add log points by clicking next to the

play04:15

line of code that you want to log so

play04:16

basically a debugger is just a more

play04:18

sophisticated way to run and inspect

play04:20

your code eventually you'll find a bug

play04:22

that you just cannot fix when that

play04:24

happens it can sometimes help to not

play04:26

focus on a solution but figure out a way

play04:28

to reproduce the problem with a minimal

play04:30

amount of code like start an entirely

play04:32

new project where the only goal is to

play04:33

make this problem happen and bonus

play04:35

points if you can reproduce it somewhere

play04:36

like stack Blitz where anyone can run it

play04:38

instantly in a browser creating a

play04:40

reproduction has several benefits one

play04:42

there's a good chance you'll solve the

play04:43

problem when going through it and two if

play04:45

you don't find the root problem you now

play04:47

have an example that you can share with

play04:48

better programmers if you can't reliably

play04:50

reproduce a problem it's nearly

play04:52

impossible for anyone else to help you

play04:54

out unfortunately not every bug can be

play04:56

reproduced which is called a Heisenberg

play04:58

there's a great story about the Crash

play04:59

Bandicoot video game where the load say

play05:01

feature would work perfectly almost

play05:03

every time but every once in a while it

play05:04

would time out and wipe out the entire

play05:06

memory card after six weeks of trying to

play05:08

debug this one issue they were

play05:10

eventually able to reproduce it by

play05:11

wiggling the PS1 controller a certain

play05:13

way to corrupt the memory card it was

play05:15

one of those rare cases where the

play05:16

hardware was at fault another very

play05:18

common type of bug that is very

play05:19

frustrating is one that happens after

play05:22

your code was working perfectly before

play05:23

this is called a regression which means

play05:25

you wrote some new code that [ __ ]

play05:27

something up at a certain point it

play05:28

becomes impossible for a human to verify

play05:31

that everything works properly after

play05:32

every code change and that's where

play05:34

automated testing comes in or test

play05:36

driven Development I've made many videos

play05:37

in the past about testing but the

play05:39

general idea is that you write code to

play05:41

make sure that your other code is

play05:43

working as expected most importantly it

play05:45

gives you the confidence that you're not

play05:46

introducing new bugs when you make

play05:48

changes but it can also help in the

play05:50

debugging process when you encounter a

play05:51

bug instead of trying to fix it right

play05:53

away you can go straight to writing a

play05:55

test this forces you to be explicit

play05:57

about what you're actually trying to

play05:58

make the code do and in the process

play06:00

you'll likely learn things about your

play06:01

code that you didn't understand

play06:02

previously and finally after you get the

play06:04

test to pass you'll be protected from

play06:06

reintroducing the same bug another way

play06:08

to catch bugs before they become a

play06:10

problem is to use a tool that performs

play06:12

some type of static analysis on your

play06:13

code in the case of JavaScript the most

play06:15

common way to do that is to use

play06:17

typescript which adds an entire type

play06:19

system on top of the language which may

play06:20

makes it far more difficult to introduce

play06:22

stupid bugs like using the wrong

play06:24

variable name somewhere or assigning the

play06:25

wrong data type because the typescript

play06:27

compiler won't even let you run your

play06:28

code when you make mistakes like this

play06:30

the drawback is that typescript can be a

play06:32

pretty big investment and you may not

play06:33

want to add it to your code base in that

play06:35

case consider adding eslint by itself to

play06:37

your project it also performs static

play06:39

analysis and attempts to improve your

play06:40

code quality and requires very little

play06:42

effort on your part that gives us seven

play06:44

different ways to debug code but I have

play06:46

one more bonus tip for you which is to

play06:48

turn off your computer and take a break

play06:49

if I had a dollar for every time I was

play06:51

fighting a bug then went and took a

play06:53

break and came back and immediately

play06:55

solved it I would have a shitload of

play06:56

dollars I like to go outside get some

play06:58

exercise and bask in the Sun and enjoy a

play07:01

smooth and flavorful Marlboro cigarette

play07:03

Marlboro may surprise you it's the

play07:05

filter cigarette with the unfiltered

play07:07

taste on second thought you may want to

play07:09

skip the cigarette unless your goal is

play07:11

to die a horrible agonizing death and if

play07:13

you go to hell the only programming

play07:14

language they have down there is Java

play07:16

thanks for watching and I will see you

play07:17

in the next one

Rate This

5.0 / 5 (0 votes)

Related Tags
Debugging TipsProgramming HumorCode EfficiencySoftware EngineeringError MessagesStack OverflowGoogle SearchesLogging TechniquesIDE DebuggersAutomated TestingStatic AnalysisTypeScriptESLintRegression BugsHeisenberg BugsDeveloper BreaksHealth Warning