---
# System prepended metadata

title: Code Readability and Commenting

---

# Code Readability and Commenting

## Code Readability

### General
- Prettify your code before committing it to git.
- Do not mutate an object or state. Use immutability-helper instead.
- Add proper comments to your code wherever necessary.
- Do not chain more than two operations in one statement. Separate those into separate operations and use them in conjunction. An example of that would be:

    **Not/less readable:**

    ```jsx
    const data = _.get(getValueList().filter(values, value => value.key), 'node', {});
    ```

    **Comparatively more readable:**

    ```jsx
    const valueList = getValueList().filter(values, value => value.key);
    const data = _.get(valueList, 'node', {});
    ```
- Remove unused 
    - module imports
    - css classes
    - passed props
    - module functions 
- Do not commit commented code. Please remove them before committing it to git. An example of that would be:

    ```jsx
    // const a = 2;
    // const b = 'abc';
    ```

    If you want to keep it intentionally then please add a note above it mentioning its reason. Also TODO flag can be used if you are commenting a pending work. But commenting code in any case is not recommended.

- Do not commit **debugger**(s) and **console**(s) with your code. Review your changes carefully before committing it.
    - console.error used in catch block are exceptions
- Each and every piece of your code should be lint passed.
- Declare the functions/methods above the code that uses them.
- Group the statements written with same intention together (Law of proximity).
- Deconstruct the props and state to be used on the top in functional scope
- A function must be written with an intention to handle only one task. Try not to acheive more than one thing in a same function.
- Take the help of coercion in evaluating truthness or falsiness. E.g: !!currentScreen
- Early returns promote code readability with negligible performance difference
```JSX
// Bad:
function returnLate( foo ) {
  var ret;

  if ( foo ) {
    ret = "foo";
  } else {
    ret = "quux";
  }
  return ret;
}

// Good:

function returnEarly( foo ) {

  if ( foo ) {
    return "foo";
  }
  return "quux";
}
```
- An empty block or block-like construct may be closed immediately after it is opened, with no characters, space, or line break in between (i.e. {}), unless it is a part of a multi-block statement (one that directly contains multiple blocks: if/else or try/catch/finally).
    - Example
    ```JSX 
    function doNothing() {}
    ```
    - Disallowed
    ```JSX 
    if (condition) {
      // …
    } else if (otherCondition) {} else {
      // …
    }

    try {
      // …
    } catch (e) {}
    ```
- Use object shorthand. 
    - Example
      ```JSX
          const name = "John";
          const age = 18;
          
          const person = {
              name
              age
          };
      ```
    - Disallowed
      ```JSX
          const name = "John";
          const age = 18;
          
          const person = {
              name: name
              age: age
          };
      ```
### Naming Conventions
- Dont hesitate to write descriptive variable or function names.
- Be kinder, more thoughtful in naming
    - 'student' is storing single student(Object) and 'students' is storing array of students(Objects).
    - variable names should be based on what it is storing. 'studentIds' if storing array of student 'ids' and 'students' if storing array of students(Objects). 
- Follow camelCase when naming a variable or methods.
- Follow PascalCase when naming components or file names.
- Constants should follow UPPER_SNAKE_CASE.
- In-short:
    - functionNamesLikeThis
    - variableNamesLikeThis
    - ComponentNamesLikeThis
    - EnumNamesLikeThis
    - ENUM_ITEM_LIKE_THIS
    - methodNamesLikeThis
    - SYMBOLIC_CONSTANTS_LIKE_THIS
- Camel case: defined
    - Don't use abbreviation until its well-known. Examples
        - thumbnailUrl (Good)
        - thumbUrl(Bad)
        - studendId (Good)
        - stndId(Bad)
    - Few other examples
         |Prose form|Correct|Incorrect| 
         |----------|-------|---------|
         |"XML HTTP reuest"|XmlHttpRequest|XMLHTTPRequest|
         |"new student ID"|newStudentId|newCustomerID|
         |"inner stopwatch"|innerStopwatch|innerStopWatch|
         |"supports IPv6 on iOS?"|supportsIpv6OnIos|supportsIPv6OnIOS|
         |"YouTube importer"|YouTubeImporter|YoutubeImporter|
         |"non-empty"|nonEmpty|nonempty|

## Commenting
### Introduction

> The goal of every programmer should be to write code so clean and expressive that code comments are unnecessary. Well named classes, functions, and variables should guide the reader through your code like a well-written novel.

We all write some code at some point of time based on problem description. And when we face some issue in that particular section, after visiting again, we get troubled at understanding that how this used to work? Were there any hack/exceptions covered? And in these scenarios we lose, unnecessarily, a lot of time which could have been avoided.

So, what’s the solution for this? Yes, you might have guessed right. Writing comments.

Commenting is essential and integral part of writing a clean code. As we all know that how important a cleaner code is. It reduces the issues related to understanding the functionality and state of the piece of code when any developer visits it again. Enforcing this good habit is bit tedious initially but in future it’s very helpful in understanding the right piece of code  in very short span.

Based on the scenarios and type of codes, the commenting rules have been defined and needed to be followed strictly to have a common standard practices which can be helpful for peer developers.


Types
* **Documentation comments**: Documentation comments are intended for anyone who is likely to consume the source code, but not likely to read through it.
  * Reusable components
  * Utilities
  * Modules
* **Clarification comments**: Clarification comments are intended for anyone (including your future self) who may need to maintain, refactor, or extend your code. For hacks, exceptions, tricky statements, optimisations, etc .
  * Non reusable components
  * Reusable components
  * Stylesheets
  * Utilities
  * Modules

Benefits
* Developer gets the idea: what and why(important than what) of functionality before going through the code.
* Reduces the time to understand, hence, easy to debug. 
* Code becomes easy to maintain.
* Helpful in identifying hacks or exceptions(if any) in code.
* A well commented code can be a great help for any level of developers, especially junior developers.

Issues
* Need Maintenance


### Rules
The rules are divided by the type of code a developer writes.

Components
Component description
![alt text](https://codahosted.io/docs/-1CTYQZHuy/blobs/bl-0nsoowSY0v/d59cf82cce97a116f0d1361c6fe1f6479d40bb295e851ea7d083e2b5c673b94c1089c6bf9ddb808f1e7790571449c237bafe3b05779dea31e916843f702ff25aa5d4a86e8c5d5a9f425401fc72942544a68b2b0b747a1be726c7c6a57b5667aa9bf07a6f "Component description")


Method description
![alt text](https://codahosted.io/docs/-1CTYQZHuy/blobs/bl-nqh5isiCRW/3d6cb5f71c59ce5775f9639fe3c1540da84a3d18510bdd4a92a20142994b6259467d8940b3d1c80abc4079bd883330815fe5137d708c36a2a638784dc1e07cd0615417ca03374a4480dcd09507d41a820e63559c708af91629aead62563fcbac8f270b2f "Method description")

Clarification comment
![alt text](https://codahosted.io/docs/-1CTYQZHuy/blobs/bl-Wn-F1sL8v0/57a49246bae7d5d5be94ee96c0921cfc2d48c4a966d9c168726065fb20039101ffa61191369f05bce62fe73ffb23cbf29b2b4c660c1dc596a8fa0653303fad7ac9c3ba4627633cbeb043c92ff664d598e6be53bf13f0e1cbc399002a5f5315e24db2e4ce "Clarification comment")




Utilities
![alt text](https://codahosted.io/docs/-1CTYQZHuy/blobs/bl-cqGewY6H8u/3f12340fdd5fa7397a6d2c316a01763bd8a5399fd1dea36a073e431d477ff8e877d98008ece1e3efaf3ccfe07101cab08e504831ff93bce944f46cc30e038a2850099f6259908ee8045646606ae053aa1d6890e79c1fa81ef61530105c123cbb168221ca "Utilities")


Hacks
![alt text](https://codahosted.io/docs/-1CTYQZHuy/blobs/bl-ymL2JENTSy/8beb0deb20ea8c5c4141dbbc16fd372cc8642eb0909104966c9e7a715188f65c210dc0e22d36032800bb096acab8f5260550ae86379929727e1d26967100b64f00a4d4c9efc6e538bd16f8322b45100b3a27cf6803746e8a55b31d12e97ecca158aadff3 "Hacks")


Unused codes
There is a special condition with unused codes. Generally, it’s not preferred to keep the unused codes but if it’s somehow important in future applications just comment the piece and write few comments about it.
![alt text](https://codahosted.io/docs/-1CTYQZHuy/blobs/bl-a08Rw-hlYJ/bae09b4325f9882ca582bb882ff78e3f9db1f334a4904ecd331e1e1aac332278bf3b2813e576c1e5eb899352aa546624fae9680493c83255701bd0c6b5c67b3b8146d45f260a81523fba2dfa36f7a3c1c3ca4e0cd3c8c9c29e3ea2b47050acc3572336bc "Unused codes")


### Reference

* For detailed JSDoc documentation please refer to this link: [https://jsdoc.app/about-getting-started.html#:~:text=JSDoc 3 is an API,HTML documentation website for you](https://jsdoc.app/about-getting-started.html#:~:text=JSDoc%203%20is%20an%20API,HTML%20documentation%20website%20for%20you).