当前位置:网站首页>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 .
边栏推荐
- Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
- Go language naming specification
- Wpviewpdf Delphi and Net PDF viewing component
- Demonstration description of integrated base scheme
- Pytorch---使用Pytorch实现U-Net进行语义分割
- 手撕——排序
- The original author is out! Faker. JS has been controlled by the community..
- Play with concurrency: draw a thread state transition diagram
- Which product of anti-cancer insurance is better?
- cookie、session、tooken
猜你喜欢
First acquaintance with P4 language
go 包的使用
Sorted out an ECS summer money saving secret, this time @ old users come and take it away
PR zero foundation introductory guide note 2
藍湖的安裝及使用
Déchirure à la main - tri
MySQL advanced SQL statement 2
阿里云polkit pkexec 本地提权漏洞
How much is the tuition fee of SCM training class? How long is the study time?
[untitled]
随机推荐
Pytoch --- use pytoch to predict birds
C语言猜数字游戏
Which insurance company has a better product of anti-cancer insurance?
C language: examples of logical operation and judgment selection structure
Suggestions on settlement solution of u standard contract position explosion
Play with concurrency: draw a thread state transition diagram
Yolov5网络修改教程(将backbone修改为EfficientNet、MobileNet3、RegNet等)
蓝湖的安装及使用
How much can a job hopping increase? Today, I saw the ceiling of job hopping.
Recyclerview add header
Yyds dry goods inventory kubernetes introduction foundation pod concept and related operations
阿里云polkit pkexec 本地提权漏洞
Pytoch --- use pytoch to realize u-net semantic segmentation
【提高课】ST表解决区间最值问题【2】
Pytorch---使用Pytorch实现U-Net进行语义分割
QT designer plug-in implementation of QT plug-in
Uni app - realize the countdown of 60 seconds to obtain the mobile verification code (mobile number + verification code login function)
Federal learning: dividing non IID samples according to Dirichlet distribution
【leetcode】34. Find the first and last positions of elements in a sorted array
First acquaintance with string+ simple usage (II)