Writing Code for Humans
Problem: Your brain
Our puny brain can handle a very limited amount of logic at a time. While programmers proclaim logic as their domain, they are only sometimes and slightly better at managing complexity than the rest of us, mortals. The more logic our app has, the harder it is to change it or introduce new people to it.
The most common mistake programmers do is assuming they write code for a machine to read. While technically that is true, this mindset leads to the hell that is other people’s code.
I have worked in several start-up companies, some of them even considered “lean.” In each, it took me between few weeks to few months to fully understand their code-base, and I have about 6 years of experience with JavaScript. This does not seem reasonable to me at all.
If the code is not easy to read, its structure is already a monument—you can change small things, but major changes—the kind every start-up undergoes on an almost monthly basis—are as fun as a root canal. Once the code reaches a state, that for a proficient programmer, it is harder to read than this article—doom and suffering is upon you.
Why does the code become unreadable? Let’s compare code to plain text: the longer a sentence is, the easier it is for our mind to forget the beginning of it, and once we reach the end, we forget what was the beginning and lose the meaning of the whole sentence. You had to read the previous sentence twice because it was too long to get in one grasp? Exactly! Same with code. Worse, actually—the logic of code can be way more complex than any sentence from a book or a blog post, and each programmer has his own logic which can be total gibberish to another. Not to mention that we also need to remember the logic. Sometimes we come back to it the same day and sometimes after two month. Nobody remembers anything about their code after not looking at it for two month.
To make code readable to other humans we rely on three things:
1. Conventions
Conventions are good, but they are very limited: enforce them too little and the programmer becomes coupled to the code—no one will ever understand what they meant once they are gone. Enforce too much and you will have hour-long debates about every space and colon (true story.) The “habitable zone” is very narrow and easy to miss.
2. Comments
They are probably the most helpful, if done right. Unfortunately many programmers write their comments in the same spirit they write their code—very idiosyncratic. I do not belong to the school claiming good code needs no comments, but even beautifully commented code can still be extremely complicated.
3. “Other people know this programming language as much as I do, so they must understand my writings.”
Well… This is JavaScript:
4. Tests
Tests are a devil in disguise. ”How do we make sure our code is good and readable? We write more code!” I know many of you might quit this post right here, but bear with me for a few more lines: regardless of their benefit, tests are another layer of logic. They are more code to be read and understood. Tests try to solve this exact problem: your code is too complicated to calculate it’s result in your brain? So you say “well, this is what should happen in the end.” And when it doesn’t, you go digging for the problem. Your code should be simple enough to read a function or a line and understand what should be the result of running it.
Your life as a programmer could be so much easier!
Solution: Radical Minimalism
I will break down this approach into practical points, but the main idea is: use LESS logic.
- Cut 80% of your product’s features
Yes! Just like that. Simplicity, first of all, comes from the product. Make it easy for people to understand and use. Make it do one thing well, and only then add up (if there is still a need.)
Use nothing but what you absolutely must
Do not include a single line of code (especially from libraries) that you are not 100% sure you will use and that it is the simplest, most straightforward solution available. Need a simple chat app and use Angular.js because it’s nice with the two-way binding? You deserve those hours and days of debugging and debating about services vs. providers.
Side note: The JavaScript browser api is event-driven, it is made to respond when stuff (usually user input) happens. This means that events change data. Many new frameworks (Angular, Meteor) reverse this direction and make data changes trigger events. If your app is simple, you might live happily with the new mysterious layer, but if not — you get a whole new layer of complexity that you need to understand and your life will get exponentially more miserable. Unless your app constantly manages big amounts of data, Avoid those frameworks.
- Use simplest logic possible
Say you need show different HTML on different occasions. You can use client-side
routing with controllers and data passed to each controller that renders the HTML from a template. Or you can just use static HTML pages with normal browser navigation, and update manually the HTML. Use the second.
- Make short Javascript files
Limit the length of your JS files to a single editor page, and make each file do one thing. Can’t cramp all your glorious logic into small modules? Good, that means you should have less of it, so that other humans will understand your code in reasonable time.
- Avoid pre-compilers and task-runners
The more layers there are between what you write and what you see, the more logic your mind needs to remember. You might think grunt or gulp help you to simplify stuff but then you have 30 tasks that you need to remember what they do to your code, how to use them, update them, and teach them to any new coder. Not to mention compiling.
Side note #1: CSS pre-compilers are OK because they have very little logic but they help a lot in terms of readable structure, compared to plain CSS. I barely used HTML pre-compilers so you’ll have to decide for yourself.
Side note #2: Task-runners could save you time, so if you do use them, do it wisely keeping the minimalistic mindset.
- Use Javascript everywhere
This one is quite specific, and I am not absolutely sure about it, but having the same language in client and server can simplify the data management between them.
- Write more human code
Give your non trivial variables (and functions) descriptive names. Make shorter lines but only if it does not compromise readability.
Treat your code like poetry and take it to the edge of the bare minimum.
I have a simple coding principle: write lines of code as though instructing a real person how to do the job.
And that naturally expands to designing and modelling systems like real organisations with their own divisions, departments, branches, and all the various “job positions” that need to be filled.
Make the whole system appear as human as possible.
This has been one of my pet peeves for years. The other one is using professional jargon when communicating with users.
Rubel Musicwould, [31.10.19 16:15]
https://publishers.propellerads.com/#/pub/ref/banners
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/300x250-popads_1.gif” alt=“PropellerAds”></a>
Rubel Musicwould
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/300x250-propellerads-monetize-traffic.gif” alt=“PropellerAds”></a>
Rubel Musicwould
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/300x250_06.gif” alt=“PropellerAds”></a>
Rubel Musicwould
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/300x250_03.jpg” alt=“PropellerAds”></a>
Rubel Musicwould
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/300x250_02.jpg” alt=“PropellerAds”></a>
Rubel Musicwould
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/300x250_01.jpg” alt=“PropellerAds”></a>
Beneficiary website Link Shopping
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/300x250_05.gif” alt=“PropellerAds”></a>
Rubel4391
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/300x250_04.gif” alt=“PropellerAds”></a>
FeniBD
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/250x250_02.gif” alt=“PropellerAds”></a>
Rubel Video
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/250x250_01.gif” alt=“PropellerAds”></a>
rubelmusicwould
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/728x90-popads_1.gif” alt=“PropellerAds”></a>
Md Rubel
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/728x90-propellerads-monetize-traffic.gif” alt=“PropellerAds”></a>
rubel4391@gmail.com
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/728x90_07.gif” alt=“PropellerAds”></a>
Verified Ads Propeller
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/728x90_04.jpg” alt=“PropellerAds”></a>
Ads Instarst Beneficiary
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/728x90_03.jpg” alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/728x90_02.jpg” alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/728x90_01.jpg” alt=“PropellerAds”></a>
Rubel Musicwould
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/728x90_06.gif” alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/728x90_05.gif” alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/468x60-popads_1.gif” alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/468x60-propellerads-monetize-traffic.gif” alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/468x60_04.gif” alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/468x60_03.jpg” alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src="http
Rubel Musicwould, [31.10.19 16:15]
://promo.propellerads.com/468x60_02.jpg" alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/468x60_01.jpg” alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/160x600-popads_1.gif” alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/160x600-propellerads-monetize-traffic.gif” alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/160x600_04.gif” alt=“PropellerAds”></a>
<a href=“https://publishers.propellerads.com/#/pub/auth/signUp?ref_id=Zadk”><img src=“http://promo.propellerads.com/160x600_03.jpg” alt=“PropellerAds”></a>
<a href="https://p