📖 Ionică Bizău's Code Style ❤️
This document contains guides that I defined and follow when building things.
Open issues with any questions, ideas, fixes etc. 😇
Contents
Variable declarations 📝
Variables 💬
Using var
in general or let
when they should be accesible only in specific blocks (e.g. if
).
// One declarationvar foo = 1; // Multiple declarationsvar foo = 1 bar = "Hello World" anotherOne = foo: "bar" ; if ... let baz = 42; /* do something with baz */
Constants 🚩
Using const
. The constant names are written with UPPERCASE letters. I also use const
when including libraries using require
and when they should not be changed. In this case, the names will not be with caps.
// Dependenciesconst http = fs = EventEmitter = EventEmitter// Constantsconst PI = MathPI MY_CONSTANT = 42 ;
Globals 🌍
I define globals when there is no commonjs environment (this is actually handled by dist-it
. When I manually define globals, I do that using window.MyGlobal
(on the client) and global.MyGlobal
(on the server).
Semicolons ✏️
I use semicolons. Almost always.
var foo = 1; { var { console; }; return y: x foo: someMethod ;} ...
Method and property definitions 📎
I use the ES6 class
for creating classes.
{ thisname = name; thisage = age; } { return thisname; }
Deleting properties ❌
I nullify the properties when that's fine:
var foo = bar: 42;foobar = null;
However, I use the delete
keyword when I really want to delete them.
delete foobar;
eval()
eval
is evil. 😡 Do not use it. However I use it in some test files and in places where I have to execute the JavaScript code provided by the user.
For converting strings to JSON, use JSON.parse(strObj)
.
Iterating objects and arrays
For arrays, most of times, I use the forEach
function:
arr;
However, using for
loops is fine too:
for var i = 0; i < arrlength; ++i for var ii = 0; ii < arrilength; ++ii ... ...
For objects, I use the following style:
Object;
To simplify this, I created iterate-object
, which abstracts this functionality:
const iterateObject = ;;
Multiline strings 🎸
I use backticks to create multiline strings:
var multiLineStr = `Lorem ipsum dolor sit amet, consectetur adipisicing elitsed do eiusmod tempor incididunt ut labore et dolore magnaaliqua. Ut enim ad minim veniam, quis nostrud exercitationullamco laboris nisi ut aliquip ex ea commodo consequatNew line again...`;
Modifying prototypes of built-in objects 💩
Just don't, unless that's the scope of the library.
Naming things 💭
Using camel case notation for variables, in general. For constructors I capitalize the variable name (e.g. EventEmitter
).
// Node.JS requireconst fs = events = EventEmitter = eventsEventEmitter ; // Local variablesvar x = 1 twoWords = "Hello World" ; // Functions {...} // Classes { thisname = name; thisage = age; } { return thisname; }// Object fieldsvar obj = full_name: "Johnny B." age: 20;obj {...};
Curly braces ➰
Open the curly brace at the end of the line. Always put the instructions between curly braces, even there is only one instruction.
if expr instr; else instr2; instr3;
Array and Object Initializers 📁
See examples.
// Arraysvar arr = 1 2 3 4; var lotOfElms = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18; var bigElms = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod." "Tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim." "Veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea." "Commodo consequat. Duis aute irure dolor in reprehenderit in voluptate"; // Objectsvar obj = a: 1 ; var obj1 = full_name: "Johnny B." age: 20;
Commas
Put commas at the beginning of the line, not at the end.
var x = 1 y = 2 ; const C_1 = 42 C_2 = -42 ; var obj = x: 1 y: 2;
Blank lines
Group the instructions inserting some blank lines where it's needed.
;; ;;
Binary and Ternary operators
See examples.
var foo = someObj method ; var a = cond ? v1 : v2; var b = long_condition_here ? v1 : v2 ; var c = another_long_condition_here ? with_some_long_value : or_another_some_long_value ;
Quotes 💬
Double quotes, with some exceptions when single quotes are used.
var foo = "\"Hello\", he said.";var jQuerySelector = "div.myClass[data-foo='bar']";
Comments 🎶
Put relevant comments. The comments start with uppercase letter.
// Dependenciesconst lib1 = lib2 = ; // Constantsconst FOURTY_TWO = 42;
Use JSDoc comments for functions and methods.
/*** sum* Calculates the sum of two numbers.** @name sum* @function* @param* @param* @return */ { return a + b;};
I use the blah
tool to generate documentation.
$ npm install -g blah$ blah --readme$ blah --docs some-file.js
Project naming
I use name-it
to generate project names.
Project licenses
I 💖 open-source! I prefer the MIT license.
❓ Get Help
There are few ways to get help:
- Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.
- For bug reports and feature requests, open issues. 🐛
- For direct and quick help, you can use Codementor. 🚀
😋 How to contribute
Have an idea? Found a bug? See how to contribute.
💖 Support my projects
I open-source almost everything I can, and I try to reply to everyone needing help using these projects. Obviously, this takes time. You can integrate and use these projects in your applications for free! You can even change the source code and redistribute (even resell it).
However, if you get some profit from this or just want to encourage me to continue creating stuff, there are few ways you can do it:
-
Starring and sharing the projects you like 🚀
-
—I love books! I will remember you after years if you buy me one. 😁 📖
-
—You can make one-time donations via PayPal. I'll probably buy a
coffeetea. 🍵 -
—Set up a recurring monthly donation and you will get interesting news about what I'm doing (things that I don't share with everyone).
-
Bitcoin—You can send me bitcoins at this address (or scanning the code below):
1P9BRsmazNQcuyTxEqveUsnf5CERdq35V6
Thanks! ❤️