In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, I would like to share with you the relevant knowledge of how to write readable code in web development. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
Why should the code be easy to understand
"Code should be written to minimize the time it would take for someone else to understand it."
The facts of daily work are:
The time spent thinking and reading the code before writing the code is much longer than the time it takes to actually write it.
It is very common to read the code, whether it is someone else's or your own. What was written six months ago can be considered to be someone else's code.
The code is highly readable, and you can quickly understand the logic of the program and enter the working state.
Code with fewer lines is not necessarily easy to understand.
The readability of the code does not conflict with the efficiency, architecture and ease of testing of the program.
The whole book is written around the goal of "how to make code more readable". This is also one of the important standards for good code.
How to name the variable name should contain more information
To use words with clear meaning, such as download instead of get, refer to the following replacement scheme:
Send-> deliver, dispatch, announce, distribute, route find-> search, extract, locate, recoverstart-> lanuch, create, begin, open make-> create,set up, build, generate, compose, add, new avoid generic words
Words like tmp and retval have no meaning except that they are temporary variables and return values. But add some meaningful words to him, and it will be clear:
Tmp_file = tempfile.NamedTemporaryFile ()... SaveData (tmp_file,...)
What variables really mean instead of using retval:
Sum_squares + = v [I]; / / Where's the "square" that we're summing? Bug!
In nested for loops, I and j have equally confusing moments:
For (int I = 0; I
< clubs.size(); i++) for (int j = 0; j < clubs[i].members.size(); j++) for (int k = 0; k < users.size(); k++) if (clubs[i].members[k] == users[j]) cout unescaped_comment html ->Html_utf8 data-> data_urlenc uses longer names for scoped variables
In a relatively small scope, you can use shorter variable names, variables used in a larger scope, preferably with longer names, and the automatic complement of the editor can all reduce keyboard input. For some abbreviated prefixes, try to choose well-known ones (such as str). One criterion is whether you can understand what the prefix stands for without the help of others when new members join.
Make rational use of symbols such as _, -, and so on, such as prefixing private variables with _. Var x = new DatePicker (); / / DatePicker () is the "construction" function of the class, starting with uppercase var y = pageHeight (); / / pageHeight () is an ordinary function var $all_images = $("img"); / / $all_images is the jQuery object var height = 250; / / height is not the separation of / / id and class. Naming should not be ambiguous.
When naming it, you can first think about whether the word I'm going to use has any other meaning. For example:
Results = Database.all_objects.filter ("year
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.