当前位置:网站首页>JS naming conventions
JS naming conventions
2022-06-23 20:39:00 【It workers】
Cache invalidity aside , It's really difficult , Whenever I can't find the right name , This infamous quote will haunt my mind .
When someone needs to understand code quickly , Clear naming provides important context , Whether they are coding 、 Debugging or assisting teammates —— I don't need to ask others what users mean , But I have to ask the meaning of the data .
Although I don't often find the best name , But I try to optimize my code by following some basic rules .
Use meaningful prefixes
Although these prefixes are not universal , But they are good for building a shared language in your team . Using them consistently in your code base can make reading easier .
- Function name prefix get、 find、 fetch Used to represent that the function returns a value
- Function name prefix set、update Used to represent that the function will be used to update
- on、handle Used for event handlers
- is、should、can Representing a value or function will be a boolean type
Any conventions that become standard in the team help improve readability . Please make sure that in the project Readme File or wiki Record these in .
Use meaningful words
for example , Developers usually name variables... By default data, But let's examine a few of its definitions :
- As reasoning 、 Discuss or calculate the basic factual information ( Such as measurement or statistics )
- Information in digital form that can be transmitted or processed
These definitions can refer to any variable we deal with , So they don't give the reader any information . Let's look at an example of not following this rule :
function total(data) {
let total = 0;
for (let i = 0; i < data.length; i++) {
total += data[i].value;
}
return total;
}We know that this function calculates something , But we're not sure what it is .
Use complete words
When it comes to variable naming , Shortcuts usually mean abbreviations or single character variable names .
Like the case code above , Let's look at a function that doesn't follow the rules :
function totBal(accts) {
let tot = 0;
for (let i = 0; i < accts.length; i++) {
tot += accts[i].bal;
}
return tot;
}We can probably guess accts yes account, tot yes total, But we can't see the meaning of the code at a glance .
Do not use “ It's useless ” The word
container and wrapper It makes sense only when it is related to what they contain . The problem is , Not every component of a basic element contains another component .
You will also get bogged down in naming components MyComponentContainerContainer Awkward situation .wrapper So it is with .
Pay attention to spelling : )
Misspelling words can cause bug, Make searching code harder . Spelling mistakes are easily overlooked , But if everything in the code base is spelled correctly , It makes a huge difference , Especially when trying to find globally / When replacing .
Right case
Apply all rules at the same time , We get the following function :
function getAccountsTotalBalance(accounts) {
let totalBalance = 0;
for (let accountIndex = 0; accountIndex < accounts.length; accountIndex++) {
totalBalance += accounts[accountIndex].balance;
}
return totalBalance;
}although accountIndex And i There may be controversy , But the rest of the function should be clearer .getAccountsTotalBalance Completely conveys the intent of the function , Prefix get It does not cause any mutation . For the benefit of readers , It is worthwhile for the code author to devote more energy .6 After a month , When they maintain code , Your future self will appreciate the extra work they do .
If you are worried about the line length , Consider using Prettier Tools like to automatically format code .
summary
The goal of these rules is to make the code we write for future readers as meaningful as possible . Find rules that fit your context , If a rule does more harm than good , Just change or discard it . Coding your team's rules will help you communicate your ideas on this issue , Instead of hitting your colleagues .
边栏推荐
- 测试的重要性及目的
- 【Golang】快速复习指南QuickReview(二)——切片slice
- 5 月最大的 GameFi 崩溃受害者能否在熊市中生存?| May Monthly Report
- What is the main content of short video audit? What is illegal?
- Zabbix监控- Aruba AP运行数据
- The "open source star picking program" container pulls private images from harbor, which is a necessary skill for cloud native advanced technology
- [golang] quick review guide quickreview (IV) -- functions
- LeetCode 473. Match to square
- How to make a commodity price tag
- 【Golang】快速复习指南QuickReview(四)——函数
猜你喜欢

小程序开发框架推荐

Interpreting the 2022 agile coaching industry status report

35歲危機?內卷成程序員代名詞了…

LeetCode 260. Number III that appears only once

Leaders of Hangcheng street, Bao'an District and their delegation visited lianchengfa for investigation

Crise de 35 ans? Le volume intérieur est devenu synonyme de programmeur...

GL Studio 5 安装与体验

SQL联合查询(内联、左联、右联、全联)的语法

Syntax of SQL union query (inline, left, right, and full)

Applet development framework recommendation
随机推荐
怎么开户?在国海证券开户安全吗?需要带什么?
Yaokui tower in Fengjie, Chongqing, after its completion, will be the safety tower for Sichuan river shipping with five local scholars in the company
How to deal with product pictures? How to select mapping software?
What is the role of computer auto audit audio? What content failed to pass the audit?
ntpupdate. tencentyun. Com has been eliminated
Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?
How to log in to the server through the fortress machine to transfer files? What are the specific steps?
Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
[sap-hcm] report jump to pa30/pa40 instance
LeetCode 473. Match to square
More than 1200 phishing kits that can intercept 2fa detected in the field
教你如何用网页开发APP
手续费佣金低的券商,华泰证券网上开户安全吗
SQL聯合查詢(內聯、左聯、右聯、全聯)的語法
TCP/UDP基本原理
After the collapse of UST, will the stable currency market pattern usher in new opportunities?
国元期货交易软件正规吗?如何安全下载?
Command line add user set password never expires add remote group add administrator group
JS advanced programming version 4: generator learning
Are internal consultants and external consultants in SAP implementation projects difficult or successful?