Clean Code in JavaScript — Part 2

12 clean code tips (JavaScript) that every developer should know!

Raghav Bang
RaghavBang
Published in
3 min readDec 24, 2022

--

  1. Never use shortform while naming variable/function.

2. Make sure the functions names are verb/verb phrase and classes names are noun.

3. Use Computer Science terms, while naming variables/functions if possible. That makes understanding easy and quick.

4. Extract the bodies of try and catch block. That makes it more readable.

5. Always update the comment as you make changes. Outdated comment is more dangerous than not having a comment.

6. If possible, try to keep variables vertically close to the functions, in which they are used.

Local variables should be declared above their first usage.

In similar way private functions should be declared above their first usage.

7. Avoid passing a null to function as well as returning a null from it.

8. While writing if conditions, make sure to give an empty line before if condition. It makes it easier to read and understand.

9. It’s difficult to integrate a third-party library as well to keep checking the functionality of third-party library is not changing with new release.

The solution to this problem is writing unit test cases for methods that are being used from third-party library in your project.

10. Consistency (Other developer struggles a lot in when this is not followed while implementing/fixing a feature).

If a particular way has been used in project to do a thing, then do all similar things in same way. Be careful you are not breaking those approach.

11. Use Object Literals over Switch statements, whereever possible.

12. Always keep default/configurable variables at top of the file.

If you like this post and find it helpful you can give me claps and do, follow me😍!!

Clean Code in JavaScript — Part 1: Clean Code in JavaScript — Part 1 | by Raghav Bang | RaghavBang | Oct, 2022 | Medium

Ref:

  1. Clean Code by Robert Martin
  2. My own experience.

--

--