当前位置:网站首页>Read "the way to clean code" - function names should express their behavior
Read "the way to clean code" - function names should express their behavior
2022-07-02 04:17:00 【Mingmingruyue senior】
One 、 background
Some time ago, I heard that some people around me thought :“ It doesn't matter how confused the function name is , Anyway, when others use the interface, they will also look at the source code ”, It surprised me very much .
I always thought , Good code should know the meaning when you see the function name , When you see the parameters and return values, you should be able to use them safely , Instead of having to read the source code to avoid misuse .
Reread today 《 Clean code 》, I see it in the book : The function name should express its behavior .
If you have to look at the implementation of the function ( Or document ) Just know what it does , You should change a better function name, or redesign the function code, or give a more appropriate name .( The translation of the original text is a little stiff , Here I changed it myself )

Two 、 Example
For example, just want to query the user's name 、 Age 、 cell-phone number , But the function is defined as follows :
UserDO getUserSimpleById(Long id)
Corresponding SQL The statement is as follows :
<select id="getUserSimpleById" parameterType="java.lang.Long" resultMapping="baseResultMapping">
SELECT name,age,phone FROM user WHERE id = #{
id}
</select>
Because of the return value UserDO Contains many fields , Call this function to get UserDO Then don't look at the corresponding SQL Statement is difficult to determine which attributes are assigned .
But change the function name to getUserNameAgePhoneById It seems very wordy again , What if you check a few more attributes ?
UserDO getUserNameAgePhoneById(Long id)
You can define a field whose return value contains only the query , This is not easy to use wrong :
UserSimpleDO getSimpleById(Long id)
3、 ... and 、 summary
The function name is inconsistent with what you do , It's easy to misunderstand users .
When we define a function signature , Include function name , Parameters and return values should be carefully considered , Don't be careless .
It's not easy to create , If this article helps you , Welcome to thumb up 、 Collection and attention , Your support and encouragement , It's the biggest driving force of my creation .
边栏推荐
- MySQL advanced SQL statement 2
- Delete the code you wrote? Sentenced to 10 months!
- 如何解决在editor模式下 无法删除物体的问题
- 66.qt quick-qml自定义日历组件(支持竖屏和横屏)
- 【提高课】ST表解决区间最值问题【2】
- Dare to go out for an interview without learning some distributed technology?
- SQL:常用的 SQL 命令
- pip 安装第三方库
- Pytorch---使用Pytorch进行图像定位
- 【毕业季·进击的技术er】年少有梦,何惧彷徨
猜你喜欢

go 包的使用

Actual combat | use composite material 3 in application

JVM知识点

CorelDRAW Graphics Suite2022免费图形设计软件

阿里云polkit pkexec 本地提权漏洞

Use of go package

文档声明与字符编码

A summary of common interview questions in 2022, including 25 technology stacks, has helped me successfully get an offer from Tencent

C language: examples of logical operation and judgment selection structure

Pytorch---使用Pytorch进行鸟类的预测
随机推荐
Use of go package
Okcc why is cloud call center better than traditional call center?
5g era is coming in an all-round way, talking about the past and present life of mobile communication
Demonstration description of integrated base scheme
LxC limits the number of CPUs
[C language] Dynamic Planning --- from entry to standing up
pip 安装第三方库
66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
Which product of anti-cancer insurance is better?
Déchirure à la main - tri
okcc为什么云呼叫中心比传统呼叫中心更好?
How to solve the problem that objects cannot be deleted in Editor Mode
[improvement class] st table to solve the interval maximum value problem [2]
Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
Use a mask to restrict the input of the qlineedit control
千亿市场规模医疗美容行业的水究竟有多浑?
Finally got byte offer. The 25-year-old inexperienced perception of software testing is written to you who are still confused
《动手学深度学习》(二)-- 多层感知机
Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
go 函数
