How to make your JS code better to read

As a JS programmer, if you write, if you look easy to read, not just look good, after another programmer takes over, it will be unusually smooth.

Do not leave the code in the code

left to git to manage, otherwise you want to do it

// Bad // Function Add () {// const a = b + c // Return A / /} function add () {RETURN A + 1000} // goodfunction add () {RETURN A + 1000}
   Appropriate wirework 

// Badfunction A ( ) {const {state_a, state_b, state_c} = this.state this.setState ({State_a: State_a * 2}) Return ‘DONE’} // Goodfunction a () {const {state_a, state_b, state_c} = this.state THIS.SETSTATE ({State_a: State_a * 2}) Return ‘Done’}

 Additional comments, but don't crazy add notes   
Code annotation for a code or a line of special attention

Don’t come crazy, too, beautiful code you will speak


// baDCONST A = ‘a’ // This is aconst b = ‘b’ // This is bconst c = ‘c’ // this is c // good / ** * declared variable * / const a = ‘a’ Const B = ‘b’ const c = ‘c’

  will be classified with similar behavior, named  

// Badfunction Handleclick (arr) {const a = 1 arr.map (e => e + a) const b = 2 returnction handleClick (arr) {const a = 1 const b = 2 Arr.map (E => E + a) Return Arr.Length + B}

In the absence of semancy, ‘can save the province’
   The function in JS is a first-class citizen 
However, if it affects readability, it is failed


Readiness and simplicity must always choose one, always select readability



Function add (a) {RETURN A + 1} function DOSMETHING ()} / / Badarr.map (a => {returniful (a)}) settimeout (() => {dosomething ()}, 1000) // Goodarr.map (add) settimeout (dosomet)HING, 1000)

Arrow function
   // BadConst a = (v) => {RETURN V + 1} // GoodConst A = V => V + 1 // BadConst B = (V, I) => {Return {V, I}} // Goodconst B = (V, I) => ({v, i }) // badconst c = () => {return (dispatch) => {// dosomething}} // goodconst c = () => dispatch => {// dosomething} 

In advance to the value (written by the study of React)
  // BadConst a = this.props.prop_a + this.props.prop_bthis .props.fun () // goodConst {prop_a, prop_b, fun} = this.propsconst a = prop_a + prop_bfun ()  
Rational use of various expressions

// Badif (CB) {CB ()} // goodcb && cb () // Badif (a) {return b} else {return c} // goodreturn a ? b: c // badiF (a) {c = a} else {c = ‘default’} // goodc = a || ‘default’

 Chain Call Writing   
// BadFetch (URL) .Then (res => {return res. json ()}). Then (() => {// dosomething}). Catch (e = > {}) // GoodFetch (URL) .Then (res => {return res. json ()}) .Then (() => {// dosomething}) .catch (e => {})

Keeping code is longitudinally developed
 When the code in the entire file is specifically 'highlight', it should be considered to make a wrap for them   // Badreturn Handleclick (Type, Key, Ref, Self, Source, Props) // Goodreturn HandleClick (Type, Key, Ref, Self, Source, Props) // BadConst a = this.props.prop_a === 'Hello'? 
World : null // Goodconst a = this.props.prop_a === ‘Hello’?
World

: Null

                    
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment