title: Clean Code Fundamentals subtitle: Names++ ...
- Video: https://cleancoders.com/episode/clean-code-episode-2
- Exam: https://cleancoders.com/episode/clean-code-episode-2/exam
| Chapter | Time |
|---|---|
| Reveal Your Intent | 00:00:42 |
| Describe the Problem | 00:04:24 |
| Avoid Disinformation | 00:08:19 |
| Pronounceable Names | 00:13:24 |
| Avoid Encodings | 00:16:36 |
| Parts of Speech | 00:20:01 |
| The Scope Length Rule | 00:25:06 |
| Recap & Conclusion | 00:31:39 |
| Distance to the Sun | 00:36:07 |
| Activity | Time |
|---|---|
| Warmup | 5 minutes |
| Exercise 1 | 10 minutes |
| Exercise 2 | 10 minutes |
| Intro to technical writing | 45 minutes |
| Exercise 3 | 10 minutes |
| Wrap up | 5 minutes |
- What was the most confusing name you ever encountered?
- Type in the meeting chat
- Prompt
- Discuss a possible meaning of a variable given its name
- Time limit: 10 minutes
| Name | Kind | Meaning |
|---|---|---|
is_true |
Function | Check if boolean statement evaluates to true |
important_details |
Variable | Details that are important |
dataType |
Variable | Contains type of dataset |
ProcessInfo |
Class | Structure with fields about a process |
count_if |
Function | Count elements that satisfy a condition |
ENXIO |
Constant | Error code for "no such device or address" |
active? |
Function | Check if a process is active |
theTable |
Variable | List of rows with column elements |
set |
Class | Collection of unique elements |
"Code should read like well-written prose." -- Robert C. Martin
What do we mean by well-written prose?
- Write in active voice
- Choose strong verbs
- Reduce the use of there is and there are
- Minimize certain adjectives
- Keep list items parallel
See Google Technical Writing for more details
- Use the active voice most of the time
- Most readers mentally translate passive voice to active voice
- It is easier to read
- It is more concise
- The actor (who does what to whom) is clearly identified
- Active voice sentence structure
- Active voice sentence = actor + verb + target
- Passive voice sentence = target + verb [+ actor]
- Yoda speak = target + actor + verb
- Examples
- Active voice
- The user clicks the button.
- The system sends the email.
- The oscillator generates the signal on demand.
- Passive voice
- The button is clicked by the user.
- The email is sent by the system.
- The signal is generated on demand.
- Yoda speak
- The button the user clicks.
- The email the system sends.
- Active voice
- Reduce imprecise, weak, or generic verbs, such as the following
- Forms of be: is, are, was, were, be, being, been
- Occur, happen, take place, come about, come to pass
| Weak verb | Strong verb |
|---|---|
| The exception occurs when dividing by zero. | Dividing by zero raises the exception. |
| This error message happens when... | The system generates this error message when... |
| We are very careful to ensure... | We carefully ensure... |
- Remove unnecessary references
- If possible, delete there is and there are
- Repair the sentence by moving the true subject and true verb
- Replace there is and there are with a more specific noun or pronoun
| Original | Revised |
|---|---|
| There is a problem with the system. | The system has a problem. |
| There are no errors in the system. | The system has no errors. |
There is a function distribute that assigns keys. |
The distribute function assigns keys. |
- Provide facts instead of loosely defined adjectives and adverbs
- Adjectives and adverbs are often vague and imprecise
- Can make the reader wonder what the writer really means
- Example
- Original: Setting this flag makes the application run screamingly fast.
- Revised: Setting this flag makes the application run 150% faster.
- Keep list items parallel
- All items in a list should "belong" together
- Align all items using similar
- Grammar and parts of speech
- Logical category
- Capitalization
- Punctuation
- Example of an ineffective list
- Parts of speech -- choose well;
- Logical category is essential,
- Capitalization and punctuation
- Improve the following sentences
- The moth was removed by Grace Hopper.
- Performance optimization is overridden by the
--noperfflag - The measurements were taken by the static
Timer()method until it was discovered by the QA team the bugs were present in theTimer()method.
- Time limit: 10 minutes
- Grace Hopper removed the moth.
- The
--noperfflag overrides performance optimization. - The QA team discovered that the
Timer()method contained bugs. The staticTimer()method provided measurements until the bugs were discovered.
- Prompt
- Fill in the blanks for using different parts of speech in names
- Parts of speech reference
- Time limit: 10 minutes
| Entity | Part of speech | Example |
|---|---|---|
| Class | Noun | Solver, SupplyChain |
| Variable | Noun | transaction, table |
| Boolean variable | Predicate | is_active, is_valid |
| Function or method | Verb | open, fill_hierarchy |
| Function or method returning a boolean | Predicate | is_empty, is_open |
| Accessor (getter or setter) | Verb | get_name, set_name |
| Property (method pretending to be a field) | Noun | name, age |
| Boolean property | Predicate | is_empty, is_open |
| Enum (state or object descriptor) | Adjective | RED, SWITCHABLE |
- Choose your names thoughtfully
- Communicate your intent
- Avoid disinformation
- Use pronounceable names
- Avoid encodings
- Choose parts of speech well
- Use The Scope Rule
- Expect an e-mail with instructions for upcoming coding dojo
Always leave the code better than you found it.
-- The Software Craftsmanship Rule