DEV Community

Srividya Mysore Venkatesh
Srividya Mysore Venkatesh

Posted on

Chapter 8: Bugs and Errors

This is the 8th blog in the series of 21 blogs that bring in all the concepts that I have learned while reading the book Eloquent JS.

When I started programming I dreaded errors. But I have come to realize that they make me a better programmer.

laws in computer programs are usually called bugs. You can roughly categorize bugs into those caused by the thoughts being confused and those caused by mistakes introduced while converting thought to code. The former type is generally harder to diagnose and fix than the latter.

Its concept of bindings and properties is vague enough that it will rarely catch typos before actually running the program. And even then, it allows you to do some clearly nonsensical things without complaint. But, There are some things that JavaScript does complain about. Writing a program that does not follow the language’s grammar will immediately make the computer complain. Other things, such as calling something that’s not a function or looking up a property on an undefined value, will cause an error to be reported when the program tries to perform the action.

The process of finding mistakes—bugs—in programs is called debugging.

Strict Mode:
This is more like that strict teacher who would invigilate an exam...who looks into finer things than a normal teacher...although a normal teacher would catch you if you commit an offense.

JavaScript can be made a little stricter by enabling strict mode. This is done by putting the string "use strict" at the top of a file or a function body.

Normally, when you forget to put let in front of your binding, as with counter in the example, JavaScript quietly creates a global binding and uses that. In strict mode, an error is reported instead.

Strict mode does a few more things. It disallows giving a function multiple parameters with the same name and removes certain problematic language features entirely.

Basically helps spot a problem efficiently.

Some languages want to know the types of all your bindings and expressions before even running a program. They will tell you right away when a type is used in an inconsistent way.

JavaScript considers types only when actually running the program, and even there often tries to implicitly convert values to the type it expects, so it’s not much help.

When the types of a program are known, it is possible for the computer to check them for you, pointing out mistakes before the program is run. There are several JavaScript dialects that add types to the language and check them. Like TypeScript!!

Assertions are checks inside a program that verify that something is the way it is supposed to be. They are used not to handle situations that can come up in normal operation but to find programmer mistakes.

Testing and Debugging:
If the language is not going to do much to help us find mistakes, we’ll have to find them the hard way: by running the program and seeing whether it does the right thing.

Doing this by hand, again and again, is a really bad idea. Not only is it annoying.

Computers are good at repetitive tasks, and testing is the ideal repetitive task. Automated testing is the process of writing a program that tests another program.

Once you notice there is something wrong with your program because it misbehaves or produces errors, the next step is to figure out what the problem is.

Sometimes it is obvious. The error message will point at a specific line of your program, and if you look at the error description and that line of code, you can often see the problem.

But not always. Sometimes the line that triggered the problem is simply the first place where a flaky value produced elsewhere gets used in an invalid way.

That is when debugging is the way to go!. to sit and work through the code. Probably add breakpoints or "console.log"-ing at frequent intervals to figure out what is happening.

When you commit mathematical sins like 0 divided by 0, infinity -1, infinity + infinity....and many such sins. The computer is like ummm....I don't know what to do. what we would like to do is just stop what we are doing and immediately jump to a place that knows how to handle the problem. This is what exception handling does.

When it gets impossible for the code to run through, it 'throws' an exception that is 'caught' in another section of the code. catching is done by encasing statements in a try block.

So three words to remember here are try, catch, throw!

Happy Reading!

Top comments (1)

yusufcodes profile image

Nice read :) Need to use ‘try ... catch’ statements more!