w1d2 Flashcards
What are 4 basic style tenets?
◦Indent your code.
◦Limit lines to 72 chars.
◦Avoid long methods and nesting more than two levels deep.
◦Don’t over-comment.
What is a good rule of thumb regarding DRY?
A good rule of thumb with DRY is that if you find yourself copying and pasting code into other places, you should most likely refactor to avoid duplication.
Explain what the =~ operator does.
Returns the first position in the (left-hand operator) string that matches the regex (right-hand operator)
Explain the ‘||’ trick.
Can be used to return the value on the right-hand side of the operator if the left-hand side evaluates to nil.
What is the recommended maximum number of lines for methods?
10
What property do the best methods have?
They are “atomic”; they do an single thing/have a single responsibility
What does making atomic methods and giving them logical & descriptive names do?
Makes our code (specifically our main function) EXTREMELY readable, to the point that anyone can read it like plain English to understand what it does. To see the specific implementation of a method, they can see the definition of that method.
T/F: Methods should not modify global variables, nor should they attempt to access values other than those given to them as arguments.
T
What is the best practice when writing a function to modify its given arguments?
They should only change the arguments if that is the EXPLICIT purpose of the method; otherwise, create a copy of the argument.
When should you use the following naming styles:
snake_case CamelCase SCREAMING_SNAKE_CASE question_mark? bang!
- Use snake_case (not CamelCase ) when naming methods, variables, and files.
- Use CamelCase for classes and modules.
- Use SCREAMING_SNAKE_CASE for other constants.
- Methods that return a boolean value (i.e. true or false ) should end with a question mark (e.g., Array#empty? ).
- Methods that are potentially ‘dangerous’ (e.g., because they modify the calling object or arguments) should end with an exclamation mark (e.g., Array#map! changes the original array).
Name a few guidelines for choosing variable names.
Make names as long as necessary, but as short as possible.
For methods that have no side-effects and just return a value, try using a description of the return value (‘cosine’, ‘square_root’)
For methods that do have side-effects, try using a verb followed by a noun(‘print_cosine’, ‘record_square_root’)
Establish conventions for common operations (‘get_cosine’, ‘set_square_root’)
Say we have a set of objects that hold data of the the same types. What is an elegant solution?
Make an array of hashes with matching keys:
cats = [{ :name => "Breakfast", :age => 8, :home_city => "San Francisco" }, { :name => "Earl", :age => 2, :home_city => "San Francisco" }]
What is a rule of thumb for using hashes?
Use a hash to collect variables that go together.
What is an instance?
An occurrence of an object.
What is a class?
A structure that combines state (data, fields) and behavior (methods).
T/F: Class names are capitalized and snake_cased.
F; Class names are capitalized and CamelCased.