Creating Skimmable Code
Creating Skimmable Code.
Narrow your focus. You want to have to remember as little as possible.
How? Lexical encapsulation. Put things in functions.
Refactoring to do this?
1. Refactor
2. Test
3. Commit
4. Repeat
Two important refactoring techniques for creating skimmable code:
1. Renaming - Give things names that are expressive.
Bad: a name like $arg or $tmp. That is like having a box that has a label on it that says "box". It says what it is, not what is in it.
2. Extract - Pulling out a chunk of functionality that should be in a function or a method. If you can explain a chunk of code in one sentence, you probably should give it its own function that does that.
"Narrow Scope Forgives Many Sins" - A chunk of code can be ugly as hell, but if it is isolated so that no one has to look at it typically that is OK.
Some people don't like putting anything in a method until it is necessary. He (we) disagree. "Discoverable reuse": if something is already in a method, people are likely to start using it (programmers are lazy).
Doesn't like comments to end blocks. Violates the DRY principle (Don't Repeat Yourself) as well as the idea that comments should not be the same as the code. Makes sense in a server side code, but I like the way Paul does this with DIVs, which inevitably are deeply nested.
Add whitespace between logical paragraphs.
"Similar things should look similar."
Add horizontal whitespace to line up arguments, operators, etc.
Name things and name them well.
If you have to have globals, use Caps to make them obvious. Just as similar things should look similar, different things should look different.
RED FLAGS
No documentation
No tests
Things are deeply nested. Makes it difficult to remember.
Complex names. function make_pancakes_and_waffles
Bad names like $tmp, $array, $data, $x, $r
Long names. If you need to say $total_amount_spent_on_corn, you probably should have a method buy_corn that has a $total.
Global variables are bad because they aren't skimmable, can be accessed anywhere.
Post new comment