当前位置:网站首页>High quality subroutine 1
High quality subroutine 1
2022-07-28 23:16:00 【Fluttering moth】
1、 What is a subroutine ?
Subroutine is a callable method or procedure written for a specific purpose .
2、 What are the benefits of using subroutines ?
The use of subroutines makes the program more readable , Easier to understand , Easier than any functional feature of any programming language .
Subroutines are also the most important means invented so far to save space and improve performance . Make changes in one place . The use of subroutines makes modern programming possible .
2.1、 Reduce complexity .
One of the most important reasons for creating subroutines , Is to reduce the complexity of the program . You can hide some information by creating subroutines , So you don't have to think about this information anymore . Of course , You must consider them when writing this subroutine . But once the program is written , You should be able to forget these details , The subroutine can be called directly without knowing its internal working details . There are other reasons for creating subroutines - such as reducing the size of the code 、 Improve maintainability 、 It is also very good to improve the correctness and so on , But without the abstract ability of subroutines , Our intelligence will not be able to manage complex programs at all .
When the nested level of internal loop or condition judgment is very deep , It means that a new subroutine needs to be extracted from the subroutine . Extract the nested parts to form an independent subroutine , It can reduce the complexity of the enclosing subprogram .
2.2、 Introduce the middle 、 Intelligible abstraction .
Put a piece of code into a properly named subroutine , Is one of the best ways to explain the meaning of this code . Compared with reading the following sentences ,
if(node < > NULL)then while(node .next < > NULL)do node node.next
leafName node,name end while else
leafName end i fIt is easier to understand the following sentence :
leafName = GetLeafName(node)
This new procedure is so short , Just give it a good name is enough to explain its purpose . With the above 8 Compare lines of code , This name provides a higher level of abstraction , This makes the code more readable .
2.3、 Subclassing is supported (subclassing).
Cover (override) The number of new codes required for short and regular subroutines , Less than covering lengthy and sloppy subroutines . If you can keep the overridable subroutine simple , Then you will also reduce the probability of making mistakes when implementing derived classes .
2.4、 Hide order .
It's a good idea to hide the sequence of events . such as , If a program usually reads data from the user first , Then read auxiliary data from a file , that , Whether it is a subroutine that reads data from the user or a subroutine that reads data from a file , Should not depend on whether another subroutine has been executed . Another example of sequence , Suppose you write two lines of code , First read the data at the top of the stack , Then reduce stackTop The value of the variable . You should put these two lines of code into one called PopStack() In the subroutine of . So as to hide the order in which these two lines of code must be executed . Hide this information , It's much better than letting them spread around the system .
2.5、 Hide the pointer .
The readability of operation pointer operation is usually poor , And it's easy to make mistakes . By isolating these operations inside the subroutine , You can focus on the intention of the operation itself , Rather than the details of the pointer operation mechanism . meanwhile , If such operations can be completed in one place , Then you will be more confident about the correctness of the code . If you find a more suitable data type than pointer , You can also make changes to the program , Don't worry about breaking the code that originally used pointers .
2.6、 Improve portability .
Subroutines can be used to isolate non portable parts of the program , So as to clearly identify and isolate future migration work . The non portable part includes the non-standard functions provided by the programming language 、 Dependence on hardware , And the dependence on the operating system .
2.7、 Simplify complex Boolean judgments .
In order to understand the flow of the program , There is usually no need to study the details of complex Boolean judgments . These judgments should be put into the function , To improve the readability of the code , because :
(1) In this way, the details of judgment are put aside ;
(2) A descriptive function name can summarize the purpose of the judgment .
Put the logic of Boolean judgment into a separate function , Also emphasized its importance . Doing so will also encourage people to make more efforts inside the function , Improve the readability of judgment code . Final , The main process of the code and the judgment code have become clearer . Simplifying Boolean judgment is also an example of reducing complexity , This point has been discussed before .
2.8、 improve performance .
By using subroutines , You can optimize your code in just one place . Putting the code in one place makes it easier to find out which code is inefficient . meanwhile , Optimization in one place , You can use it ( Whether directly or indirectly ) All the code of this subroutine benefits . Put the code in one place , It is also easier to rewrite code with more efficient algorithms or faster and efficient languages .
3、 Should all subroutines be small ?
No, it isn't . Since there are so many good reasons to write code as subroutines , This is not necessary . in fact , It would be better to write a large subroutine to complete some things .
4、 Some operations seem too simple , Still need to write subroutines ?
When writing a valid subroutine , One of the biggest psychological obstacles is unwillingness to write a simple subroutine for a simple purpose . Writing a subroutine with only twoorthree lines of code may seem overqualified , But experience can show , A good and small subroutine will be very useful .
Small subroutines have many advantages . One is that they can improve their readability . For example, the slave equipment unit (device unit) To pounds (point) Conversion calculation of . People will also see that these dozens of codes are doing the same thing . however , They could have been clearer , So I created a subroutine , And gave it a good name , So that this conversion can be carried out in only one place .
After replacing the code directly embedded in the calculation with this subroutine , This line of code is more readable -- even to the point of self annotation .
This example also suggests another reason to write simple operations as functions : Simple operations often become complex operations . I didn't realize this when I wrote this subroutine , But in some cases , When a device is activated (active) when ,DevicetJnitPer1nch() Returns the 0. That means I have to consider dividing by zero , For this reason, we need to write more 3 Line code .
边栏推荐
- 赋能中国芯创业者!看摩尔精英如何破解中小芯片企业发展难题
- Research on cookies in WebView
- Basic concept of MySQL database and deployment of MySQL version 8.0 (I)
- recursion and iteration
- [C language] implementation of three piece chess games
- Sqlilabs-2 (breakthrough record)
- 希捷发布全新RISC-V架构处理器:机械硬盘相关性能暴涨3倍
- Symbol符号类型
- There are four ways for Nacos to configure hot updates and multiple ways to read project configuration files, @value, @refreshscope, @nacosconfigurationproperties
- Several common methods of SQL optimization
猜你喜欢

Servlet的使用手把手教学(一)

Yolov5 improvement 7: loss function improvement

Nacos配置热更新的4种方式、读取项目配置文件的多种方式,@value,@RefreshScope,@NacosConfigurationProperties

Improvement 11 of yolov5: replace backbone network C3 with lightweight network mobilenetv3

A new paradigm of distributed deep learning programming: Global tensor

Performance optimized APK slimming

The tenth improvement of yolov5: the loss function is improved to Siou

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

WebView optimization

MySQL数据库的基本概念以及MySQL8.0版本的部署(一)
随机推荐
Xshell7, xftp7 personal free version official download, no need to crack, no activation, download and use
TypeError: can‘t convert cuda:0 device type tensor to numpy. Use Tensor. cpu() to copy the tensor to
[copy] Internet terms, abbreviations, abbreviations
A new MPLS note from quigo, which must be read when taking the IE exam ---- quigo of Shangwen network
c语言进阶篇:指针(三)
leetcode 199. 二叉树的右视图
Wheel 6: qserialport serial port data transceiver
软件测试工具fiddler postman jmeter charlse核心功能总结
【C语言】三子棋小游戏实现
(important) first knowledge of C language -- function
LTE cell search process and sch/bch design
MySQL foundation - data query
Es learning directory
The US FCC provided us $1.6 billion to support domestic operators to remove Huawei and ZTE equipment
Is 1E3 a floating point number?
Introduction to original code, inverse code and complement code
Retrofit Usage Summary
【MongoDB】MongoDB数据库的基础使用,特殊情况以及Mongoose的安装和创建流程(含有Mongoose固定版本安装)
cannot resize variables that require grad
Console.log() console display... Solution