当前位置:网站首页>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 .
边栏推荐
- Where can I buy cancer insurance? Which product is better?
- C language guessing numbers game
- 66.qt quick-qml自定义日历组件(支持竖屏和横屏)
- 如何解决在editor模式下 无法删除物体的问题
- LxC limits the number of CPUs
- Pandora IOT development board learning (HAL Library) - Experiment 2 buzzer experiment (learning notes)
- Go language naming specification
- 66.qt quick QML Custom Calendar component (supports vertical and horizontal screens)
- Fluent icon demo
- Pytoch yolov5 runs bug solution from 0:
猜你喜欢

Pytorch---使用Pytorch实现U-Net进行语义分割

go 包的使用

Go语言介绍

【c语言】基础篇学习笔记

BGP experiment the next day

树莓派GPIO引脚控制红绿灯与轰鸣器

Www 2022 | rethinking the knowledge map completion of graph convolution network

WiFi 5GHz frequency

Realizing deep learning framework from zero -- Introduction to neural network

Play with concurrency: draw a thread state transition diagram
随机推荐
office_ Delete the last page of word (the seemingly blank page)
【提高课】ST表解决区间最值问题【2】
5g era is coming in an all-round way, talking about the past and present life of mobile communication
Play with concurrency: draw a thread state transition diagram
FAQ | FAQ for building applications for large screen devices
Pytorch-Yolov5从0运行Bug解决:
文档声明与字符编码
How much is the tuition fee of SCM training class? How long is the study time?
Pandora IOT development board learning (HAL Library) - Experiment 2 buzzer experiment (learning notes)
Pytoch yolov5 runs bug solution from 0:
阿里云polkit pkexec 本地提权漏洞
There is no prompt for SQL in idea XML, and the dialect setting is useless.
Go language naming specification
Actual combat | use composite material 3 in application
Li Kou interview question 02.08 Loop detection
[live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
Pytorch---使用Pytorch进行图像定位
Www 2022 | rethinking the knowledge map completion of graph convolution network
Wechat applet map annotation
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
