Notes Flashcards
Boy Scout Rule
Check-in our code a little cleaner than when we checked it out
Rules for Meaningful Names
1) Pronounceable
2) Searchable
3) Class Names are noun or noun phrases
4) Methods are verb or verb phrases
5) Do not pun - same word for two purposes
Rules for Functions
1) They should be small
2) Do One Thing. Note, passing flag argument proclaims a function does more than one thing. Do not do this.
3) Max two or three arguments. If more, it is likely that some of those arguments ought to be wrapped into a class of their own.
4) Command Query Separation
5) Extract Try/Catch Blocks
What is Command Query Separation
Functions should either do something or answer something, but not both
What is a guideline for error handling in a function?
If the keyword try exists in a function, it should be the very first word in the function and there should be nothing after the catch/finally blocks.
What are Good Comments?
1) Legal Comments
2) Informative Comments such as explaining a regex pattern.
3) Explanation of Intent
4) Clarification
5) Warning of Consequence (ex: really long test case)
6) Amplification - amplify line otherwise might be inconsequential.
Name three formatting goals
neatness, consistency, and attention to detail
Describe dependent function formatting
If one function calls another, they should be vertically close, and the caller should be above the callee, if at all possible. This gives the program natural flow.
What is the Law of Demeter?
Objects hide their data and expose operations. More precisely, a method f of a class C should only call the methods of these:
1) C
2) An object created by f
3) An object passed as an argument to f
4) An object held in an instance variable of C
What is a DTO?
Data Transfer Object. Class with public variables and no functions.
What is an Active Record?
A special form of DTO. They are data structures with public variables; but they typically have navigational methods like save and find.
Why wrap third party APIs?
When you wrap a third-party API, you minimize your dependencies upon it. You can choose to move to a different library in the future without much penalty.
What are learning tests?
Writing tests to explore our understanding of third-party code. We call a third-party API, as we expect to use in our application.
The Three Laws of TDD
1) You may not write production code until you have written a failing unit test.
2) You may not write more of a unit test than is sufficient to fail, and not compiling is failing.
3) You may not write more production code than is sufficient to pass the currently failing test.
What’s the most important aspect of a clean test?
Readability. Specifically clarity, simplicity and density of expression. In a test you want to say a lot with a few expressions as possible.
What is the Build-Operate-Check pattern?
Each test is clearly split into three parts. The first part builds up the test data, the second part operates on that test data, and the third part checks that the operation yielded the expected results.
What is a good rule for asserts in your tests?
The number of asserts in a test ought to be minimized. We want to test a single concept in each test function.
Define F.I.R.S.T. clean tests
Fast: Test should run quickly.
Independent: Tests shouldn’t depend on each other. One test shouldn’t set up the conditions for the next.
Repeatable: Run in any environment.
Self-Validating: Should have boolean output.
Timely: Unit tests should be written just before the production code that makes them pass.
What is the first rule of a class?
Classes should be small. We use a responsibility measurement for length.
What’s the single responsibility principle?
It states, that a class or module should have one and only one reason to change.
What is Cohesion?
Classes should have a small number of instance variables. Each of the methods of a class should manipulate one or more of those variables. In general, the more variables a method manipulates the more cohesive that method is to its class.
What should we be able to do when describing a class?
Describe it in about 25 words or less, without using the words “if”, “and”, “or”, or “but”
What is the essence of iterative and incremental agility?
It is a myth that we can get systems “right the first time.” Instead, we should implement only today’s stories, then refractor and expand the system to implement new stories tomorrow.
Describe starting “naively simple”
We can start a software project with a “naively simple” but nicely decoupled architecture, delivering working user stories quickly, and then adding more infrastructure as we scale up.
Feature Envy
The methods of a class should be interested in the variables and functions of the class they belong to, and not the variables and functions of other classes. When a method uses accessors and mutators of some other object to manipulate the data within that object, then it envies the scope of the class of that other object. It wishes it were inside that other class so that it could have direct access to the variables it is manipulating.
Selector Arguments
Type of argument that is used to select the behavior of the function.
Describe use of explanatory variables
One of the more powerful ways to make a program readable is to break the calculations up into intermediate values that are held in variables with meaningful names.