Skip to content

Latest commit

 

History

History
558 lines (316 loc) · 10.8 KB

README.md

File metadata and controls

558 lines (316 loc) · 10.8 KB

📖 Ionică Bizău's Code Style ❤️

Support me on Patreon Buy me a book PayPal Ask me anything Version Downloads Get help on Codementor

Buy Me A Coffee

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 declaration
var foo = 1;

// Multiple declarations
var 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.

// Dependencies
const http = require("http")
   , fs = require("fs")
   , EventEmitter = require("events").EventEmitter
// Constants
const PI = Math.PI
    , 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;
function bar (x) {
    var someMethod = function (m) {
        console.log(m);
    };
    return { y: x, foo: someMethod };
}
class Foo {
    ...
}

Method and property definitions 📎

I use the ES6 class for creating classes.

class Person {
    constructor (name, age) {
        this.name = name;
        this.age = age;
    }
    getName () {
        return this.name;
    }
}

Deleting properties ❌

I nullify the properties when that's fine:

var foo = {
    bar: 42
};
foo.bar = null;

However, I use the delete keyword when I really want to delete them.

delete foo.bar;

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.forEach(c => {
    // do something
});

However, using for loops is fine too:

for (var i = 0; i < arr.length; ++i) {
    for (var ii = 0; ii < arr[i].length; ++ii) {
        ...
    }
    ...
}

For objects, I use the following style:

Object.keys(obj).forEach(k => {
    var cValue = obj[k];
    // do something
});

To simplify this, I created iterate-object, which abstracts this functionality:

const iterateObject = require("iterate-object");
iterateObject(obj, (value, key) => {
    // do something
});

Multiline strings 🎸

I use backticks to create multiline strings:

var multiLineStr = `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
New 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 require
const fs = require("fs")
    , events = require("events")
    , EventEmitter = events.EventEmitter
    ;

// Local variables
var x = 1
  , twoWords = "Hello World"
  ;

// Functions
function fooBar () {...}

// Classes
class Person {
    constructor (name, age) {
        this.name = name;
        this.age = age;
    }
    getName () {
        return this.name;
    }
}
// Object fields
var obj = {
    full_name: "Johnny B."
  , age: 20
};
obj.methodA = function () {...};

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.

// Arrays
var 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"
];

// Objects
var 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.

foo(x);
bar(x);

foo(y);
bar(y);

Binary and Ternary operators

See examples.

var foo = someObj
    .method()
    .method2()
    .method3()
    ;

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.

// Dependencies
const lib1 = require("lib1")
    , lib2 = require("lib2")
    ;

// Constants
const FOURTY_TWO = 42;

Use JSDoc comments for functions and methods.

/**
* sum
* Calculates the sum of two numbers.
*
* @name sum
* @function
* @param {Number} a The first number,
* @param {Number} b The second number.
* @return {Number} The sum of the two numbers.
*/
function sum (a, b) {
  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:

  1. Please post questions on Stack Overflow. You can open issues with questions, as long you add a link to your Stack Overflow question.
  2. For bug reports and feature requests, open issues. 🐛
  3. 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 🚀

  • Buy me a book—I love books! I will remember you after years if you buy me one. 😁 📖

  • PayPal—You can make one-time donations via PayPal. I'll probably buy a coffee tea. 🍵

  • Support me on Patreon—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! ❤️

📜 License

MIT © Ionică Bizău