Git Product home page Git Product logo

my-coding-best-practices's Introduction

Coding practices for writing Javascript/react application

Disclaimer: I prefer to write this my code following these guideline. Feel free to adopt or ignore.

  1. Imports

Imports can be a lot and messy, especially if you have named exports. Developer should not be spending a lot of time handling them. For example, grouping them based on their functionality like hooks, utils, etc.

I generally,

  • Segregate the imports to 2 groups, one consisting imports from node-modules and other from project source folder.
  • Sort the individual group alphabetically.
  1. Objects

Javscript is all about objects. As our application grow we need to add/remove certain properties, it can be a headache for future developers to find the fields to add/remove. Do them a favour and always

  • Sort the object keys alphabetically.
  1. Exports

  • Export ONE thing from a file, Separation of concerns.
  • Always use default export.
  1. Test cases

WHERE TO KEEP TEST CASES?

This is debatable, go around in the team, take a vote and decide. I prefer a folder replicating the exact src folder. And all the test cases within it. I then open two instances of VS code and work.

  1. Typescript

Typescript is awesome but it comes at a cost of lot extra symbols, try keep your files as light as possible.

  • Define the types in a separate folder.
  • Avoid exporting types from files containing react component. It's just messy.
  1. Code flow

  • Write code that flows naturally. Use your judgement. Read before commiting.
  • Don't force atomicity. Use Separation of concerns and your judgement to disntegerate the code.
  1. Performace

  • Follow tree shaking best practices, like
  className.do(methodName)

This allows you to write functions outside of your class and when bundling only the methods used will go in the bundle.

Test performance using dev tools or

console.time("...")

performance.now("...")

React

React Docs already mentions lot of best practises, do follow them. Read famous github repos for best practice, and tricks. Render props is very powerful, when it comes to code reusability.

  1. Props

  • In case of functional component, expand the props within the function brackets ({...})
  • In case of class component, expand the component as the first thing or after the return null statement inside the member functions.
  • Sort the props while calling the component.
  1. Render functions

  • In case of return null or similar stmt, put it as early as possible and put all the unnecessary login after that.
  1. Hooks

  • Use useMemo() and useCallback() wisely.

*WILL KEEP UPDATING*

my-coding-best-practices's People

Contributors

udit1994 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.