Tips on improving the readability of JS code

Source: Internet
Author: User
Tags readable

Logical flattening of

We work, may encounter some more complex logic, such as: Meet the condition 1, or XX, meet the conditions 2, or XX ... This is all satisfying Oo, written out about it:

The code is as follows Copy Code

if (condition1) {
if (condition2) {
if (Condition3) {
Todo
A
Lot
} else {
Do ZZ
}
} else {
Do Oo
}
} else {
Do xx
}

The result is that logical nesting is complicated and difficult to read. It's even worse if you catch the indentation and don't do well or change lines. At this time, it is usually possible to interrupt code execution early to make the logic flat. Methods for early interruption include return, throw, and die () and exit () in PHP. The following is the rearrangement code:

The code is as follows Copy Code

if (!condition1) {
Do xx
Return
}

if (!condition2) {
Do Oo
Return
}

if (!condition3) {
Do ZZ
Return
}

Todo
A
Lot

Of course, this approach is also applicable where exceptions are excluded. There are even radical statements that should not use else, but should be interrupted so early. I think that's a bit too much, if ... else ... The meaning is very clear.

Shortening the long-length logic judgment

There is an unwritten rule from the ancient times, a line of code 80 characters. As the display becomes larger, the old rules should be broken. But a single line of code too long will look very tired, so we still need to control. Very long code often appears in the logical judgment, such as the game's common test bullets have not hit the player's code, written in a better format is probably the case:

The code is as follows Copy Code

if (Bullet.x > enemy.x + enemy.width | | bullet.y > enemy.y + enemy.height | | bullet.x + bullet.width < enemy.x | | Bullet.y + Bullet.width < enemy.y) {
Console.log (' missed ');
}


It's not too short. Sometimes we have to add enemy.damagable,!enemy.immunephysical and other conditions to judge, this line is very long written. This time we need to use a little skill to shorten it. There may be many ways of thinking, which is to refine the "purpose"--to refine the purpose of a set of judgments into a variable that is both readable and shortens the code. Now let's use a piece of code to illustrate this:

The code is as follows Copy Code
var isgod =!enemy.damagable | | Eneymy.immunephysical,//judge whether the enemy can not attack/God mode
Ishit = Bullet.hittest (eneymy); "Collision detection" with that large string of extraction methods just now.
if (!isgod && ishit) {
Enemy.die ();
}


Summary

Both of these tips are simple, but the readability of the code is significant. The combination of various kinds of tricks will make the code more readable and easier to maintain.

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.