当前位置:网站首页>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
边栏推荐
- 炼金术(3): 怎样做好1个业务流程的接口对接
- 零基础自学SQL课程 | CASE函数
- Thread pool implementation: semaphores can also be understood as small waiting queues
- [PCL self study: pclplotter] pclplotter draws data analysis chart
- VMware virtual machine bridging connectivity
- 自定义MySQL连接池
- request对象、response对象、session对象
- [PCL self study: segmentation4] point cloud segmentation based on Min cut
- mysql读写分离配置
- One step forward is excellent, one step backward is ignorant
猜你喜欢

【无标题】

Local visualization tool connects to redis of Alibaba cloud CentOS server

零基础自学SQL课程 | SQL中的日期函数大全

Sell notes | brief introduction to video text pre training

Zero foundation self-study SQL course | if function

现代编程语言:zig

MongoDB-在windows电脑本地安装一个mongodb的数据库

C language character pointer and string initialization
![[digital ic/fpga] detect the position of the last matching sequence](/img/67/a1b575aa9b63892ed585d39e615c58.png)
[digital ic/fpga] detect the position of the last matching sequence
![[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation](/img/9e/f08ce0729c89b0205c0ac47c523ad7.png)
[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation
随机推荐
After a period of silence, I came out again~
VMware virtual machine bridging connectivity
技术的极限(11): 有趣的编程
[PCL self study: pclplotter] pclplotter draws data analysis chart
flutter系列之:flutter中的变形金刚Transform
Scu| gait switching and target navigation of micro swimming robot through deep reinforcement learning
炼金术(3): 怎样做好1个业务流程的接口对接
互联网的发展为产业的变革和转型提供了新的解决方案
[idea] idea formatting code skills
Learning notes for qstringlist
表单form 和 表单元素(input、select、textarea等)
Zero foundation self-study SQL course | if function
RNA-seq入门实战(一):上游数据下载、格式转化和质控清洗
超纲练习题不超纲
十大券商注册账户安全吗,会有风险吗?
2022 PMP project management examination agile knowledge points (3)
Matlab基本函数 length函数
ValidateRequest=”false” 是做什么的「建议收藏」
[AI application] detailed parameters of NVIDIA geforce RTX 1080ti
Is not null and in SQL= Difference between null
