当前位置:网站首页>High quality subroutine
High quality subroutine
2022-07-19 02:46:00 【chip1234】
High quality subroutines
The meaning of subroutines :
- Reduce duplicate code , Make the whole code not bloated
- Improve code efficiency , Local function improvement subroutine
- Promote the development of modern programming
Reason for creating subroutines :
Reduce complexity . You can use subroutines to hide some information , Reduce code size , Improve correctness, etc
Introduce the middle 、 Intelligible abstraction . According to good naming , It can be well-known
Avoid code duplication . Improve the consistency of the same function code
Subclassing is supported . Reduce the chance of making mistakes
Hide order . Subroutines are independent of each other , There is no sequence limit for development
Hide pointer operation . Concentrate on the main intention
Improve portability . Distinguish between non portable code , Reduce the workload of cutting and transplantation
Simplify complex Boolean judgments . Make the main process clearer
improve performance . By modifying the performance of a subroutine , It can improve the performance of the whole program
The length of the subroutine depends on the actual situation
… …
Easy to reconstruct
Subroutine design :
- Functional cohesion . Embodied in the principle of single responsibility , Each subroutine does only one thing , More reliable
Subroutine name principle :
- Describe what subroutines do . The name should describe all the results and side effects , But not long and stupid
- Avoid using meaningless , A vague or ambiguous verb .
- Don't just use numbers to form different subroutine names .
- Determine the length of the subroutine name as needed . The best length 9 – 15 Characters
- When naming a function, you should describe the return value .
- When naming a process, use a strong verb object structure .
- Use antithetical words accurately .
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-TCVuHaSq-1656143363450)(Personal export_20220516.assets/image-20220623175407902.png)] - Establish naming rules for common operations .
How long can a subroutine be written
- According to nesting , level , Depending on the number of decision points and other factors
How to use subroutine parameters
- Press enter - modify - Output sequence parameters .
- Consider creating IN and OUT keyword .
- If several subroutines are used for similar parameters , There should be consistency .
- Do not have parameters that cannot be used .
- Put the state and error variables last
- Do not use subroutine parameters for working variables
- The assumption of using parameters in the interface is explained . Use assertion
- The number of parameters is limited to 7 Within a
- Pass a variable or object to a subroutine to maintain its interface abstraction
- Use named parameters
- Ensure that the actual parameters match the formal parameters
Special considerations when using functions :
- function & The process . What has a return value is a function , What has no return value is the procedure . The return value is void Is also a process . If the main purpose of a subroutine is to return the return value specified by its name , You should return the function , Otherwise, the process should be used .
Set the return value of the return function , Suggest
- Check all possible return paths . Initialize the return value with a default value at the beginning of the function .
- Do not return references or pointers to local data .
Macro subroutines and inline subroutines
Enclose the macro expression in parentheses
#define Cube(a) a*a*a // error demo e.g. Cube(x+1) x+1*x+1*x+1 #define Cube(a) (a)*(a)*(a) // error demo #define Cube(a) ((a)*(a)*(a)) // right demoEnclose macros with multiple statements in curly braces . Replacing function calls with macros is risky , And it's not easy to understand , Unless necessary , It is recommended to avoid using this method .
Use the method of naming subroutines to name macros whose code is like subroutines after expansion , In order to replace macros with subroutines when necessary .
- The side effect is ++ and – Operator , It is easy to mistake macros for subroutines
Restrictions on the use of macro subroutines ,C++
- const Can be used to define constants
- inline Define embedded code
- template Define various standard operations in a type safe manner
- enum Can be used to define enumeration types
- typedef Used to define simple type substitution
- Try not to use macros instead of subroutines , Because it is difficult to find problems with the debugger
Inline subroutine
- inline keyword , Use sparingly
边栏推荐
- [hsjframework] unity time management timemanger timer
- 多层数据包结构及TCP三次握手
- ctfhub--ssrf
- 2022.6.28-database-1 Isolation level of database
- Shell编程规范与变量
- 安装.NET提示“无法建立到信任根颁发机构的证书链”(方法简单有下载地址)
- Decentralized edge rendering meta universe protocol cadeus was invited to attend the cbaia 2022 summit to enable more Web3 application scenarios with technology
- Uniapp wechat applet login (authorize wechat first and then mobile phone number) - (1)
- [tools] unity quickly starts to make the artifact tilemap of 2D and 2.5D games
- 安装软件提示无法定位程序输入点AddDllDirectory于动态链接库Kernel32.dll上(文末有下载地址)
猜你喜欢

Swagger -- the most popular API framework in the world

学习网络基础
![[solution] the local Group Policy Editor (gpedit.msc) in Win 11 cannot be opened](/img/8c/705d5f03ca12eef740957c7c80a9f7.png)
[solution] the local Group Policy Editor (gpedit.msc) in Win 11 cannot be opened

echo -e用法

Echo -e usage

Leetcode --- one question per day

In depth performance test data analysis

FTP service

Project Performance Optimization Practice: solve the white screen problem of the home page, customize the loading animation to optimize the first screen effect
![[unity Editor Extension] find all objects of a script attached in the scene and resources](/img/c2/ea07a227535755945100dc80a43658.png)
[unity Editor Extension] find all objects of a script attached in the scene and resources
随机推荐
Performance test implementation specification Guide
Getting to know Alibaba cloud environment construction for the first time: unable to connect remotely, and having been in the pit: the server Ping fails, FTP is built, the server builds the database,
Convert string to integer
Flyway的SaaS多租户实现方案
General knowledge of network (detailed)
Performance bottleneck positioning XMIND
Redisson实现分布式锁的实战案例-锁单key-锁多key-看门狗
让我们了解awk~
HCIP第一天_HCIA复习
FTP服务
Inverse yuan (I'll add these words if there are too many people using the name)
For solopi app performance test
If a hunter shoots a rabbit with a gun
Leetcode buckle classic question - 42 Connect rainwater
Tree array and St table
正则、扩展表达式,sed文本处理器与awk工具、用脚本改IP地址
Services for NFS
2022 latest software testing tools
Understand inheritance, polymorphism, abstraction and their concepts
Dirty reading, unreal reading, non repeatable reading