How to make your JS Code more readable and JavaScript code easier to read

Source: Internet
Author: User

How to make your JS Code more readable and JavaScript code easier to read

As a JS programmer, if the code written by myself looks nice and easy to read, it will not only look nice to myself, but it will be very smooth after other programmers take over the work.

Do not leave a large comment in the code

Leave it to git for management. What else do you want git to do?

// bad// function add() {// const a = b + c// return a// }function add() { return a + 1000}// goodfunction add() { return a + 1000}

Line feed as appropriate

// 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'}

Add comments as appropriate, but do not add comments as crazy.

Comments to a piece of code or a line of code that requires special attention

Don't comment crazy. It's too cool. Pretty code can speak on its own.

// Badconst a = 'A' // This is aconst B = 'B' // This is bconst c = 'C' // This is c // good/*** declarative variable * /const a = 'A' const B = 'B' const c = 'C'

Classify code with similar behaviors and names

// badfunction handleClick(arr) { const a = 1 arr.map(e => e + a) const b = 2 return arr.length + b}// goodfunction handleClick(arr) { const a = 1 const b = 2 arr.map(e => e + a) return arr.length + b}

In the case that semantics is not damaged, 'saving is saved'

Remember that functions in JavaScript are first-class citizens.

However, if it is omitted to affect readability, it will fail.

If you must select either readability or conciseness, always select readability first.

function add(a) { return a + 1}function doSomething() {}// badarr.map(a => { return add(a)})setTimeout(() => { doSomething()}, 1000)// goodarr.map(add)setTimeout(doSomething, 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}

Get the object values in advance (react users must understand)

// 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'

Method of chained call

// badfetch(url).then(res => { return res.json()}).then(() => { // doSomething}).catch(e => {})// goodfetch(url) .then(res => { return res.json() }) .then(() => { // doSomething }) .catch(e => { })

Keep code vertically developed

We should consider wrapping the code that is particularly 'paint' in the entire file.

// badreturn handleClick(type, key, ref, self, source, props)// goodreturn handleClick( type, key, ref, self, source, props)// badconst a = this.props.prop_a === 'hello' ? <di>world</div> : null// goodconst a = this.props.prop_a === 'hello' ? <di>world</div> : null

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.