Coding and Node-ing — ACA 300 Week 4 Questions
This week in Austin Coding Academy, we’ve been learning how to code SQL requests with mySQL npm package rather than making the requests via MySQL Workbench. The people using our apps won’t be using MySQL workbench, so we (as developers) need to be able to make our web apps talk to our databases. Simple as that.
As always, here are this weeks interview questions, and the answers I’ve scrounged up via research or prior knowledge.
What is “callback hell” and how can it be avoided?
What are “stubs” in Node.js?
A stub is like a fake function. They have all the components of a function and behave as a function should but the function’s only purpose is to test that the code calling the stub function works properly. This allows developers to provide an endpoint for another function without needing to fully build the endpoint function.
In simpler words, it’s a fake function that can be incomplete in implementation but allow another function relying on calling the stub to run properly. Think of it as a testing shortcut.
What are “streams” in Node.js?
Evidently, streams are often misunderstood. After doing research for a few minutes I found this explanation:
“Streams are one of the fundamental concepts that power Node.js applications. They are data-handling method and are used to read or write input into output sequentially.”
So this means that “streams” are a type of data handling method. They are more memory efficient than other methods and are able to process data much faster.
There’s a lot more that goes into understanding streams. If you’d like to read more about them check out this article on nodesource.com
What does “chaining” mean in Node.JS?
Chaining is a concept in Object Oriented Programming where we quite literally “chain” method calls together so that they are called in some kind of sequence. There’s more utility to the concept but that’s the gist.
Explain “console” in Node.JS?
Node console is a global object, which can be used to interact with the node streams. Commonly the console is used to print different levels of messages to the stdout or stderr. Stdout is a writable stream used for output like log or info output and stderr is an error/warning output.
Explain exit codes in Node.JS. List out some exit codes.
Node.js scripts usually run until they reach their end and no longer have any event handlers waiting for events. This results in the standard exit code of 0. However, if you want to exit the script earlier you’ll need to include exit codes in your script. Here are two common codes.
Exit Code 1 — Uncaught Fatal Exception: This code means there was an uncaught exception, and it was not handled by a domain or an
Exit Code 9 — Invalid Argument: Either an unknown option was specified, or an option requiring a value was provided without a value.
What is the difference between clustered and non-clustered Index?
A clustered index can be considered a “smart index” meaning that it will sort the table based on the information in the clustered index. This could be simple logic like sorting by alphabetic name, or more complex like sorting by gender and then a fallback of age to further sort the data.
A non-clustered index is more of an index in the traditional sense, it does no sorting and is simply a relationship between different tables. There can be multiple non-clustered indices on a table.
What are user defined functions? What are all types of user defined functions?
There are two main distinctions (barring a type called aggregate functions which I will come back to in another story.)
Those two types are called Table-valued functions and Scalar-valued functions. Table-valued ultimately returns it’s results in the form of a table, while Scalar-valued return a single value.
Let’s look at a Scalar-valued UDF.
CREATE FUNCTION dbo.udf_GetProductSales (@Product VARCHAR(10),@BeginDate DATETIME,@EndDate DATETIME)RETURNS MONEYASBEGINDECLARE @Sales MONEYSELECT @Sales = SUM(SalePrice)FROM SalesHistoryWHEREProduct = @Product ANDSaleDate BETWEEN @BeginDate AND @EndDateRETURN(@Sales)END
Without knowing this type of more advanced SQL we can see that this UDF is returning a single value, the sum of all SalePrice between two dates.
Now lets look at a Table-valued function.
CREATE FUNCTION dbo.udf_GetProductSalesTable(@Product VARCHAR(10),@SaleID INT)RETURNS @SalesTable TABLE (SalesTotal MONEY,SalesCount INT)BEGININSERT INTO @SalesTable(SalesTotal, SalesCount)SELECTSUM(SalePrice), COUNT(SaleID)FROMSalesHistoryWHEREProduct = @Product ANDSaleID <= @SaleIDRETURNENDGO
We can see at a glance that this UDF is returning a table called SalesTable with two columns SalesTotal and SalesCount.
And that’s a wrap on this week! Thanks for reading.