w3d1-w3d2 revisions Flashcards
T/F: When working in a TDD paradigm and you encounter a bug, write a test to isolate and capture the bug.
T
T/F: Comment Ruby liberally
F; Use comments sparingly; Ruby code should strive to be self-explanatory.
How do && and || short-circuit?
&& short circuits on false, || short circuits on true.
What is ‘fail’ a synonym for?
‘raise’
T/F: Subqueries are run once and used by each row.
F; the subquery is run again for each row that calls it.
What is a join table, and does it need a primary key?
It is a table that serves to join together two otherwise unrelated tables. It needs a primary key, even if it is never used.
When planning a SQL workflow, when is a good time to use a JOIN?
When you need a specific piece of data.
Describe an efficient SQL workflow.
1) Identify the queries that will be needed
2) Start with the FROM clause
3) Break down the pieces of data you need; each one will probably be a JOIN
4) In your WHERE clause, reference the alias of each JOIN
What is “normalization” in SQL, and how is it implemented?
Normalization refers to breaking apart repetitive data into different tables. For example, if you have a table with post and user columns, but many posts are by the same user, it makes more sense to make the user column a foreign key value that points to the primary (id) key of the users table.
This allows you to make a change in one place, and should be implemented whenever you encounter repetition in a table.
When should you use an INNER JOIN?
When you don’t care if you lose values that don’t have a match.
When should you use an OUTER JOIN?
When you want to retain values, even if they don’t have a match (NULL value)
What are the naming conventions for foreign keys?
They should be nameofothertable_id.id
Ex:
posts.user_id = user.id
What is the difference between COUNT(*) and COUNT(colname)
The former will count all rows including NULLs, whereas the latter will only count values that are not NULL.
What is the ‘singleton’ gem, and why bother with it?
It restricts a class to creating only one instance of itself. It is important for threading (i.e. two users shouldn’t be able to access a value at the same time), and avoids large costs (if the size of the instance is massive)
What happens when you JOIN tables where there is a column name collision (besides the keys you are matching)
The column that is being merged to the other column (read: the SECOND one) is retained, the other is not.