In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces you to avoid the common JavaScript errors of the 9 codes have, the content is very detailed, interested friends can refer to, hope to be helpful to you.
There are some very common programming errors that we need to avoid to keep the code clean and readable.
There are too many things in a function.
A function should only do one thing, which is what it indicates.
If there is something else, then it should be moved to its own function, and there are too many things in a function that make it difficult to read and understand.
For example, if you have a function that can calculate both subtotals and taxes, because they are different, you should divide them into two separate functions.
Commented out code
Commented-out code is code that cannot be run. Therefore, they should not appear after we have completed this part of the code.
When finished, we should delete the comments or uncomment them.
Either way, the final product should not comment out the code in it.
Non-descriptive variable name
Naming variables in a way that doesn't convey their meaning, once we forget the code we wrote and look back, it frustrates the reader and ourselves.
Therefore, we should use the variable that describes the variable name to name the variable.
Instead of writing "let x", write something like let numApples, so we know that our variable will hold the number of apples.
Magic numbers and strings
We should not have magic numbers and strings. These values appear in multiple values and represent the same thing, but are not explicitly explained in the code.
For example, if we have the following code:
For (let I = 0; I < 10; iTunes +) {/...}
Then we don't know what 10 means. Instead, we should set it to a named constant so that we can understand its meaning.
For example, we can write like this:
Const numApples = 10; for (let I = 0; I < numApples; iTunes +) {/...}
Now we know that 10 actually means the number of apples.
A messy code format
Messy code formatting can make the code difficult to read, so we should clean it up using lint or a code formatter.
There are many options there, so we can use a tool like Prettier or ESLint to clean it up automatically.
Hard-coded value
We should not hard-code values into our code, especially if they are private. Instead, we should use them as environment variables and read all the values from there.
There are many ways to do this. For example, for the front end, both Angular,React and Vue have places to retain variables to save them in different environments in different files.
For the backend, we can use things such as dotenv packages to read environment variables from .env files so that we don't have to hard-code them.
Duplicate code
Repetitive code is not good. If we change some repetitive things, we have to change them in all the repetitive places.
Instead, we should move the public parts to their own files so that they can be used in multiple places.
The DRY principle applies everywhere. If we copy and paste them exactly as they are and use them, we should move them to a shared location.
Do not back up the code
We should use Git to manage the code so that we can provide local and remote repositories for our code.
In this way, we can automatically keep a copy in the remote location. Similarly, we can easily restore the error code and easily get the code from an earlier submission.
We can't do this without version control.
If we don't back up the code, then if something goes wrong, we may lose everything.
Complex code
Complex code should be simplified so that we can understand them more easily.
Summary
These common mistakes can be easily avoided as long as we pay attention to what we do.
It's always good to break things down into small pieces, just like keeping things simple without repeating them.
When commented out, non-descriptive variables and values should also be replaced with something more meaningful.
On the common JavaScript errors to avoid which 9 codes are shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.