Skip to content

Coding Standards

Apratim Shukla edited this page Apr 1, 2020 · 2 revisions

Refer to this document and follow these standards while coding.

JavaScript

No semicolons

Yes, it may sound weird to some, but javascript can written without semicolons. For maintaining consistency, semicolons should not be used (both server side and client side).

Variable naming

Do not use variable names like a, b, or xyz. Use suggestive variable names so that other developers can also understand your code. Also, use snake_casing instead of camelCasing or PascalCasing.

Wrong Right
var i = 0
var numUsers = 0
var NumUsers = 0
var num_users = 0

Exporting modules

For exporting functions, export them as you create them. Avoid using module.exports wherever possible. This eliminates the possibility of forgetting to export a functionality.

Wrong Right
var login = (req, res) => {
// something
}
var logout = (req, res) => {
// something
}
module.exports = {login, logout}
exports.login = (req, res) => {
// something
}
exports.logout = (req, res) => {
// something
}

Function definitions

Use the shorthand version.

Wrong Right
var x = function(x, y) {
return x \* y
}
var x = (x, y) => x \* y

Handling HTTP requests

Sending status with HTTP requests is necessary.

Wrong Right
res.send("OK") res.status(200).send("OK")

HTML

Guidelines:

  • Always keep your code tidy.

  • Always declare the document type first.

    Wrong Right
    <!doctype html> <!DOCTYPE html>

         Note that DOCTYPE is written in capital not small, in all HTML documents.

  • Use lowercase for element names as well as attributes.

    Wrong Right
    <SECTION></SECTION> <section class="hello"></section>

         DO NOT use uppercase for any element names.

  • Use closing tags, don't omit them and they must come on a new line.

    Wrong Right
    <div. . . .</div> <div>
    ...
    </div>

         Exception: If there's only one line of content within a tag, ending tag should be on the same line.

  • Nested elements should be tabbed like so:

    Wrong Right
    <div id="parent"><div id="child"></div></div> <div id="parent">
         <div id="child">
         </div>
    </div>
  • Always add an alt tag to images.

  • DO NOT use spaces between equal signs or before and after angle brackets.

    Wrong Right
    <div id = " hello " > <div id="hello">
  • There should be no embedded JavaScript or CSS code in HTML and that should be separately imported. The imports should be mentioned in the &lt;head&gt;&lt;/head&gt; tag. JavaScript and CSS includes should be separated from each other within the heading tag. Moreover, the online and local JavaScript and local includes should be separated from each other. Proper comments are necessary to know what's what.

    Wrong Right
    wrong-includes correct-includes
  • Comments

    Wrong

    <!-- This is a long comment example. This is a long comment example.This is a long comment example. This is a long comment example.->

    Right

    <!-- This is a long comment example.
    This is a long comment example.
    This is a long comment example.
    This is a long comment example.
    ->

  • Encapsulate the entire HTML body in the container class.

  • Use forms wherever possible and refrain from using the onclick function on each button and making AJAX requests.