当前位置:网站首页>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
边栏推荐
- MySQL enterprise parameter tuning practice sharing
- Using two stacks to implement queues [two first in first out is first in first out]
- [AI application] detailed parameters of NVIDIA Tesla v100s-pcie-32gb
- [PCL self study: pclplotter] pclplotter draws data analysis chart
- golang使用mongo-driver操作——查(数组相关)
- Golang uses Mongo driver operation - query (Advanced)
- ASP.NET仓库进销存ERP管理系统源码 ERP小程序源码
- 炼金术(8): 开发和发布的并行
- 内网IP和公网IP的区别及作用
- [paper reading | deep reading] sdne:structural deep network embedding
猜你喜欢

翻译(5): 技术债务墻:一种让技术债务可见并可协商的方法

Transmitting and receiving antenna pattern

本地可视化工具连接阿里云centOS服务器的redis

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

Local visualization tool connects to redis of Alibaba cloud CentOS server

flutter系列之:flutter中的变形金刚Transform

Zero foundation self-study SQL course | complete collection of date functions in SQL

logging日志的使用

Flutter series: Transformers in flutter

MySQL enterprise parameter tuning practice sharing
随机推荐
Thread pool implementation: semaphores can also be understood as small waiting queues
认识微信小程序项目的基本组成结构
免费、好用、强大的开源笔记软件综合评测
apipost脚本使用讲解一~全局变量
2022 PMP project management examination agile knowledge points (3)
Systematic learning + active exploration is the most comfortable way to get started!
往前一步是优秀,退后一步是懵懂
Golang uses Mongo driver operation -- Query (array related)
炼金术(3): 怎样做好1个业务流程的接口对接
Grab those duplicate genes
Zero foundation self-study SQL course | complete collection of SQL basic functions
表单form 和 表单元素(input、select、textarea等)
Mysql database tourism management system_ Jsp+mysql tourism management system based on SSM [easy to understand]
[AI application] detailed parameters of NVIDIA Tesla v100-pcie-32gb
At the beginning of reading English literature, I would like to ask you how you should read it in the first place?
互联网业衍生出来了新的技术,新的模式,新的产业类型
Is the securities registration account safe? Is there any risk?
C language - date formatting [easy to understand]
[读书摘要] 学校的英文阅读教学错在哪里?--经验主义和认知科学的PK
Sécurité, économie de carburant et protection de l'environnement chameau
