当前位置:网站首页>High quality subroutine 3 - a good name
High quality subroutine 3 - a good name
2022-07-28 23:17:00 【Fluttering moth】
1、 How to choose a name that can describe the subroutine at the appropriate level of abstraction ?
Describe everything the subroutine does , The name of the subroutine should describe all its output results and side effects .
If the function of a subroutine is to calculate the total amount of the report and open an output file , Then name it ComputeReportTotals() It's not complete .ComputeReportTotalsAndOpenOutputFile() It's complete , But it's too long and seems a little silly if you write a subroutine with some side effects , That will give rise to many long and stupid names .
The solution is not to use a less descriptive subroutine name , Instead, you should write the program in another way , Directly solve the problem locally without side effects .
Avoid using meaningless 、 A vague or ambiguous verb , Some verbs have very flexible meanings , It can be extended to cover almost any meaning .
Don't just use numbers to form different subroutine names , A programmer wrote all the code as a large function , Then create a function for each line of code , And name them respectively partl、Part2 etc. . After that , He also created a high-level function to call these parts Partn() . This method of creating subroutines and naming subroutines is really shocking ( I really hope this seldom happens ). But programmers sometimes use numbers to distinguish things like OutputUser、OutputUser1 and OutputUser2 Such subroutines .
The numbers after these names do not show how the abstractions represented by the subroutines are different , So the names of these subroutines are also very bad .
Use antithetical words accurately , When naming, following the naming rules of antithetical words helps to maintain consistency , Thus improving readability , image first/last Such antithetical phrases are easy to understand .
Here are some common antithetical phrases :
add/remove increment/decrement open/close begin/end insert/delete show/hide create/destroy lock/unlock source/target first/last mini/max start/stop get/put next/previous up/down get/set old/new
Establish naming rules for common operations , In some systems , It is important to distinguish between different categories of operations . Naming rules are often the simplest and most reliable way to indicate this difference . such as , In the code of a project , Each object is assigned a unique identifier . We need to establish a naming rule for subroutines that return such object identifiers .
otherwise , In the middle of the project , Everyone in the project team has to spend unnecessary energy , Remember the syntax details used on each object to obtain a unique identifier . These problems can be avoided by establishing naming rules to obtain unique identification .
2、 The reason why the subroutine name is bad ?
Sometimes the only problem in a subroutine is its The name is unclear , The subroutine itself may be well designed , But if its name is changed from Handleoutput() Change it to FormatAndPrintOutput() , Then you can easily see the function of this subroutine .
There are other situations , The reason why the verbs are vague , It is due to the execution of subroutines The operation is ambiguous . The problem with this subroutine is that the purpose is not clear , And its vague name is only a kind of appearance . If that's the case , Then the best solution is to reorganize the subroutine and any related subroutines , So that they all have a clearer purpose , And then give it a clearer name that can accurately describe these purposes .
3、 How long can the name of a subroutine be written ?
The optimal length of a variable name is 9 To 7 Characters . Studies have shown that , Subroutines are usually more complex than variables , therefore , Good subroutines usually have longer names .
On the other hand , The subroutine name is usually followed by the object name , This actually provides some names for free . in general , The key point of naming subroutines is to be as clear as possible , in other words , The length of the subroutine name depends on whether the name is clear and understandable .
4、 The name of the function that has the return value
When naming a function, you should describe the return value , therefore , The function should be named for its return value . for instance ,customerId.next()、printer.isReady() and pen.currentColor() Are good function names , They accurately express the result that the function will return .
5、 The name of the function that has no return value
When naming a process, use a strong verb plus an object . A process with functional cohesion usually performs an operation on an object . The name of the process should reflect what the process does , An operation performed on an object requires a verb + The name of the object form .
Such as printDocument()、checkOrderInfo() etc. , They are all very good process names .
In object-oriented languages , You don't need to add the name of the object to the process name ( The object ), Because the object itself has been included in the call statement . You can use document.print()、orderlnfo.check() Wait for the statement to call the subroutine .
And things like document.printDocument() Such statements are too bloated , And it is easy to misunderstand when they are called in derived classes . If Check( Check ) Class is from Document( file ) Class , that check.print() It obviously means printing a check , and check.printDocument() It looks like printing a checkbook or a credit card statement , It's not like printing the check itself .
边栏推荐
- Learning experience sharing 3: yolov5 training data set path index
- 18 diagrams, intuitive understanding of neural networks, manifolds and topologies
- [database]
- [copy] Internet terms, abbreviations, abbreviations
- Will Qualcomm and MediaTek chips soon be sold, and will they surpass Huawei to become the first in China?
- 【数据库】
- Learning experience sharing 4: learning experience of yolov7
- Sqlilabs-1 (breakthrough record)
- Thesis reading (2) - vggnet of classification
- Kotlin JVM annotation
猜你喜欢
Target detection notes fast r-cnn
【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
Thesis reading (3) - googlenet of classification
Introduction to address book export without code development platform
A new paradigm of distributed deep learning programming: Global tensor
Applet Download Preview PDF, document cannot open solution
A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network
[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code
NPM run dev, automatically open the browser after running the project
Sqlilabs-3 (entry notes)
随机推荐
frontiers出版社投稿记录(附状态变化)
软件测试面试笔试题及答案(软件测试题库)
即将获售高通、联发科芯片,荣耀要超越华为做国内第一?
Kotlin function nesting
弹框遮罩层「建议收藏」
Shell script foundation - shell operation principle + variable and array definitions
[C language] implementation of three piece chess games
Applet, JS, transfer object jump transfer parameter problem
Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
The simple neural network model based on full connection layer MLP is changed to the model based on CNN convolutional neural network
【物理应用】水下浮动风力涡轮机的尾流诱导动态模拟风场附matlab代码
定了!哪吒S全系产品将于7月31日上市发售
Console.log() console display... Solution
[physical application] atmospheric absorption loss with matlab code
MySQL数据库的基本概念以及MySQL8.0版本的部署(一)
Advanced C language: pointer (2)
希捷发布全新RISC-V架构处理器:机械硬盘相关性能暴涨3倍
Leetcode 199. right view of binary tree
WebView whitelist
无代码开发平台管理后台入门教程