你们觉得一份代码,注释和代码的比例应该是怎么样的一个范围空间比较合适,可以是字节角度,也可以是行数角度?
回复内容:
国标规定,VC工程注释率为25%~50%,关键代码注释率为50%;不满足要求就无法通过软件三方测评;
在满足这个要求的前提下再谈哪些需要注释。
为了增加代码注释率,减少三方测评前“脑补”注释的痛苦,
我们一般要求把《软件设计文档》中的"关键算法”、“接口数据定义”等内容直接拷贝到代码前面;
修改代码时也顺便把”关键算法“、“接口数据定义”等的注释也修改了;
修编《软件设计文档》时再用注释中的”关键算法“、“接口数据定义”等内容替换文档中的相关内容;
通过这种办法来保证文档和代码的一致性,减少软件维护与人员更换时痛苦…… 我的观点,只写说明性注释,不写功能性注释。也就是说,注释Why,而不是How和What。
类和函数多写文档注释,多少行无所谓,写在最前面,只要你是注释的Why。
函数内部,尽量少写注释。如果你的代码需要写注释来说明他的功能,那么这段代码就需要重构,最简单的方法,最简单的方法:提取函数。这样的好处是,函数名就是注释。一个错误的观点就是 注释是给人看的,程序是给电脑看的。其实,程序是给人的,凑巧的是,它居然可以在电脑里运行。
《重构:改善既有代码的设计》一书写道:
每当感觉需要以注释来说明点什么的时候,我们就把需要说明的东西写进一个独立函数中,并以其用途(而非实现手法)命名。每次我给别人讲解「选择排序」、「插入排序时」,他们都觉得太难了,而且几乎每本数据结构教科书都是写了一堆代码和注释,这丝毫没有降低这个算法的难度。
如果不写注释,而写成函数呢?
伪代码:
array_ordered = []
loop_all_element(array, function(i){
el = select(array[i+1, array.length])
push(array_ordered, el)
......
})
- 构建一个有序数组,初始为空
- 循环整个数组,进行如下操作:
- 从数组剩下的元素里面选择最小的(或最大的)
- 将最小元素放在有序数组的最后面(或者最前面)
插入排序呢?大同小异,我就不详细写了。
所以,文档注释,多少无所谓。函数内、类内注释,能不写,就不写。 我是这样看的,要看你写的代码的作用是什么。
1.简单代码或脚本,遵循命名规范,可以不写注释,文件开始写明作用即可。关键部分写简单注释。
2.由多个文件组成的复杂项目结构,文件开始的说明最好由工具生成,类方法的输入输出要标明。
3.如果是开源项目,那情况又完全不一样了,注释简直是越多越好,“为什么这么做”也越多越好。
以上是我的理解,还请各位指正 PHP本身,并不需要什么注释
变量命名规范一点就行。
最多给每个方法给一段说明。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
