In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What is the magic value of JavaScript, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Hello everyone, I am the fish skin, through one thing today, share an important tip when writing code.
Some time ago, I opened up a piece of code in the programming navigation project. The function is that when the user's operation fails, an error box will pop up on the page and prompt "operation failed". The code is as follows:
/ / error prompt const ERROR_MESSAGE = "Operation failed"; / / Delete resource const result = deleteResource (); if (! result) {alert (ERROR_MESSAGE);}
However, unexpectedly, a classmate directly left a message saying that it was unnecessary for me to write the code like this! Why define a constant for the string "operation failure" separately? Why don't you just write this:
/ / Delete resource const result = deleteResource (); if (! result) {alert ("operation failed");}
It seems that the code has become more concise, but in fact this is a common programming misunderstanding, the problem of magic value.
Magic value
What is the magic value? Sounds a bit like the blue slot MP of the game. Haha.
In fact, mana has nothing to do with MP!
Magic values refer to values that are undefined in the code and appear directly out of thin air like magic. They can be numbers, strings, etc., such as:
/ / output console.log (1); / / pop-up warning box alert ("dog")
Why give this value a name "magic value", because it has a very bad impact on the code!
The problem of magic value
First of all, magic values can seriously affect the readability and maintainability of the code.
Magic strings like the one above may seem fine, but if the magic value is a number, it can only be inferred by reading other code, such as:
If (a = = 1) {alert ("good");} else if (a = = 2) {alert ("bad");} else if (a = = 3) {.}
With such a piece of code, can you know what the numbers 1, 2 and 3 mean, respectively?
Some students said, can't I understand the code I wrote myself? Don't worry, you can read this code again in a month.
If you have a good memory, it doesn't matter if you pursue some efficiency when working on a project on your own. But if you work on a project and maintain code with other students, writing code with magic values will undoubtedly be difficult for others to understand, and if you don't write comments, they may even have the heart to kill you.
Second, the mana value will also affect the efficiency and accuracy of development.
Let's take the code at the beginning as an example. My classmate didn't finish reading the code file at all. In fact, I used the constant ERROR_MESSAGE more than once in this file:
/ / error prompt const ERROR_MESSAGE = "Operation failed"; / / Delete resource const result = deleteResource (); if (! result) {alert (ERROR_MESSAGE);} / / modify resource const result = updateResource (); if (! result) {alert (ERROR_MESSAGE);}.
If, as he said, instead of defining constants, I would use magic strings directly, I would type these words repeatedly every time I want to pop up "operation failure", wasting time and risking mistyping at the same time. If you use predefined constants, you can easily take advantage of the code hints and completion capabilities provided by development tools.
Code completion
In addition, the mana value also affects the ease of modification of the code.
If the same magic string appears many times in the code, then when I want to modify the copy of the string, I have to find the string one by one to modify it, even if I can use the search and global replacement functions provided by the development tool. but it's very troublesome to check it again.
Alert ("your operation failed"); alert ("your operation failed"); alert ("your operation failed")
If you define a string as a constant, you only need to change its value at the definition. At this time, the constant name is like a pointer to the magic value.
Const ERROR_MESSAGE = "your operation failed" alert ("ERROR_MESSAGE"); resolve mana value
Precisely because the magic value is very harmful to the code, developers are advised not to use the magic value in all kinds of code specifications.
The way to solve the magic value is very simple, in fact, as mentioned above, is to define a constant for a series of the same values. Note that it is a constant, not a variable! Because the content of the mana value is generally fixed, its own structure will not be modified.
For a series of magic values, it is recommended to define them as an enumeration or a separate constant class. For example, resources have many audit states, and all states can be defined centrally.
JavaScript Code:
Const REVIEW_STATUS = {/ / to be audited WAITING: 0, / pass PASS: 1, / / reject REJECT: 2}
Java Code:
Public enum ReviewStatus {WAITING, PASS, REJECT} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.