Coding Standards

What is Coding Standards and Guidelines?

Good software development teams want their programmers to follow a clear and consistent way of writing code, called coding standards. Each organization usually creates its own standards and guidelines based on the type of software they build. It’s important for programmers to follow these standards, because code that doesn’t meet them can be rejected during a code review.

Purpose of Coding Standards:

  • They make code written by different programmers look consistent.

  • They improve readability, maintainability, and reduce complexity.

  • They make it easier to reuse code and find errors.

  • They encourage good programming practices and help programmers work more efficiently.

Example:

Copy

// Not Correct: vulnerable to injection
app.get('/user/:id', (req, res) => {
    const sql = `SELECT * FROM users WHERE id=${req.params.id}`;
    db.query(sql, (err, result) => res.send(result));
});

// Correct: safe parameterized query
app.get('/user/:id', (req, res) => {
    const sql = 'SELECT * FROM users WHERE id = ?';
    db.query(sql, [req.params.id], (err, result) => res.send(result));
});

Why: Using parameterized queries prevents SQL injection attacks, making the code secure.

1. Indentation & Formatting

Proper indentation and spacing make code readable and maintainable.

Wrapping Lines:

  • Break lines after a comma or operator.

  • Prefer higher-level breaks than lower-level breaks.

  • Align new lines with the beginning of the previous expression.

White Spaces:

  • Use Tabs for indentation, not spaces.

  • Proper spacing makes code easier to read.

Blank Lines:

  • Two blank lines between logical sections, class/interface definitions.

  • One blank line between methods, properties, local variables, or logical sections inside a method.

Inter-term spacing:

  • Single space after a comma or semicolon.

Table-like formatting:

  • Align code logically for readability.

Example:

Copy

Why: Proper formatting and spacing make code easy to scan, reducing mistakes and improving collaboration.


2. Inline Comments

All comments should be write by English

Internal Code Comments

  • Single-line comments: Describe a single line or block.

  • Multi-line comments: Explain complex logic in detail.

Documentation Comments

  • All non-private classes, interfaces, methods, and properties should have documentation tags.

  • These comments generate documentation automatically (e.g., JSDoc for Node.js).

Best Practices:

  • Be concise but clear.

  • Explain why, not what.

  • Keep comments updated.

  • Avoid redundancy.

  • Use a consistent style.

Example:

Copy

Why: Comments should explain the reasoning behind the code and make it understandable to others.


3. Global Variables Usage

  • Minimize global variables to avoid conflicts and unintended side effects.

  • Prefer constants or local variables inside functions or classes.

Example:

Copy

Why: Using local variables prevents bugs caused by accidental overwrites in other parts of the program.


4. Naming Conventions

  • Variables & functions: camelCase (userList, addUser)

  • Classes: PascalCase (UserManager)

  • Constants: UPPER_CASE (MAX_USERS)

  • Avoid digits in variable names.

Example:

Copy

Why:

  • Makes the purpose of variables and functions clear.

  • Avoids confusion, improves readability, and makes it easy to locate files.


5. Structured Programming

  • Break complex problems into smaller functions or modules.

  • Each function should have a single responsibility.

  • Use clear control flow structures.

Example:

Copy

Why: Small functions are easier to test, maintain, and reuse in other parts of the project.


6. Error and Exception Handling

  • Implement proper error handling.

  • Use try-catch for exceptions.

  • Avoid using exceptions for regular control flow.

Example:

Copy

Why: Proper handling prevents crashes and makes debugging easier.


7. Coding Guidelines Summary

  • Avoid difficult-to-read code.

  • Follow language-specific style guides.

  • Write well-documented, commented code.

  • Use consistent spacing and line breaks.

  • Keep functions small and modular.

  • Conduct code reviews for quality.

  • Avoid excessive use of third-party libraries.

  • Never hard-code sensitive information.


8. Five Pillars of Code Quality

1. Readability

Copy

2. Maintainability

Copy

3. Reusability

Copy

4. Reliability

Copy

5. Performance Efficiency

Copy


9. Additional Node.js Guidelines

Version Control

Copy

Why: Clear commits make it easier to track changes and collaborate.

Testing

Copy

Why: Proper test names describe the functionality and simplify debugging.

Security

Copy

Why: Prevents SQL injection attacks and protects user data.

Documentation

Copy

Why: Clear module documentation helps team members understand functionality quickly.

CI/CD

Copy

Why: Running tests in CI ensures code quality before deployment.

Tools

Copy

Why: Linting and formatting enforce consistent coding standards and prevent common errors.


10. ESLint configurations

1. No Console Logs

Config:

Copy

Code:

Copy

Why: console.log often sneaks into production → messy logs + possible security leaks. Use warn/error (or a logger like winston) instead.


2. Semicolons

Config:

Copy

Code:

Copy

Why: Semicolons prevent Automatic Semicolon Insertion bugs, which can cause runtime errors.


3. Quotes

Config:

Copy

Code:

Copy

Why: Consistent quoting style improves readability and avoids unnecessary diffs.


4. Indentation

Config:

Copy

Code:

Copy

Why: Consistent indentation makes code easier to scan and reduces confusion in nested logic.


5. Trailing Commas

Config:

Copy

Code:

Copy

Why: Trailing commas make Git diffs cleaner when adding new items.


6. Curly Braces

Config:

Copy

Code:

Copy

Why: Always using {} prevents hidden bugs when adding more statements later.


7. Strict Equality

Config:

Copy

Code:

Copy

Why: == does type coercion ("5" == 5 → true). === is strict → safer and less confusing.


8. No Unused Variables

Config:

Copy

Code:

Copy

Why: Dead code makes projects harder to maintain and can hide bugs.


9. No Duplicate Imports

Config:

Copy

Code:

Copy

Why: Duplicate imports clutter the code and confuse readers.


10. No var, Prefer const

Config:

Copy

Code:

Copy

Why:

  • var has weird scoping issues (hoisting).

  • const signals values that never change → safer.


11. Import Order

Config:

Copy

Code:

Copy

Why: Organized imports make dependencies easy to scan.


12. React Refresh Rule

Config:

Copy

Code:

Copy

Why: Prevents exporting random values that break React Fast Refresh (hot reloading).

Last updated

Was this helpful?