当前位置:网站首页>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 .
边栏推荐
- Monkey test
- What is 5g industrial wireless gateway? What functions can 5g industrial wireless gateway achieve?
- 树莓派GPIO引脚控制红绿灯与轰鸣器
- 向数据库中存入数组数据,代码出错怎么解决
- [JS -- map string]
- Hand tear - sort
- Homework of the 16th week
- How muddy is the water in the medical beauty industry with a market scale of 100 billion?
- [C language] Dynamic Planning --- from entry to standing up
- Introduction to JSON usage scenarios and precautions
猜你喜欢

MySQL advanced SQL statement 2

How to model noise data? Hong Kong Baptist University's latest review paper on "label noise representation learning" comprehensively expounds the data, objective function and optimization strategy of

【leetcode】34. Find the first and last positions of elements in a sorted array

Lei Jun wrote a blog when he was a programmer. It's awesome

WiFi 5GHz frequency
![[untitled]](/img/53/cb61622cfcc73a347d2d5e852a5421.jpg)
[untitled]

Opencv learning example code 3.2.4 LUT

C language: examples of logical operation and judgment selection structure

2022-07-01: at the annual meeting of a company, everyone is going to play a game of giving bonuses. There are a total of N employees. Each employee has construction points and trouble points. They nee
![[C language] basic learning notes](/img/d2/1aeb2d37d97b9cfe4b21aa3ac37645.png)
[C language] basic learning notes
随机推荐
[untitled]
Wechat applet pull-down loading more waterfall flow loading
JVM knowledge points
Pandora IOT development board learning (RT thread) - Experiment 1 LED flashing experiment (learning notes)
JVM知识点
Www 2022 | rethinking the knowledge map completion of graph convolution network
Okcc why is cloud call center better than traditional call center?
Opencv learning example code 3.2.4 LUT
BiShe cinema ticket purchasing system based on SSM
[C language] Dynamic Planning --- from entry to standing up
Analysis of the overall design principle of Nacos configuration center (persistence, clustering, information synchronization)
There is no prompt for SQL in idea XML, and the dialect setting is useless.
MySQL advanced SQL statement 2
Yyds dry goods inventory kubernetes introduction foundation pod concept and related operations
LCM of Spreadtrum platform rotates 180 °
如何解决在editor模式下 无法删除物体的问题
【c语言】动态规划---入门到起立
Feature Engineering: summary of common feature transformation methods
Monkey test
Message mechanism -- message processing
