当前位置:网站首页>How many lines of code is appropriate for a function? Clean Code
How many lines of code is appropriate for a function? Clean Code
2022-07-25 19:11:00 【Undefind_ object】
ava Middle function is also called method ,2 They are the same , Now let's talk about , How many lines of code is appropriate in a function ?
In terms of readability and maintainability , The more concise the code, the better
How many lines of code is appropriate in a function , In fact, there are no mandatory provisions , Just be reasonable , Can't be generalized ( Some people say that a function should not exceed 15 That's ok , Some people say that a function should not exceed 50 That's ok , Some people say that a function should not exceed 200 That's ok , I think it's all ok , No one's right or wrong , Only whether it is reasonable and appropriate )
Don't write too many lines in a function , Too many lines will increase the complexity and increase the difficulty of reading and maintenance .
Personally, I think the code in a function should not exceed 30 That's ok , Of course! ,26 That's ok ,37 OK, actually, it's ok , What I said 30 Line is only a rough range of lines , The number of lines of code is not absolute , Everyone should understand flexibility 、 Flexible flexibility , Don't be so rigid and conformist .
A function should not exceed 30 That's ok ,30 It's OK to go around , For example, one of your functions writes 28 Row sum 35、36 Everything is ok , In short, a function should not be written too long , If one of your functions writes 50 many 、60 Multiple lines , even to the extent that 70 many 、80 Multiple lines , At this point, you should consider disassembling this function into multiple functions , Because it will be clearer after disassembly , It is easier to read and maintain .
Some enterprises , A function does not exceed 15 Line code . I saw some articles on the Internet saying , The requirement of Microsoft is , Don't exceed... In a function 15 Line code !
When writing program code , Give you a few programming principles
1. Write code , Write the function name , When writing variable names and so on , To see the meaning of fame , The name is not afraid of being too long , I'm afraid the name is obscure .
2. It is suggested that the code you write should be easy to understand , Let yourself and others flow freely when reading your code ( Of course! , It's not going to happen overnight , We need to practice slowly ).
3. Comment style of code 、 Naming style 、 Writing style 、 Naming standards and so on must be unified , Don't personalize ( such as : Deliberately give the variable or path a Chinese name or $ Dollar symbol , You have nothing to do !), Who makes personalization , The more error prone the code is ( Don't personalize the programming field )
4. Don't write too many nested loops in a function , Generally, the upper limit is written 2 Layer cycle , If you write 3 Layers even 4 Layer cycle , The code will be hard to read , It's hard to maintain , If in a function , If there are many nested loops ( exceed 2 Layer cycle ), Then disassemble the function .
5. All in all , A function does one thing , So keep the functions simple 、 Refine 、 concise 、 concise 、 Easy to understand 、 suit both the refined and the common people , But don't overdo it , carp !
6. The more lines of code in a function , The complexity of this function will increase , The fewer lines of code in the function , The complexity of this function will decrease .
I once heard a little story , There is a little friend who is maintaining the code left by others before , Open that function and have a look , He was instantly drunk , That function actually has 1000 Line above , In an instant, life is worse than death , I feel loveless , I can't wait to resign and leave immediately .
This explains why long functions are being decomposed / After dismantling , Maintainability will be greatly improved .
Finally, I hope you all maintain a strong curiosity in programming , Only in this way can you make progress !
In addition, I would like to give you a few suggestions :
1. Skillfully use IDE( development tool )
2. Master database (oracle、mysql)
3. Have a certain understanding of the operating system
4. Familiar with network protocol TCP/IP
5. Have a strong curiosity about program programming
6. Familiar with software development process
7. Properly understand some knowledge of network and hardware
The following are the views of some netizens on the Internet :
Net friend A: The optimal number of lines of a function is limited to 50-80 Between the lines , In this way, for the future maintenance of developers , Simple code is easier to modify and maintain . The number of rows of a class should be unlimited .
Net friend B: Number of lines of code in the function , There is no limit , But general development advice , A function should not exceed 50 That's ok , Too many lines, poor readability . But how many functions are put in a class , It doesn't matter how many lines , Because current development tools such as eclipse, You can quickly locate a function body . In some large applications , It is normal for a class to swell to thousands of lines . So my personal suggestion is , Keep the separate method at 50 Within the line . The whole class , As many lines as you need .
In addition to the number of lines of code in the function , By the way, let's talk about how many lines of code are appropriate in a class ?
Theoretically, as many lines of code as there are in a class can actually .
Theoretically, how many functions are there in the class , How many variables are actually ok .
Theory is theory , Reality is not as perfect as theory , Sometimes I have to compromise .
The rules are dead , Man is alive. , The rules are also set by people , It can be fixed or broken , It can also be changed , Be flexible according to the actual situation !
Loading classes takes time , So my personal advice , Don't write too many lines in a class , Of course! , There are many things in some classes , It's normal to write thousands of lines , In general , I suggest , We write our own classes , Especially the frequently used classes , Don't count too many lines , Unless you really need to write a lot .
Finally, some suggestions : Programming , Writing class , Write variables , It is best to write functions :
1. The notes are clear
2. Regular intervals
3. High readability
4. Good maintainability
5. Good scalability 边栏推荐
- Cross Site Request Forgery in PHP
- 小程序毕设作品之微信校园维修报修小程序毕业设计成品(3)后台功能
- Go代码检查工具
- Real estate enterprises have launched a "war of guarantee"
- 弱网测试工具-QNET
- Detailed explanation of Bluetooth protocol (what is Bluetooth)
- 接口自动化测试平台FasterRunner系列(三)- 操作示例
- Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice
- 小程序毕设作品之微信校园维修报修小程序毕业设计成品(2)小程序功能
- qt exec和show的区别
猜你喜欢
![[open source project] stm32c8t6 + ADC signal acquisition + OLED waveform display](/img/5f/413f1324a8346d7bc4a9490702eef4.png)
[open source project] stm32c8t6 + ADC signal acquisition + OLED waveform display

21 days proficient in typescript-4 - type inference and semantic check

Modelsim and quartus jointly simulate PLL FIFO and other IP cores

阿里云技术专家郝晨栋:云上可观测能力——问题的发现与定位实践

2022 IAA industry category development insight series report - phase II

Pixel2mesh generates 3D meshes from a single RGB image eccv2018

【919. 完全二叉树插入器】

【iniparser】项目配置工具iniparser的简单使用

小程序毕设作品之微信校园维修报修小程序毕业设计成品(8)毕业设计论文模板

The degree of interval of basic music theory
随机推荐
What is the application value of MES management system
【919. 完全二叉树插入器】
C 调的满级和玄
Clip can also do segmentation tasks? The University of Gottingen proposed a model clipseg that uses text and image prompt and can do three segmentation tasks at the same time, squeezing out the clip a
Pymoo学习 (6):终止条件
PyQt5单击QTableView垂直表头verticalHeader获取行数据以及单击单元格获取行数据操作
小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告
Wechat campus maintenance application applet graduation design finished product of applet completion work (3) background function
Analysis of the internet jam in IM development? Network disconnection?
浅析IM即时通讯开发出现上网卡顿?网络掉线?
常用的开发软件下载地址
华为交换机系统软件升级和安全漏洞修复教程
Cross Site Request Forgery in PHP
鸿蒙-大喵计算画板-视频
Hough transform understanding [easy to understand]
JS basic type reference type deep / shallow clone copy
【阅读笔记】《深度学习》第一章:引言
Real estate enterprises have launched a "war of guarantee"
SQL realizes 10 common functions of Excel, with original interview questions attached
Detailed explanation of Bluetooth protocol (what is Bluetooth)