当前位置:网站首页>Embedded development: 10 problems for successful code review
Embedded development: 10 problems for successful code review
2022-07-21 08:23:00 【Guangdong embedded Education】
these years , Developers have some common problems when reviewing code , No matter how big the company is , How mature is the development process , There will always be problems . To help alleviate these common problems , Embedded developers are reviewing C Code can be put forward 10 A question , To help find potential bug problem .
problem 1 – No warning when building the program ?
If the compilation fails , You cannot load code on the target . Successful compilation requires programmers to work hard to eliminate any syntax errors , To satisfy the compiler and create the output file . however , The compiler can build an error free application , But you will still find other anomalies , Such as implicit coercion , And report it as a warning . therefore , A truly successful program compilation should not only have zero errors , There should also be zero warnings .
problem 2 – Is there any blocking function ?
Micro controller (MCU) One of the main purposes of is to be able to handle real-time events .MCU It should be able to handle these events in a very certain way , This way can be measured and proved . However, a common mistake is , A driver or some application code segment is written to enter a loop or call a delay function for a long time . however , Loops or delays prevent any other code from running on the processor , This may undermine certainty .
problem 3 – Is there a potential infinite loop ?
Which embedded developer will deliberately put infinite loops into their code ?( Of course, it does not include the code needed in the main loop of the task or application ). However , Many sample codes provided by the network and chip suppliers show infinite cycle failure behavior . for example , Write data to flash memory or EEPROM Your code usually monitors whether the hardware flag is completed . The sample code will create a while loop , To reach a certain state before continuing . But if the hardware fails and the flag is never set , The code will fall into an infinite loop !
One way to remedy this infinite loop failure is to have the loop monitoring system tick or limit the number of times the loop can be executed before it is finally determined that an error has occurred . These remedies allow error handling to be built into the system in the event of hardware failure . Although it is generally believed that , Hardware ( And software ) It did fail .
problem 4 – The function parameter should be const Do you ?
Programmers tend not to use as much as possible const, Especially when it comes to function parameters . Declare the passed function parameter as const It is a good way to prevent the variable from being accidentally modified in the function . Why let future embedded developers realize that they should not modify the key variables of the system, but only use it ?
problem 5 – Whether the cyclomatic complexity of the code is less than 10?
Monitoring the cyclomatic complexity measurement of functions is a good way to help limit the complexity of functions . This indicator is directly related to the minimum number of test cases that need to be executed on the function to test each branch . More Than This , This indicator also really shows how much developers need to remember when writing or modifying functions . Because most people can only track 7 To 9 thing , So keep the cyclomatic complexity at 10 The following are good choices to help reduce the error rate .
problem 6 – extern Is it limited to static free use ?
C The scope of the language default variable is extern, This default value is implicit , Variables declared in the module that do not use static variables are preceded by an invisible large external variable . The only way to get rid of that invisible exterior is to put a visible static in front of the declaration . Another advantage of this approach is to make variables become local variables within the scope , Help data hiding and encapsulation . The most common place to look for implicit external variables is module level variable declarations .
problem 7 – Is it all if ... else if ... Conditions are based on else ending ?
stay switch The use of defaults in statements should be mandatory . If there is no default , Static analysis tools will report errors . Embedded developers can easily see , If conditions warrant use in all cases switch sentence , There may be an unexpected or neglected situation , There should be a default end-all situation . This also applies to if ... else if ... Conditions . If you want to check two or more conditions , What if these conditions do not meet the current conditions ? The last one in the statement else It's like switch The default in the statement is the same .
problem 8 – Whether there are assertions and / Or input / Output check ?
Embedded software developers should add assertions to their code , To verify whether their assumptions about the behavior of the program at some points are correct , Boundary checks should be performed on inbound and outbound data . I still remember the old saying “ Garbage goes into , Garbage out ” Do you ?
problem 9 – Whether there is title protection present ?
Title Protection is a simple macro , It can ensure that the title file does not contain multiple times in the translation unit . Protection is to prevent double inclusion #include Instructions . Excluding Title Protection may lead to some very strange static analysis behavior , what's more , Embedded developers use protection to prevent multiple definition errors .
problem 10 – Whether floating point mathematics is used ?
The use of floating-point mathematics in embedded systems can be a thorny topic . Resource constrained microcontrollers usually do not include floating-point units (FPU). This lack means that the processor has only one way to perform floating-point calculations : Use library functions . Library functions for floating-point math are usually slow and inefficient , They do not necessarily have deterministic behavior , And they can cause code to swell . For these reasons , Developers should carefully consider when to use floating point in microcontrollers . They should also perform additional tests , And alternative methods should be considered , Look up tables, for example 、 Zoom and fixed point math .
Conclusion
Many engineers find code reviews boring , But it's actually interesting , Because performing code reviews can be a very exciting moment . Every programmer is interested in embedded software development and C Languages have their own unique views and opinions , So there is always something to learn . However , Although embedded developers are implementing many insights and different levels of checks and balances , But mistakes still exist . These ten problems solve the common errors and misunderstandings that should be checked at each code review when developing embedded software .
边栏推荐
- Chapter3 : Fighting COVID-19 with Artificial Intelligence
- Worthington羧基转移丨碳酸酐酶的应用和文献参考
- After Huawei, no one can stop apple. It is expected that iphone14 will be popular with hundreds of millions of domestic users
- 三维数据(channel在第2维)-四维数据(输入到pooling层之前,channel在第一维)-三维数据(channel在第2维)
- With the release of Hongmeng 3.0, multi screen integration has been steadily promoted, but Google has suffered another setback
- Open up the chain and build a credible value ecosystem of the whole chain - Chang'an chain Oracle
- ES6模块化
- 从零开始学习CANoe(十六)—— Graphics
- 安全第二天课后练习
- 中继器、集线器、网桥、交换机、路由器
猜你喜欢
Overview | comprehensive comparative research on image denoising
从放弃到精通,这13篇推荐系统论文一定要读!【附资料】
鴻蒙3.0發布,多屏融合穩步推進,穀歌卻再受重挫
5G网络用户4.5亿,5G套餐用户超9亿,为何用户还不愿接受5G?
重组单克隆抗体丨ProSci CD154 抗体实例分析
ProSci 抗CD22抗体Epratuzum28流式细胞术展示
嵌入式中常见的存储器总结(一)存储器分类
Laravel realizes bidirectional synchronization of database and migrated files
Open up the chain and build a credible value ecosystem of the whole chain - Chang'an chain Oracle
Five coquettish projects were born!
随机推荐
redis实现周边景点由近到远排序
从零开始学习CANoe(十六)—— Graphics
Detailed explanation of C language array
动态库和静态库(.so/dll,.a/lib)
重组单克隆抗体丨ProSci CD154 抗体实例分析
双快门技术
Locate the kernel code through vmlinuz
Summary of common memory in embedded system (I) memory classification
多线程与高并发(二)—— Synchronized 加锁解锁流程
内核结构与设计
Three dimensional data (channel in the second dimension) - four-dimensional data (channel in the first dimension before input to the pooling layer) - three-dimensional data (channel in the second dime
Four redis cluster schemes you must know and their advantages and disadvantages
安全第二天课后练习
Rose pass is being issued!
Point cloud format reading and saving
About MySQL Boolean and tinyint (1)
面试题:八皇后问题(N皇后问题)「建议收藏」
华为摄像机布局
stap命令行小技巧(笔记,持续更新)
Introduction to JPEG image compression viewed by programmers (be careful with multiple images)