Thanks, Oleksii, for the great coverage of how a developer should write good code!
From my perspective, I would add the following:
Variables naming
The shorter, the better:
consts=1000
instead of
constMILLISECONDS_IN_ONE_SECOND=1000
You should decrease your variables/functions names.
Comments
Write comments in your native language - totally agree!
In case you need to leave a comment about fixing something in the future, please use it like this:
// FIXME: this function returns an invalid result// the problem could be in the incorrect algorithm chosen// to calculate a resultfunctionadd(a,b){returna-b}
One-line code
You should write a one-line code because it can save a ton of memory! Just imagine how much performance gain you reach once you rewrite your app into one-liners!
Errors
Just don't catch them, I agree with Oleksii. You will see errors if your app fails anyway, so why do you need to write additional code to catch them?
Callbacks vs. straightforward code
Use callbacks. Especially, if you have more than 5 of them.
Bonuses:
this manipulating
Don't create too many variables, just use "this" context!
Examples:
this.f=()=>{returnerr("error")}// as you remember: use shorter names, this is bad nameconsterr=u=>thrownewError(u)// 1. Also bad function name// 2. Good names for arguments// 3. Function takes more than 2-3 arguments which is greatfunctioncalculateHash(context,cb,k,e,i,few){if(this.true==context.true)returncb(false)if(typeofthis.cb==='function')k(cb(i))returnmd5(k(i),few+e)}
Functions return signature
Write functions that can return multiple types and unexpected results like this:
Just don't use tools for code linting. They only take additional place in your "package.json".
Write code as you want, especially if there is more than one developer in a team.
Unnecessary code
Don't delete the code your app doesn't use. At most, comment it.
Logic fragmentation
Don't divide a program logic into readable pieces. What if your IDE's search brokes and you will not be able to find the necessary file or function?
10 000 lines of code in one file is pretty OK.
1000 lines of a function body is pretty OK.
Dealing with many services (3rd party and internal, also, there are some helpers, database hand-written ORM and jQuery slider) in one service.js? It's OK.
Thanks, Oleksii, for the great coverage of how a developer should write good code!
From my perspective, I would add the following:
Variables naming
The shorter, the better:
instead of
You should decrease your variables/functions names.
Comments
Write comments in your native language - totally agree!
In case you need to leave a comment about fixing something in the future, please use it like this:
Not like this:
One-line code
You should write a one-line code because it can save a ton of memory! Just imagine how much performance gain you reach once you rewrite your app into one-liners!
Errors
Just don't catch them, I agree with Oleksii. You will see errors if your app fails anyway, so why do you need to write additional code to catch them?
Callbacks vs. straightforward code
Use callbacks. Especially, if you have more than 5 of them.
Bonuses:
this
manipulatingDon't create too many variables, just use "this" context!
Examples:
Functions return signature
Write functions that can return multiple types and unexpected results like this:
Code linting
Just don't use tools for code linting. They only take additional place in your "package.json".
Write code as you want, especially if there is more than one developer in a team.
Unnecessary code
Don't delete the code your app doesn't use. At most, comment it.
Logic fragmentation
Don't divide a program logic into readable pieces. What if your IDE's search brokes and you will not be able to find the necessary file or function?
service.js
? It's OK.Very good points! I've updated the main list 👍🏻