当前位置:网站首页>Code neatness -- function
Code neatness -- function
2022-06-28 00:13:00 【Hua Weiyun】
Function is probably one of the most important things we programmers face every day , But have you ever thought about , How to write a function well ?
One 、 Short
seeing the name of a thing one thinks of its function , Every function we write , It should be possible not to be too long , Don't write code into a function , Keep the function as short as possible ;
Two 、 Do one thing
A function requires a function to be completed , It is usually done in several steps , For example, write a function , The function is to put the elephant in the refrigerator ,
So this function needs three steps
1, Open the refrigerator ;
2, Put the elephant in ,
3, Close the refrigerator
Each of these three steps is one thing , Abstract into code , Each step should be encapsulated in a function , Three functions are needed to complete a function , Not a function ;
Like the code above , Is to satisfy that the function only does one thing , If you write every operation in a function , That's what it's like
Some of them may say , Isn't that the same , There is one more line of code above
Yes , This is a simple case , In actual development , Often '' Put the elephant in the fridge '', Each step has dozens of lines of code , If you don't use function encapsulation , It will greatly affect the code reading !
3、 ... and 、 Hierarchical abstraction of functions
A function , Sometimes, I will call multiple other functions to realize the functions , But in the process of calling other functions , We need to pay attention to , What level is the called function ,
For example , I have a function , After the user logs in , send data , And then quit , The code is as follows
The above code is the main flow of the function , Next you need to add details , For example, when sending data , You need to send the login time at the same time , But where to add details ?
This is the wrong way to modify , Details like getting time , It should not be placed in the top-level function module , It should be placed in setMessage() Get it by yourself in the method
Four 、 Reduce function parameters
The function should have as few arguments as possible , Functions with more than three arguments are not recommended , If three or more parameters are required , It is recommended to use objects to encapsulate , Or use the creator mode to simplify the parameters
5、 ... and 、 No side effect
In a function that mainly does query , Do not make other changes in this function , This is likely to cause unexpected consequences when called elsewhere
6、 ... and 、 Functions should be reused
Don't write the same function repeatedly , Functions should be reused many times
7、 ... and 、 Exception handling should be placed separately in the function
Not recommended in try It writes a lot of code , You can try to put the statements that may throw exceptions , Encapsulate it in a function , as follows
边栏推荐
- Build an open source and beautiful database monitoring system -lepus
- golang使用mongo-driver操作——查(基础)
- ASP.NET仓库进销存ERP管理系统源码 ERP小程序源码
- golang使用mongo-driver操作——查(进阶)
- 内网IP和公网IP的区别及作用
- Zero foundation self-study SQL course | case function
- 零基础自学SQL课程 | IF函数
- [AI application] detailed parameters of NVIDIA Tesla v100s-pcie-32gb
- Matlab基本函数 length函数
- 線程池實現:信號量也可以理解成小等待隊列
猜你喜欢
![[PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer](/img/38/c7ce908bfcc5cc5cd5856996aa015b.png)
[PCL self study: pclvisualizer] point cloud visualization tool pclvisualizer
![[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK](/img/7b/8b3619d7726fdaa58da46b0c8451a4.png)
[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK
![Count prime [enumeration - > space for time]](/img/11/c52e1dfce8e35307c848d12ccc6454.png)
Count prime [enumeration - > space for time]

吴恩达《机器学习》课程总结(14)_降维

炼金术(7): 何以解忧,唯有重构
Does the subscription of Siyuan notes stop deleting cloud data directly?

Sell notes | brief introduction to video text pre training

Msp430f5529 MCU reads gy-906 infrared temperature sensor

智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维

认识微信小程序项目的基本组成结构
随机推荐
代码整洁之道--函数
[idea] idea formatting code skills
零基础自学SQL课程 | CASE函数
吴恩达《机器学习》课程总结(11)_支持向量机
系统学习+主动探索,是最舒适的入门学习方式!
[microservices sentinel] sentinel data persistence
Customize MySQL connection pool
数仓的字符截取三胞胎:substrb、substr、substring
mysql数据库旅游管理系统_JSP+MySQL基于ssm的旅游管理系统[通俗易懂]
PAT乙级1013
Matlab基本函数 length函数
【论文阅读|深读】SDNE:Structural Deep Network Embedding
零基础自学SQL课程 | SQL基本函数大全
Is it safe to open a stock account through the account opening QR code of CICC securities manager? Or is it safe to open an account in a securities company?
Zero foundation self-study SQL course | complete collection of date functions in SQL
抓出那些重复的基因
request对象、response对象、session对象
RNA SEQ introduction practice (I): upstream data download, format conversion and quality control cleaning
互联网的发展为产业的变革和转型提供了新的解决方案
How to quote Chinese documents when writing a foreign language?
