CC Flashcards
(90 cards)
What is the 5S philosophy?
A set of principles part of the Total Productive Maintenance (TPM) where the focus is on maintenance instead of producing.
- Seiri, or organization. Knowing where things are - suitable names.
- Seiton, or tidiness. A piece of code should be where we expect to find it.
- Seiso, or cleaning. Clean workspace remove litter in code.
- Seiketsu, or standardization. Consistent code style and practices.
- Shutsuke, or discipline. Have the discipline to follow the practices.
What is LeBlanc’s law?
Later equals never
What is clean code?
- Clean code is focused
- Decisive and matter-of-fact without unncessary details.
- Tested
- No surprises
What is the boy scout rule?
The code has to be kept clean over time. The rule is:
leave the campground cleaner than you found it.
What are intention-revealing names?
A name that reveals intent. A name should tell us why the entity exists.
Problem with bad names is that they increase the implicity of the code; the degree to which the context is not explicit in the code itself.
What is disinformation in code?
We must avoid leaving false clues that obscure the meaning of code.
Or names that vary in small ways.
What about meaningful distinctions in names?
If names must be different, then they should also mean something different.
Noise words are another meaningless distinction. If we have ProductInfo and ProductData, then we have made the names different, without changing the meaning, since Data and Info are noise words.
Distinguish names in such a way that the reader knows what the differences offer.
What about using searchable names?
The length of a name should correspond to the size of its scope. If a variable or constant might be seen or used in multiple places in a body of code, it is imperative to give it a search-friendly name.
What about encodings in interface names?
Common in legacy but we are generally not interested in letting users/developers know that an API is handing them an Interface, they do not need to know these details, hence we should not encode interfaces in any special way.
How to name classes?
Classes and objects should have noun or noun phrase names like Customer, WikiPage, AddressParser.
Avoid words like Manager, Processor, Data, or Info in the name of a class. As these indicate god classes.
A class name should not be a verb.
How to name methods?
Methods should have verb or verb phrase names like postPayment, deletePage or save.
When constructors are overloaded use static factory methods with names that describe the arguments.
What is the first rule of creating functions?
They have to be small. Functions should not be large enough to hold nested structures.
What is the second rule of creating functions?
They should do one thing. A function an perform multiple steps but each step has to be on the same abstraction level given the function name. After all the reason we write functions is to decompose a larger concept (function name) into a set of steps.
How can we check if a function is doing more than one thing?
If we can extract another function from it with a name that is not merely a restatement of its implementation.
Why is it important to have only one level of abstraction per function?
Mixing levels of abstraction within a function is always confusing. Readers may not be able to tell whether a particular expression is an essential concept or a detail. Once details are mixed with essential concepts, more and more details tend to accrete within the function.
How can we handle the do N thing nature of switch statements?
We bury them in a low-level class and never repeat them. The appropriate switch is then called through polymorphism.
Why are flag arguments a bad idea?
Immediately complicates the signature and proclaims that the method does more than one thing.
How to handle methods that take many arguments?
When a function needs more than one argument, it is likely that come of those arguments ought to be wrapped in class. Since the arguments are part of a concept that deserves a name of its own.
What is the problem with side effects in functions?
Side effects are lies. They often result in strange temporal couplings and order dependencies.
What is command query separation?
Functions should either do something or answer something, but not both.
How to should we extract try/catch blocks?
Separate functions into error processing and the actual command. Functions should do one thing, and error handling is one thing.
Why are comments not good?
Noisy. Have to be maintained. Might be wrong. Might cause confusion if not written properly. Might be redundant.
Write Javadocs on public APIs when its not simply restating what the function name already says.
Use small functions with good names instead of comments.
Comments should not be necessary if we could expressive ourselves properly in code.
Why is code formatting important?
- If code is poorly formatted then it sends a signal to any reader that the same inattentiveness to detail pervades every other aspect of the code.
- Readability/Maintainability since formatting help separating into logical units and keeping concepts that are closely vertically close.
Why should we not use horizontal alignment of declarations?
They emphasize the wrong things and leads eyes away from their types. The real problem is the length (number of) declarations.