当前位置:网站首页>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 .
边栏推荐
- Bullet frame mask layer "recommended collection"
- Kotlin function nesting
- WebView whitelist
- 6 open source tutorials of super conscience!
- 软件测试工具fiddler postman jmeter charlse核心功能总结
- High quality subroutine 1
- MySQL foundation - data query
- [filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code
- 投资500亿元!中芯京城正式注册成立!
- Yolov5 improvement 15: network lightweight method deep separable convolution
猜你喜欢

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](/img/e5/878bf5908856c80d6e07b03ba59da5.png)
[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code

一种分布式深度学习编程新范式:Global Tensor

6 个超级良心的开源教程!

6 open source tutorials of super conscience!

c语言进阶篇:指针(二)

Xshell7, xftp7 personal free version official download, no need to crack, no activation, download and use

WebView optimization

can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to host memory

Sdwebimage source code combs 5 author motivation, modifies directory, and changes inheritance relationship
随机推荐
[C language] implementation of three piece chess games
Sqlilabs-2 (breakthrough record)
Improvement 17 of yolov5: cnn+transformer -- integrating bottleneck transformers
There are four ways for Nacos to configure hot updates and multiple ways to read project configuration files, @value, @refreshscope, @nacosconfigurationproperties
View APK signature
CGLIb 创建代理
Pgbench benchmark PostgreSQL
Kotlin function nesting
Summary of common formula notes for solving problems in Higher Mathematics
Record a question about the order of trigonometric function exchange integrals
RuntimeError: set_ sizes_ contiguous is not allowed on a Tensor created from .data or .detach().
The simple neural network model based on full connection layer MLP is changed to the model based on CNN convolutional neural network
赋能中国芯创业者!看摩尔精英如何破解中小芯片企业发展难题
c语言进阶篇:指针(三)
2020年国内十大IC设计企业曝光!这五大产业挑战仍有待突破!
Hbuilderx shortcut key
Several common methods of SQL optimization
Learning experience sharing 5: yolov5 dataset division and Yolo format conversion
It's settled! All products of Nezha s will be launched on July 31
Is 1E3 a floating point number?