How to improve the quality of your code

The capabilities of people vary widely, so the quality of the code written must be different. Some people may need 100 lines to write a small logic, while others may only need 10 lines. There will always be bugs in the code, and there is no best but better in this regard. Modularity and object orientation are ways to achieve efficient error-free code. Efficient, error-free code requires constant iteration of thought and practice. How to make the code efficient and error-free, and what are the ways to improve the quality of the code? What experience and skills do you have?

1. Code quality
 
Software is a product delivered to users and experienced by users; code is a correct and detailed description of the software, so the quality of the code is related to the quality of the software product. Although software quality is not equal to code quality, defects in code can seriously affect the quality of software products. Therefore, the investment in improving code quality is worthwhile.
 
 
Second, the quality of software products can usually be measured from the following six aspects
 
 
Functionality, that is, whether the software meets the customer's business requirements;
 
Usability, which is a measure of how much effort a user needs to make to use the software;
 
Reliability, that is, whether the software can always be in a stable state to meet availability;
 
Efficiency, which is to measure how much physical resources the software needs to run normally;
 
Maintainability, which is a measure of how much effort is required to make adjustments to already-completed software;
 
Portability, which is a measure of whether the software can be easily deployed to different operating environments;
 
 
3. Specific experience in improving code quality
 
 
1. Never copy code
 
Avoid duplicate code at any cost. If a commonly used piece of code appears in several different places in the program, refactor it to put it in a function of its own. Duplicate code can cause confusion among your colleagues when reading your code. If the duplicate code is modified in one place and forgotten to be modified in another place, there will be bugs everywhere, and it will also make your code bloated.
 
2. Test your finished code
 
You know what your code can do, and you try it, and it works, but you actually need to validate it well. Analyze all possible edge cases and test that it works as expected under all possible conditions. If there are parameters, pass some value outside the expected range. Pass a null value. If possible, show colleagues your code and ask if they can break it. Unit testing is the usual way to do this.
 
3. Code Review
 
Before committing your code, sit down with a colleague and explain to him what changes you made. Often, by doing this, you'll be able to spot bugs in your code without needing a word from a colleague. This is much more effective than reviewing your own code yourself.
 
4. 编写不言自明的代码
 
勿庸置疑,注释是编程中很重要的一部分,但能够不言自明的代码跟胜一筹,因为它能让你在看代码时就能理解它。函数名变量名要慎重选择,好的变量/方法名字放到语言语义环境中时,不懂编程的人都能看懂。
 
5. 不要使用纯数字
 
直接把数字嵌入代码中是一种恶习,因为无法说明它们是代表什么的。当有重复时更糟糕——相同的数字在代码的多个地方出现。如果只修改了一个,而忘记了其它的。这就导致bug。一定要用一个命名常量来代表你要表达的数字,即使它在代码里只出现一次。
 
6. 不要做手工劳动
 
当做一系列动作时,人类总是喜欢犯错误。如果你在做部署工作,并且不是一步能完成的,那你就是在做错事。尽量的让工作能自动化的完成,减少人为错误。当做工作量很大的任务时,这尤其重要。
 
 
7、不要试图死磕代码加快速度,找个更加有效的算法可能更加有效。
 
8、代码要先做对,在弄快。先使其可靠,再让其更快。先把代码弄干净,再让它变快
 
9、当发现一个函数具有以下特征时,需要考虑抽取函数
 
(1)、过长
 
(2)、嵌套层数过深。
 
(3)、自然分块,需要使用注释描述该程序块
 
(4)、判断条件过于复杂
 
(5)、函数的某些判断分支不断变化
 
(6)、参数过于复杂
 
(7)、逻辑重复
 
10、局部变量应当用途单一
 
11、程序员应当将整洁的代码风格作为一种习惯,时刻意识到整洁代码的重要性并不断地提高重构技巧
 
12、关于注释
 
(1)、如果能用短小函数描述,则使用子函数替代注释本身。
 
(2)、确保注释和代码表达的意图一致,否则就失去了注释的意义。
 
(3)、在重要的地方写注释,不要注释满天飞,简单的重复代码的功能是毫无意义的。要让每一处注释都有价值。不要过分注释。
 
 
13、关于何时重写代码
 
开发团队要预留20% 的时间用作保持对原有系统的重构。剩余的时间用作开发新功能。
 
只要有可能,所要重构的部分进行递增修改,让用户切身感受到产品的改进,哪怕将工作时间延长。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326309560&siteId=291194637
Recommended