当前位置:网站首页>命名规范/注释规范/逻辑规范
命名规范/注释规范/逻辑规范
2022-06-12 10:23:00 【乔朵朵】
为了提高代码的可读性,以及后续其他人员方便查找、修改代码,可以遵循以下规范:
1.命名规范:
(1)id/class采用中划线命名法,例如article-title、article-content;变量命名采用驼峰命名法例如articleList、articleInfo
(2)采用英文命名而不用中文命名,例如页面头部应该命名为header,而不是toubu
(3)尽量不缩写,除非是一看就明白的单词,例如nav
(4)变量的命名应使用易理解的单词或词组,避免使用data、value等泛指的单词,使用单数不使用复数。例如使用articleInfo、articelList不使用articlesList、articleLists
(5)为了避免class命名的重复,命名时一般取父元素的class名作为前缀,例如:
<div class="column>
<h3 class="column-title></h3>
<div class="column-content></div>
</div>
(6)为了避免歧义,变量命名时尽量使用单词组合的形式。例如:用provinceCode表示省份的code,用provinceName表示省份的名称,若是只使用province,则容易让人迷惑该变量存放的数据到底是什么。一看就明白的单词可以不用非得使用单词组合,例如phone一般表示电话号码。
(7)方法的命名:动词 + 名词,例如getArticleList表示获取文章列表、submitArticleInfo表示提交文章信息、goAddPage表示跳转到新增页面、showDetailModal/closeAddModal表示展示/关闭详情的弹窗
2.注释规范
(1)模块注释:在模块的开始或结尾添加注释说明,说明加上“start”、“end”,方便定位。例如:
<!--导航start-->
...
<!--导航end-->
/*文章列表样式start*/
...
/*文章列表样式end*/
3.逻辑规范
使用if分支语句时,为了避免过度代码嵌入(不断的向右缩进),采用负逻辑判定的写法,并处理异常;主逻辑应为正逻辑。代码示例如下:
if(不能继续执行逻辑的判定表达式) {
/*错误提示或抛出异常*/
return
}
正确的业务逻辑继续执行
边栏推荐
- PHP wechat red packet allocation logic
- 3. Abstract Factory
- reflex
- Valentina Studio Pro for MAC (MAC database management software)
- CONDA install tensorflow test tensorflow
- Remote link software xshell and xftp download address
- Class. Forname connection MySQL driver keeps throwing classnotfoundexception exception solution
- [MySQL] index invalidation and index optimization
- PHP get (remote) large file method record
- Several methods of importing ThinkPHP
猜你喜欢

机器学习不是你想用,想用就能用

【实验】MySQL主从复制及读写分离

Circuitbreaker fuse of resilience4j -- Measurement of circuitbreakermetrics index

Chapter 3 search

Leetcode 2169. 得到 0 的操作数

Machine learning is not something you can use if you want to use it

Amélioration de la 3dsc par HSC
![[MySQL] index invalidation and index optimization](/img/af/9ed540aab764f4962ccc9bbfe406f3.jpg)
[MySQL] index invalidation and index optimization

A snare - Cookie spoofing

Is the acceptance standard a test case?
随机推荐
Circuitbreaker fuse of resilience4j - Summary
2022淘宝618超级喵运会怎么玩?2022淘宝618喵运会玩法技巧
Tp6+memcached configuration
Leetcode2154. Multiply the found value by 2 (binary search)
reflex
Unable to load dynamic library ‘oci8_ 12C 'or unable to load dynamic library' PDO_ OCI 'or cannot find module
PHP wechat red packet allocation logic
Halcon combined with C # to detect surface defects -- affine transformation (III)
Reverse analysis of Huawei housekeeper software [transfer]
PHP download station B video
[CEGUI] resource loading process
XML Parsing Error: mismatched tag. Expected
Set SVG color
Timers in golang
How Qualcomm platform modifies special voltage
See if you fall into the trap of "labeling" customers and users?
Circuitbreaker fuse of resilience4j - circuitbreakerevent event
Dynamic proxy
MySQL user and permission management, role management
Cookie object