当前位置:网站首页>好友让我看这段代码
好友让我看这段代码
2022-07-25 12:48:00 【嵌入式Linux,】
周末的时候,一个微信好友让我旁边看一段代码
在写下面的文章之前,我先简单说下
写代码是一件非常有意思的事情,同时也是一件需要我们认真对待的事情,我不认为一定要把代码写的和大神一样看不明白,但是至少要逻辑清晰,不能出现混淆不清的情况发生。
最近一年,我也在不断的提高自己的编程能力,之前的工作,很多是做处理系统和驱动等事情,但是实际的应用编程并没有很强的功底。也是在不断学习和前进,也非常感谢朋友和同事们对我的帮助和支持。
——
这段代码是这样的
static unsigned short timer_tick_cnt = 0;
static ___interrupt USER_TIMER void timer_isr(void)//中断服务函数
{
if (TMR->CON & BIT(15)) //定时器溢出中断标志位
{
TMR->CON |= BIT(14);//清中断
///putchar('@');
//todo,中断函数执行程序...
timer_tick_cnt++;
}
}
static unsigned short timer_tick_get_current_timer(void)
{
unsigned short timer_tick_count = timer_tick_cnt;
return timer_tick_count;
}
unsigned short usRecord = 0;
unsigned char timer_tick_timeout_wait(unsigned short *p_var, unsigned short timeout)
{
if( timer_tick_get_current_timer() - *p_var < timeout )
{
///usRecord = timer_tick_get_current_timer();
///printf("=======rec=%d,*p_var=%d=====\n",usRecord,*p_var);
///printf("====TIMER return 0====\n");
return 0;
}
*p_var = timer_tick_get_current_timer();
///usRecord///printf("====TIMER return 1====\n");
return 1;
}
unsigned short testTimer = 0;
#define TIME_TICK_1MS_1S 1000
void testHandler(void)
{
if(timer_tick_timeout_wait(&testTimer,TIME_TICK_1MS_1S))//1ms的中断溢出,故1000为1s
{
printf("helloworld\n");//问题点是如果timer_tick_timeout_wait的参数timeout定义为unsigned short,在约65s(65535ms)后该函数一直返回0而不再返回1,unsigned int正常
}
}
int main(void)
{
timerInit();//1ms的定时器中断,该函数不是问题点,问题点在下面
while(1)
{
testHandler();
}
}这个是原始的代码,我没有做任何的修改。
问题他在代码里面描述的比较清晰
//问题点是如果timer_tick_timeout_wait的参数timeout定义为unsigned short,在约65s(65535ms)后该函数一直返回0而不再返回1,unsigned int正常
细心的人会发现一个问题

这 timer_tick_cnt 该死的变量一直递增
我之前写过一篇文章,说是内核里面时间戳的问题,如果保存时间戳的变量定义有问题,那可能也会导致时间有问题。
这也是他发现他的代码有问题的原因。
——
我再说下其他的情况
他设计的这个系统的初衷是系统定时器到一个指定的时间后去执行一个函数,但是直接把在定时器里面的变量拿到外面去判断,这里就不很好。
定时器应该只完成定时的事情,至于到了多少时间,告诉外面的其他任务就好了,这样可以做到高内聚。
变量timer_tick_cnt在其他地方操作,后续有问题排查起来肯定会很难受。


——
变量的命名、函数的命名、代码风格
简直不堪入目,有的地方用下划线、有的地方用驼峰。

还有这个函数的执行,我有点看不懂

CPU就是这样被你累坏的啊。
关于变量和函数命名的网站,我推荐这个,这个网站非常适合我们
https://www.chtml.cn

这不比你自己想好多了。
——
宏的含义不清晰

这样给宏命名是非常不好的,先是1MS 又是1S,所以这个宏到底是啥,如果不看代码的话是很难理解意思。
如果是我,我会表明这个宏做什么事情,但是后面要加上MS,因为这个事件是MS的时间。
——
函数的命名大家可能也发现了,有些是驼峰,有些下划线。
这不是关键,关键是有些函数的用意没有表明清楚,不知道这个函数的作用是什么。
以上是我自己的个人观点
我觉得写代码是要对待一个艺术品,把这个东西做得好,做得优秀,是一件令我们愉悦的事情。
大家共勉之!


边栏推荐
- 【运维、实施精品】月薪10k+的技术岗位面试技巧
- Leetcode 1184. distance between bus stops
- Docker learning - redis cluster -3 master and 3 slave - capacity expansion - capacity reduction building
- [problem solving] ibatis.binding BindingException: Type interface xxDao is not known to the MapperRegistry.
- mysql函数汇总之日期和时间函数
- Use vsftpd service to transfer files (anonymous user authentication, local user authentication, virtual user authentication)
- Zero basic learning canoe panel (14) -- led control and LCD control
- ECCV 2022 | 登顶SemanticKITTI!基于二维先验辅助的激光雷达点云语义分割
- 2022.07.24 (lc_6126_design food scoring system)
- 【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
猜你喜欢

web安全入门-UDP测试与防御

跌荡的人生

Memory layout of program

微软提出CodeT:代码生成新SOTA,20个点的性能提升

Common operations for Yum and VIM

mysql函数汇总之日期和时间函数

Cyberspace Security penetration attack and defense 9 (PKI)

Shell common script: get the IP address of the network card

卷积神经网络模型之——GoogLeNet网络结构与代码实现
![[review SSM framework series] 15 - Summary of SSM series blog posts [SSM kill]](/img/fb/6ca8e0eb57c76c515e2aae68e9e549.png)
[review SSM framework series] 15 - Summary of SSM series blog posts [SSM kill]
随机推荐
JS 中根据数组内元素的属性进行排序
Introduction to web security UDP testing and defense
ECCV 2022 | climb to the top semantickitti! Semantic segmentation of LIDAR point cloud based on two-dimensional prior assistance
卷积神经网络模型之——GoogLeNet网络结构与代码实现
State mode
Azure Devops(十四) 使用Azure的私有Nuget仓库
2022 年中回顾 | 大模型技术最新进展 澜舟科技
【问题解决】org.apache.ibatis.exceptions.PersistenceException: Error building SqlSession.1 字节的 UTF-8 序列的字
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2 supplementary problem solution (g, J, K, l)
7行代码让B站崩溃3小时,竟因“一个诡计多端的0”
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing
Clickhouse notes 03-- grafana accesses Clickhouse
Detailed explanation of switch link aggregation [Huawei ENSP]
Seven lines of code made station B crash for three hours, but "a scheming 0"
Use vsftpd service to transfer files (anonymous user authentication, local user authentication, virtual user authentication)
简单了解流
Perf performance debugging
AtCoder Beginner Contest 261E // 按位思考 + dp
卷积神经网络模型之——AlexNet网络结构与代码实现
The programmer's father made his own AI breast feeding detector to predict that the baby is hungry and not let the crying affect his wife's sleep