Learn Node-SQLite - Lesson 10 Code Challenge 4

Learn Node-SQLite - Lesson 10 Code Challenge 4

Hi all,
Just a little thing but would love another opinion or some clarification. This exercise asks for a simple piece of ‘error’ code , “Log the error to the console if it exists, otherwise log the retrieved rows”.
After trying all the ways of previous lessons in Learn Node SQLite I finally succumbed to asking for the solution.
The solution in this simple ‘if’ statement was to include the ‘else’ statement. This floored me because even though I know it is correct syntax, not once in the entire Learn Node SQLite section has the ‘else’ statement been used. The HINT of this challenge referred me to a previous lesson for the correct syntax and nowhere in this HINT lesson can you find the word ‘else’.
The syntax taught throughout eliminates the else statement and is written as:

//my incorrect attempt as per previous lessons in NODE-SQLite

db.all('SELECT * from NonexistentTable', (err, rows) => {
if(err){
  console.log(err)
};
  console.log(rows);
});

//solution
db.all('SELECT * from NonexistentTable', (err, rows) => {
   if (err) {
   console.log(err);
 } else {
   console.log(rows);
 }
});

Is the removal of the ‘else’ statement for errors considered acceptable as per the lessons in Learn Node-SQLite but not in its challenges lesson? What is the more acceptable syntax?
Any thoughts on this would be appreciated.
SD

I would not call it acceptable to remove the else statement.

The diffrence between the two functions are as followed.

//my incorrect attempt as per previous lessons in NODE-SQLite

db.all('SELECT * from NonexistentTable', (err, rows) => {
if(err){
  console.log(err)
};
  console.log(rows);
});

here what you are saying is:

  • If there is a error, print it.
  • print rows
//solution
db.all('SELECT * from NonexistentTable', (err, rows) => {
   if (err) {
   console.log(err);
 } else {
   console.log(rows);
 }
});

here what the solution is saying is:

  • if there is a error print it. else(otherwise) print the row.

As you can see your attempt does two things. Since console.log(rows); is not inside a else statement it will always try to print row.

The solution however does one or the other.

Now consider in your example that the if statement is true and an error “no rows found” is shown. after it is done printing the error it will try to print the row. But there is no row to print. Now the whole program go’s boingus because what does it need to print ?

Thanks biirra for your comments. I understand what this code is doing now.
As two other alternatives, if I used ‘return’ or ‘throw’ my understanding is that both of these options would stop the last line from trying to execute and achieve the same thing? For example…

db.all('SELECT * from NonexistentTable', (err, rows) => {
if(err){
  return console.log(err)
};
  console.log(rows);
});

//*or*

db.all('SELECT * from NonexistentTable', (err, rows) => {
if(error){
  throw error;
};
  console.log(rows);
});

If you have any thoughts on this I would appreciate your help…
SD

Here your function will stop if a error is found. But the thing is, you should use return when the function actually needs to return something. In this function you are trying to return a function (console.log("")). But after you have returned it your not doing anything with it.
I am honestly not sure what the function is going to do after it returns it.
You should test if console.log() runs if you return it, cause i expect it not to.

Personally i would change the function like this if return was needed.

var result = db.all('SELECT * from NonexistentTable', (err, rows) => {
if(err){     
  return err
};
  return rows;
});
console.log(result);

Note that console.log() is no longer part of the function.

this will say:

  • if there is a error, return it. Otherwise return the rows you found.
  • when your done with the function whatever is in result print it to the console.

Here is some documentation on using return.

Your second option:

This way is a little dangerous to do if the error does not necessarily need to break the code.
What throw does is stop everything until this error is fixed. This means any code anywhere will stop working while this is throwing errors.

If for example the error is “No rows were found.” your whole program would shut down because you are throwing an error. But the error “No rows were found.” is hardly an error that should stop the program. It would be much better practice to catch these kinds of things and act accordingly.

A problem that you would encounter using throw in this case would be for example that you can’t close your connection with the database after this function has run and an error was thrown. The code that would close the database connection is never reached.

Here is the documentation on using throw.

Thanks again for your insight. There are subtle differences that I never quite fully understood until now. I guess in terms of syntax for me the key word you used above is ‘Otherwise’. There is no ‘else’ here but still the code says if there is no error, return it, Otherwise or elsereturn the rows we found. This is the thing I was trying to get my head around. Many thanks for your help…

2 Likes

Sure no problem.

A trick i usually do when i have trouble understanding what code is supposed to do is converting it to pseudo code.

With pseudo code you basically write the functionality of the program without using actual code.

In general this is a good way to avoid uncertainties that would happen during code.
Also it is great to show to someone who does not know how to code. They will still be able to read and understand pseudo code.

This is for example the pseudocode for the game snake:

Create window
Load graphics and rotate snake in 4 directions

Initialize variables
    create a 2d array representing the playing field
    create head
    create tail
    create food

Reset game variables

Main loop
    if arrow key pressed then
        Set head's direction accordingly

    move snake's head according to head's direction and update playing field array
    move snake's tail according to content of playing field and clear playing field accordingly

    if head pos = food pos then
        add score
        pause tail for a while
        re-position food randomly

    draw food
    loop through 2d array drawing snake body where the snake has been
    draw snake head and tail according to their position
    draw current score

    // Collision detection
    if playing field at head pos has value then
        kill player
        reset variables
        restart game
end main loop