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.
The process of finding mistakes—bugs—in programs is called debugging.
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.
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.
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!
Top comments (1)
Nice read :) Need to use ‘try ... catch’ statements more!