SlideShare a Scribd company logo
Writing code for people
Alexey Ivanov, Evil Martians
Writing code for people
Evil Martians
Evil Martians
Writing code for people
Let's talk about
writing
—  Know how to write letters?
—  Know how to write words?
—  Know how to write sentences?
—  Do you write emails, Line messages, or tweets every day?
—  Published articles in magazines or books?
Do you know how to write?
7
Writing code is the
same
—  You open your code from one year ago and can't remember how it works.
—  You spend half of the day adding debug and console.log to functions
to see what they are doing.
—  You tried to read another person's code and can't understand it.
—  ^ Another person probably thought the same about your code too.
Have you been in these situations?
9
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.
This is a common situation
10
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.
But it's not a good situation
11
Writing code for people
Writing code for
people
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.
It's a skill that can be learned
14
1.  Writing the code — principles, rules, style guides, and linters.
2.  Reading the code — how to train yourself to become a better writer.
Talk structure
15
Writing the code
1.  Clean code.
2.  Easy to understand code.
3.  Code that shares context.
Writing the code
17
Clean code
—  Select one code style.
—  Automate it.
—  Forget about it.
Clean code
19
There are tons of existing style guides:
—  https://standardjs.com/
—  https://github.com/airbnb/javascript
—  https://google.github.io/styleguide/jsguide.html
—  Standard Prettier settings
Choose any of them. It's not important which one. Don't spend time on that.
Select a code style
CLEAN CODE
20
—  eslint for Javascript or TypeScript.
—  stylelint for CSS.
—  rubocop for Ruby.
—  prettier — tool that can automatically fix style for multiple languages.
Linters usually have excellent documentation about when and why to use
each rule.
Add linters
CLEAN CODE
21
—  eslint-config-airbnb
—  eslint-config-standard
—  stylelint-config-standard
The good thing about using a popular style guide is the fact that it is already
automated.
Add standard config for linters
CLEAN CODE
22
—  Add prettier and linter plugins to your editor.
—  Enable automatic fixing in prettier .
—  Add precommit hook that automatically format/lint your code.
—  Run your checks in CI (CircleCI or TravisCI will do).
Enable automatic linting and fixing
CLEAN CODE
23
Demo
Again, it's not important what style you choose. Toss a coin if you like. You
can always automatically reformat it later.
Forget about it
CLEAN CODE
25
Easy to
understand code
1.  Readability.
2.  Complexity.
3.  Reading flow.
Easy to understand code
27
Bad:
const yyyymmdstr = moment().format("YYYY/MM/DD");
Good:
const currentDate = moment().format("YYYY/MM/DD");
Use meaningful and pronounceable
variable names
READABILITY
28
Bad:
setTimeout(blastOff, 86400000);
Good:
const MILLISECONDS_IN_A_DAY = 86_400_000;
setTimeout(blastOff, MILLISECONDS_IN_A_DAY);
Use searchable names
READABILITY
29
Bad:
function createMenu(title, body, buttonText, cancellable) {
// ...
}
createMenu("Foo", "Bar", "Baz", true);
Function arguments (2 or fewer ideally)
READABILITY
30
Good:
function createMenu({ title, body, buttonText, cancellable })
// ...
}
createMenu({
title: "Foo", body: "Bar",
buttonText: "Baz", cancellable: true
});
Function arguments (2 or fewer ideally)
READABILITY
31
Bad:
function createFile(name, temp) {
if (temp) {
fs.create(`./temp/${name}`);
} else {
fs.create(name);
}
}
Don't use flags as function parameters
READABILITY
32
Good:
function createFile(name) {
fs.create(name);
}
function createTempFile(name) {
createFile(`./temp/${name}`);
}
Don't use flags as function parameters
READABILITY
33
—  Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin
⽇本語
—  https://github.com/ryanmcdermott/clean-code-javascript   ⽇本語 —
examples was from there.
—  https://github.com/ryanmcdermott/3rs-of-software-architecture
—  JS UX: Writing code for Humans by Lea Verou
Further reading
READABILITY
34
One of the sighs of the bad code is complexity:
—  Large functions that do multiple things at once.
—  Too many arguments for the function.
—  Multiple nested IFs or callbacks.
Complexity
35
You can automate detection of such things. For example with Eslint:
—   complexity  — Cyclomatic Complexity
—   max-depth
—   max-statements
—   max-nested-callbacks
Be careful. Following these rules blindly can produce harder to read code.
Automation
COMPLEXITY
36
—  How often do you go back and forth between functions and methods in the
file?
—  How many files do you need to open to understand how something works?
—  Are the files that you are switching between are close to each other or do you
need to go to different nested folders?
Sometimes it is better to break complexity rules to make the reading
flow easier.
Reading flow
37
dropdown/
locale/
en.json
jp.json
index.js
index.test.js
readme.md
styles.css
Place things close to each other
READING FLOW
38
Code that shares
context
Old code:
window.ga("send", {
hitType: "event",
eventCategory: "Videos",
eventAction: "play",
eventLabel: "Fall Campaign",
eventValue: 10
});
Replacing GA with GTM
EXAMPLE
40
New code:
window.dataLayer.push({
category: "Videos",
action: "play",
label: "Fall Campaign",
value: 10
});
Replacing GA with GTM
EXAMPLE
41
Solution:
const event = { /* ... */ }
const eventPlaceholder = {
label: undefined, value: underfined
};
window.dataLayer.push({ ...eventPlaceholder, ...event });
Replacing GA with GTM
EXAMPLE
42
1.  You can't just leave the code" as is" – next developer can delete it while
refactoring, and they spend eight more hours debugging it.
2.  You can't ask other people to review this code without telling them what it is
doing and why.
3.  Other people can't update and maintain this code.
But how do you share this knowledge?
43
/*
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
*/
Use comments
44
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
What is context?
“
45
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
What is context?
46
@media (--sm-scr) {
.input {
font-size: 16px; /* prevents iOS auto-scale on focus */
}
}
CSS example
47
React Source
GIT Source
—  The reader needs a context that can't be expressed using other means.
—  The code exists because of the environment issues. Browser bugs, for
example.
—  The ugly solution is chosen because of speed optimization reasons.
When to comment
50
Writing code for people
—  Code Tells You How, Comments Tell You Why by Jeff Artwood
—  The Art of Comments by Sarah Drasner
Links
52
1.  Clean code.
2.  Easy to understand code.
1.  Readability.
2.  Complexity.
3.  Reading flow.
3.  Code that shares context.
Summary of the first part
53
Reading the code
1.  Code reading.
2.  Code reviews.
3.  Pair Programming.
Reading the code
55
Code reading
Douglas Crockford — Goto There and Back Again starting from 27:15
Code reading
57
Crockford's method:
1.  Do it every day.
2.  Discuss approaches, patterns, and possible solutions.
3.  Don't judge. Don't use results as metrics.
Code reading
58
Who should read:
1.  Reading by the author — faster to do, better at describing context, but
fewer chances to find problems.
2.  Reading by the other person — slower, requires more steps, more useful
feedback on writing.
Code reading
59
You can use methodology from usability testings for the additional effect:
—  Think aloud. Make assumptions about what variables and functions will do.
—  Check if these assumptions were correct.
—  See how much time you spend on each block.
—  See how often you switch between files and sections inside the file.
Read more: Thinking Aloud: The #1 Usability Tool
Code reading
60
Read the code from open-source projects that you use together in the same
way:
—  You will have a better understanding of how they work.
—  You will see which approaches to writing code works and which don't.
Code reading
61
—  https://github.com/reduxjs/redux
—  https://github.com/mobxjs/mobx
—  webpack loaders
Some projects to start from
62
Code review
Principles of the clean code apply to commits and PRs as well:
—  Short, <200LOC PRs.
—  Do only one thing.
—  Describe the context.
Code review
64
If you have large PR — split them to the smaller parts:
1.  Refactoring filenames? Create PR.
2.  Changing the React component that is used in multiple places? Create PR.
3.  Making new utility for everyone to use? Create PR.
Split to many small PRs
CODE REVIEW
65
You can (and should) use commit messages and PR description to describe
the task's context:
1.  What was the task?
2.  Why did you choose to solve it in this way and not another?
Describe context
CODE REVIEW
66
Things that should be done before code review:
—  Discuss the PR process and decide who and how fast should review them.
—  Chose style guide and enable linters.
—  Discuss the new APIs before starting writing code for them.
Advance preparation
CODE REVIEW
67
1.  Split large PRs that need to be merged all at once in the separate commits
that only do one thing.
2.  Give each commit a meaningful name.
3.  Before pushing to upstream, make your commit history beautiful: Squash,
rename, and clean up your commits.
Can't split to different PRs?
CODE REVIEW
68
Writing code for people
Links:
—  http://ohshitgit.com/
—  https://chris.beams.io/posts/git-commit/
—  https://sethrobertson.github.io/GitBestPractices/
Code review
70
Pair programing
How to use to become a better writer:
—  Discuss the plan and make a basic structure.
—  Split tasks so both sides can program and not only listen.
—  Comment what you are trying to do now.
—  Give real-time feedback and ask questions.
Pair programing
72
Writing:
1.  Clean code.
2.  Easy to understand code.
1.  Readability.
2.  Complexity.
3.  Reading flow.
3.  Code that shares context.
Reading:
1.  Code reading.
2.  Code reviews.
3.  Pair Programming.
Summary of the talk
73
Alexey Ivanov, Evil Martians
Twitter: @iadramelk
Writing code for people
74

More Related Content

PDF
WordCamp US: Clean Code
mtoppa
 
PDF
Understanding, measuring and improving code quality in JavaScript
Mark Daggett
 
PPTX
Clean Code III - Software Craftsmanship
Theo Jungeblut
 
PDF
Euro python 2015 writing quality code
radek_j
 
PDF
How To Become A Good C# Programmer
LearnItFirst.com
 
PDF
Clean Code V2
Jean Carlo Machado
 
PPTX
Code quality
Provectus
 
PPTX
Importance of the quality of code
Shwe Yee
 
WordCamp US: Clean Code
mtoppa
 
Understanding, measuring and improving code quality in JavaScript
Mark Daggett
 
Clean Code III - Software Craftsmanship
Theo Jungeblut
 
Euro python 2015 writing quality code
radek_j
 
How To Become A Good C# Programmer
LearnItFirst.com
 
Clean Code V2
Jean Carlo Machado
 
Code quality
Provectus
 
Importance of the quality of code
Shwe Yee
 

What's hot (19)

PPTX
Clean Code I - Best Practices
Theo Jungeblut
 
PPTX
API workshop: Deep dive into Java
Tom Johnson
 
PPTX
Thinking of Documentation as Code [YUIConf 2013]
evangoer
 
PPT
Clean Code summary
Jan de Vries
 
PDF
DEF CON 23 - Saif el-sherei and etienne stalmans - fuzzing
Felipe Prado
 
DOC
RajeshK
kallutla rajesh
 
PDF
Software Analysis using Natural Language Queries
Pooja Rani
 
PDF
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
VincitOy
 
PDF
GPT-2: Language Models are Unsupervised Multitask Learners
Young Seok Kim
 
PPTX
code documentation
MADUABUM NNANNA
 
PDF
Prefer Code to Comments
Kevlin Henney
 
PPTX
Object Oriented Apologetics
Vance Lucas
 
PPTX
Writing High Quality Code in C#
Svetlin Nakov
 
PPTX
Clean Code II - Dependency Injection
Theo Jungeblut
 
PDF
Clean code
Jean Carlo Machado
 
PDF
How to write maintainable code - Peter Hilton - Codemotion Amsterdam 2017
Codemotion
 
PDF
Inside Requirements
Kevlin Henney
 
PDF
Sorted
Kevlin Henney
 
PPTX
API Documentation -- Presentation to East Bay STC Chapter
Tom Johnson
 
Clean Code I - Best Practices
Theo Jungeblut
 
API workshop: Deep dive into Java
Tom Johnson
 
Thinking of Documentation as Code [YUIConf 2013]
evangoer
 
Clean Code summary
Jan de Vries
 
DEF CON 23 - Saif el-sherei and etienne stalmans - fuzzing
Felipe Prado
 
Software Analysis using Natural Language Queries
Pooja Rani
 
Improving Code Quality In Medical Software Through Code Reviews - Vincit Teat...
VincitOy
 
GPT-2: Language Models are Unsupervised Multitask Learners
Young Seok Kim
 
code documentation
MADUABUM NNANNA
 
Prefer Code to Comments
Kevlin Henney
 
Object Oriented Apologetics
Vance Lucas
 
Writing High Quality Code in C#
Svetlin Nakov
 
Clean Code II - Dependency Injection
Theo Jungeblut
 
Clean code
Jean Carlo Machado
 
How to write maintainable code - Peter Hilton - Codemotion Amsterdam 2017
Codemotion
 
Inside Requirements
Kevlin Henney
 
API Documentation -- Presentation to East Bay STC Chapter
Tom Johnson
 
Ad

Similar to Writing code for people (20)

PDF
How to write good quality code
Hayden Bleasel
 
PPTX
30% faster coder on-boarding when you have a code cookbook
Gabriel Paunescu 🤖
 
PPTX
Best-Practices-for-Writing-Clean-Code.Presentation
Ozias Rondon
 
PPTX
Linters for frontend code review
Vsevolod Nechaev
 
PDF
Meaningful code - BeCode Brussels - August 2018
Yannick Lemin
 
PDF
Code Quality Makes Your Job Easier
Tonya Mork
 
PDF
Good Coding Practices with JavaScript
🏁 Pierre-Henry Soria 💡
 
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
krantzloigu
 
PPTX
Writing code for others
Amol Pujari
 
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
sagolbencib
 
ODP
Documenting code yapceu2016
Søren Lund
 
PDF
Clean Code. An Agile Guide to Software Craft Kameron H.
komvjzfjj621
 
PDF
Chapter17 of clean code
Kuyseng Chhoeun
 
PPS
CS101- Introduction to Computing- Lecture 44
Bilal Ahmed
 
PDF
Refactoring JavaScript turning bad code into good code First Edition Burchard
simbajdzikie4
 
PDF
Managing and evolving JavaScript Code
Jean Carlo Emer
 
PDF
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Fwdays
 
PPTX
Improving Code Quality Through Effective Review Process
Dr. Syed Hassan Amin
 
PDF
How to write maintainable code
Peter Hilton
 
PDF
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
DevDay Da Nang
 
How to write good quality code
Hayden Bleasel
 
30% faster coder on-boarding when you have a code cookbook
Gabriel Paunescu 🤖
 
Best-Practices-for-Writing-Clean-Code.Presentation
Ozias Rondon
 
Linters for frontend code review
Vsevolod Nechaev
 
Meaningful code - BeCode Brussels - August 2018
Yannick Lemin
 
Code Quality Makes Your Job Easier
Tonya Mork
 
Good Coding Practices with JavaScript
🏁 Pierre-Henry Soria 💡
 
Clean Code. An Agile Guide to Software Craft Kameron H.
krantzloigu
 
Writing code for others
Amol Pujari
 
Clean Code. An Agile Guide to Software Craft Kameron H.
sagolbencib
 
Documenting code yapceu2016
Søren Lund
 
Clean Code. An Agile Guide to Software Craft Kameron H.
komvjzfjj621
 
Chapter17 of clean code
Kuyseng Chhoeun
 
CS101- Introduction to Computing- Lecture 44
Bilal Ahmed
 
Refactoring JavaScript turning bad code into good code First Edition Burchard
simbajdzikie4
 
Managing and evolving JavaScript Code
Jean Carlo Emer
 
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Fwdays
 
Improving Code Quality Through Effective Review Process
Dr. Syed Hassan Amin
 
How to write maintainable code
Peter Hilton
 
[DevDay2018] Let’s all get along. Clean Code please! - By: Christophe K. Ngo,...
DevDay Da Nang
 
Ad

More from Alexey Ivanov (6)

PDF
REST to GraphQL migration: Pros, cons and gotchas
Alexey Ivanov
 
PDF
Внутреннее устройство и оптимизация бандла webpack
Alexey Ivanov
 
PDF
CSS-в-JS, HTML-в-JS, ВСЁ-в-JS. Все гораздо проще, когда всё вокруг JavaScript
Alexey Ivanov
 
PDF
Будущее шаблонизаторов
Alexey Ivanov
 
PDF
ДАМП 2015 Екатеринбург
Alexey Ivanov
 
PDF
Gipp
Alexey Ivanov
 
REST to GraphQL migration: Pros, cons and gotchas
Alexey Ivanov
 
Внутреннее устройство и оптимизация бандла webpack
Alexey Ivanov
 
CSS-в-JS, HTML-в-JS, ВСЁ-в-JS. Все гораздо проще, когда всё вокруг JavaScript
Alexey Ivanov
 
Будущее шаблонизаторов
Alexey Ivanov
 
ДАМП 2015 Екатеринбург
Alexey Ivanov
 

Recently uploaded (20)

PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PDF
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
PDF
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
PPTX
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PDF
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
PDF
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
DOCX
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PPTX
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PPTX
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
PDF
Immersive experiences: what Pharo users do!
ESUG
 
PPTX
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
PDF
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
PPT
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
PDF
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
PDF
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
PPTX
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
PPTX
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
Generating Union types w/ Static Analysis
K. Matthew Dupree
 
49784907924775488180_LRN2959_Data_Pump_23ai.pdf
Abilash868456
 
Web Testing.pptx528278vshbuqffqhhqiwnwuq
studylike474
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
Adobe Illustrator Crack Full Download (Latest Version 2025) Pre-Activated
imang66g
 
An Experience-Based Look at AI Lead Generation Pricing, Features & B2B Results
Thomas albart
 
Can You Build Dashboards Using Open Source Visualization Tool.docx
Varsha Nayak
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
slidesgo-unlocking-the-code-the-dynamic-dance-of-variables-and-constants-2024...
kr2589474
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
ConcordeApp: Engineering Global Impact & Unlocking Billions in Event ROI with AI
chastechaste14
 
Immersive experiences: what Pharo users do!
ESUG
 
Maximizing Revenue with Marketo Measure: A Deep Dive into Multi-Touch Attribu...
bbedford2
 
Download iTop VPN Free 6.1.0.5882 Crack Full Activated Pre Latest 2025
imang66g
 
Why Reliable Server Maintenance Service in New York is Crucial for Your Business
Sam Vohra
 
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
Key Features to Look for in Arizona App Development Services
Net-Craft.com
 
Visualising Data with Scatterplots in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Contractor Management Platform and Software Solution for Compliance
SHEQ Network Limited
 

Writing code for people

  • 1. Writing code for people Alexey Ivanov, Evil Martians
  • 7. —  Know how to write letters? —  Know how to write words? —  Know how to write sentences? —  Do you write emails, Line messages, or tweets every day? —  Published articles in magazines or books? Do you know how to write? 7
  • 8. Writing code is the same
  • 9. —  You open your code from one year ago and can't remember how it works. —  You spend half of the day adding debug and console.log to functions to see what they are doing. —  You tried to read another person's code and can't understand it. —  ^ Another person probably thought the same about your code too. Have you been in these situations? 9
  • 10. 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. This is a common situation 10
  • 11. 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. But it's not a good situation 11
  • 14. 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. It's a skill that can be learned 14
  • 15. 1.  Writing the code — principles, rules, style guides, and linters. 2.  Reading the code — how to train yourself to become a better writer. Talk structure 15
  • 17. 1.  Clean code. 2.  Easy to understand code. 3.  Code that shares context. Writing the code 17
  • 19. —  Select one code style. —  Automate it. —  Forget about it. Clean code 19
  • 20. There are tons of existing style guides: —  https://standardjs.com/ —  https://github.com/airbnb/javascript —  https://google.github.io/styleguide/jsguide.html —  Standard Prettier settings Choose any of them. It's not important which one. Don't spend time on that. Select a code style CLEAN CODE 20
  • 21. —  eslint for Javascript or TypeScript. —  stylelint for CSS. —  rubocop for Ruby. —  prettier — tool that can automatically fix style for multiple languages. Linters usually have excellent documentation about when and why to use each rule. Add linters CLEAN CODE 21
  • 22. —  eslint-config-airbnb —  eslint-config-standard —  stylelint-config-standard The good thing about using a popular style guide is the fact that it is already automated. Add standard config for linters CLEAN CODE 22
  • 23. —  Add prettier and linter plugins to your editor. —  Enable automatic fixing in prettier . —  Add precommit hook that automatically format/lint your code. —  Run your checks in CI (CircleCI or TravisCI will do). Enable automatic linting and fixing CLEAN CODE 23
  • 24. Demo
  • 25. Again, it's not important what style you choose. Toss a coin if you like. You can always automatically reformat it later. Forget about it CLEAN CODE 25
  • 28. Bad: const yyyymmdstr = moment().format("YYYY/MM/DD"); Good: const currentDate = moment().format("YYYY/MM/DD"); Use meaningful and pronounceable variable names READABILITY 28
  • 29. Bad: setTimeout(blastOff, 86400000); Good: const MILLISECONDS_IN_A_DAY = 86_400_000; setTimeout(blastOff, MILLISECONDS_IN_A_DAY); Use searchable names READABILITY 29
  • 30. Bad: function createMenu(title, body, buttonText, cancellable) { // ... } createMenu("Foo", "Bar", "Baz", true); Function arguments (2 or fewer ideally) READABILITY 30
  • 31. Good: function createMenu({ title, body, buttonText, cancellable }) // ... } createMenu({ title: "Foo", body: "Bar", buttonText: "Baz", cancellable: true }); Function arguments (2 or fewer ideally) READABILITY 31
  • 32. Bad: function createFile(name, temp) { if (temp) { fs.create(`./temp/${name}`); } else { fs.create(name); } } Don't use flags as function parameters READABILITY 32
  • 33. Good: function createFile(name) { fs.create(name); } function createTempFile(name) { createFile(`./temp/${name}`); } Don't use flags as function parameters READABILITY 33
  • 34. —  Clean Code: A Handbook of Agile Software Craftsmanship by Robert C. Martin ⽇本語 —  https://github.com/ryanmcdermott/clean-code-javascript   ⽇本語 — examples was from there. —  https://github.com/ryanmcdermott/3rs-of-software-architecture —  JS UX: Writing code for Humans by Lea Verou Further reading READABILITY 34
  • 35. One of the sighs of the bad code is complexity: —  Large functions that do multiple things at once. —  Too many arguments for the function. —  Multiple nested IFs or callbacks. Complexity 35
  • 36. You can automate detection of such things. For example with Eslint: —   complexity  — Cyclomatic Complexity —   max-depth —   max-statements —   max-nested-callbacks Be careful. Following these rules blindly can produce harder to read code. Automation COMPLEXITY 36
  • 37. —  How often do you go back and forth between functions and methods in the file? —  How many files do you need to open to understand how something works? —  Are the files that you are switching between are close to each other or do you need to go to different nested folders? Sometimes it is better to break complexity rules to make the reading flow easier. Reading flow 37
  • 40. Old code: window.ga("send", { hitType: "event", eventCategory: "Videos", eventAction: "play", eventLabel: "Fall Campaign", eventValue: 10 }); Replacing GA with GTM EXAMPLE 40
  • 41. New code: window.dataLayer.push({ category: "Videos", action: "play", label: "Fall Campaign", value: 10 }); Replacing GA with GTM EXAMPLE 41
  • 42. Solution: const event = { /* ... */ } const eventPlaceholder = { label: undefined, value: underfined }; window.dataLayer.push({ ...eventPlaceholder, ...event }); Replacing GA with GTM EXAMPLE 42
  • 43. 1.  You can't just leave the code" as is" – next developer can delete it while refactoring, and they spend eight more hours debugging it. 2.  You can't ask other people to review this code without telling them what it is doing and why. 3.  Other people can't update and maintain this code. But how do you share this knowledge? 43
  • 44. /* 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 */ Use comments 44
  • 45. 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 What is context? “ 45
  • 46. 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 What is context? 46
  • 47. @media (--sm-scr) { .input { font-size: 16px; /* prevents iOS auto-scale on focus */ } } CSS example 47
  • 50. —  The reader needs a context that can't be expressed using other means. —  The code exists because of the environment issues. Browser bugs, for example. —  The ugly solution is chosen because of speed optimization reasons. When to comment 50
  • 52. —  Code Tells You How, Comments Tell You Why by Jeff Artwood —  The Art of Comments by Sarah Drasner Links 52
  • 53. 1.  Clean code. 2.  Easy to understand code. 1.  Readability. 2.  Complexity. 3.  Reading flow. 3.  Code that shares context. Summary of the first part 53
  • 55. 1.  Code reading. 2.  Code reviews. 3.  Pair Programming. Reading the code 55
  • 57. Douglas Crockford — Goto There and Back Again starting from 27:15 Code reading 57
  • 58. Crockford's method: 1.  Do it every day. 2.  Discuss approaches, patterns, and possible solutions. 3.  Don't judge. Don't use results as metrics. Code reading 58
  • 59. Who should read: 1.  Reading by the author — faster to do, better at describing context, but fewer chances to find problems. 2.  Reading by the other person — slower, requires more steps, more useful feedback on writing. Code reading 59
  • 60. You can use methodology from usability testings for the additional effect: —  Think aloud. Make assumptions about what variables and functions will do. —  Check if these assumptions were correct. —  See how much time you spend on each block. —  See how often you switch between files and sections inside the file. Read more: Thinking Aloud: The #1 Usability Tool Code reading 60
  • 61. Read the code from open-source projects that you use together in the same way: —  You will have a better understanding of how they work. —  You will see which approaches to writing code works and which don't. Code reading 61
  • 64. Principles of the clean code apply to commits and PRs as well: —  Short, <200LOC PRs. —  Do only one thing. —  Describe the context. Code review 64
  • 65. If you have large PR — split them to the smaller parts: 1.  Refactoring filenames? Create PR. 2.  Changing the React component that is used in multiple places? Create PR. 3.  Making new utility for everyone to use? Create PR. Split to many small PRs CODE REVIEW 65
  • 66. You can (and should) use commit messages and PR description to describe the task's context: 1.  What was the task? 2.  Why did you choose to solve it in this way and not another? Describe context CODE REVIEW 66
  • 67. Things that should be done before code review: —  Discuss the PR process and decide who and how fast should review them. —  Chose style guide and enable linters. —  Discuss the new APIs before starting writing code for them. Advance preparation CODE REVIEW 67
  • 68. 1.  Split large PRs that need to be merged all at once in the separate commits that only do one thing. 2.  Give each commit a meaningful name. 3.  Before pushing to upstream, make your commit history beautiful: Squash, rename, and clean up your commits. Can't split to different PRs? CODE REVIEW 68
  • 72. How to use to become a better writer: —  Discuss the plan and make a basic structure. —  Split tasks so both sides can program and not only listen. —  Comment what you are trying to do now. —  Give real-time feedback and ask questions. Pair programing 72
  • 73. Writing: 1.  Clean code. 2.  Easy to understand code. 1.  Readability. 2.  Complexity. 3.  Reading flow. 3.  Code that shares context. Reading: 1.  Code reading. 2.  Code reviews. 3.  Pair Programming. Summary of the talk 73
  • 74. Alexey Ivanov, Evil Martians Twitter: @iadramelk Writing code for people 74