Alexey Ivanov, Evil Martians
debug and
console.log to functions to see what they are doing.
Most of programming tutorials and books teach how to write code that can be understood by the computer and do some stuff with it.
Very few books and tutorials teach how to write code that other people can read and understand.
You write code once. But you and other people will be reading and modifying it tens or even hundreds of times after that.
So it's crucial to write code that is readable, reusable, and refactorable.
As with any other skill, if you will be mindful of what you are writing and spend enough time training, you will become better at this.
There are tons of existing style guides:
Choose any of them. It's not important which one. Don't spend time on that.
Linters usually have excellent documentation about when and why to use each rule.
The good thing about using a popular style guide is the fact that it is already automated.
prettier and linter plugins to your editor.prettier.Bad:
const yyyymmdstr = moment().format("YYYY/MM/DD");
Good:
const currentDate = moment().format("YYYY/MM/DD");
Bad:
setTimeout(blastOff, 86400000);
Good:
const MILLISECONDS_IN_A_DAY = 86_400_000;
setTimeout(blastOff, MILLISECONDS_IN_A_DAY);
Bad:
function createMenu(title, body, buttonText, cancellable) {
// ...
}
createMenu("Foo", "Bar", "Baz", true);
Good:
function createMenu({ title, body, buttonText, cancellable })
// ...
}
createMenu({
title: "Foo", body: "Bar", buttonText: "Baz", cancellable: true
});
You can automate detection of such things. For example with Eslint:
complexity - Cyclomatic Complexitymax-depthmax-statementsmax-nested-callbacksBe careful. Following these rules blindly can produce harder to read code.
Sometimes it is better to break complexity rules to make the reading flow easier.
dropdown/
locale/
en.json
jp.json
index.js
index.test.js
readme.md
styles.css
Old code:
window.ga("send", {
hitType: "event",
eventCategory: "Videos",
eventAction: "play",
eventLabel: "Fall Campaign",
eventValue: 10
});
Old code:
window.ga("send", {
hitType: "event",
eventCategory: "Page",
eventAction: "Navigate",
});
New code:
window.dataLayer.push({
category: "Videos",
action: "play",
label: "Fall Campaign",
value: 10
});
New code:
window.dataLayer.push({
category: "Page",
action: "Navigate",
});
Solution:
const event = { /* ... */ }
const eventPlaceholder = {
label: undefined, value: underfined
};
window.dataLayer.push({ ...eventPlaceholder, ...event });
/*
GTM sets all event fields as global variables inside itself,
so until we rewrite them, they will have old values. Because
label and value are optional fields, we need to unset them
for the next events explicitly. Relevant docs:
- https://developers.google.com/tag-manager/devguide
- https://support.google.com/tagmanager/answer/6164391?hl=en
*/
Code can't explain why the program is being written, and the rationale for choosing this or that method. Code cannot discuss the reasons certain alternative approaches were taken.
Jeff Raskin
For example:
/* A binary search turned out to be slower than the
Boyer-Moore algorithm for the data sets of interest,
thus we have used the more complex, but faster method
even though this problem does not at first seem
amenable to a string search technique. */
Jeff Raskin
@media (--sm-scr) {
.input {
font-size: 16px; /* prevents iOS auto-scale on focus */
}
}
Crockford's method:
Who should read:
You can use methodology from usability testings for the additional effect:
Read more: Thinking Aloud: The #1 Usability Tool
Read the code from open-source projects that you use together in the same way:
Principles of the clean code apply to commits and PRs as well:
If you have large PR - split them to the smaller parts:
You can (and should) use commit messages and PR description to describe the task's context:
Things that should be done before code review:
Links:
How to use to become a better writer:
Writing:
Reading:
Alexey Ivanov, Evil Martians
Twitter:
@iadramelk
Slides:
https://iadramelk.github.io/code-reading/