Skip to content

πŸ“– This document contains guides that I defined and follow when building things. ❀️

License

Notifications You must be signed in to change notification settings

IonicaBizau/code-style

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

45 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“– 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

About

πŸ“– This document contains guides that I defined and follow when building things. ❀️

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published