当前位置:网站首页>好友让我看这段代码
好友让我看这段代码
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的时间。
——
函数的命名大家可能也发现了,有些是驼峰,有些下划线。
这不是关键,关键是有些函数的用意没有表明清楚,不知道这个函数的作用是什么。
以上是我自己的个人观点
我觉得写代码是要对待一个艺术品,把这个东西做得好,做得优秀,是一件令我们愉悦的事情。
大家共勉之!


边栏推荐
- ORAN专题系列-21:主要的玩家(设备商)以及他们各自的态度、擅长领域
- [300 opencv routines] 239. accurate positioning of Harris corner detection (cornersubpix)
- The world is exploding, and the Google server has collapsed
- Seven lines of code made station B crash for three hours, but "a scheming 0"
- Business visualization - make your flowchart'run'(3. Branch selection & cross language distributed operation node)
- 业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
- 【AI4Code】《InferCode: Self-Supervised Learning of Code Representations by Predicting Subtrees》ICSE‘21
- 7行代码让B站崩溃3小时,竟因“一个诡计多端的0”
- 【OpenCV 例程 300篇】239. Harris 角点检测之精确定位(cornerSubPix)
- 【问题解决】ibatis.binding.BindingException: Type interface xxDao is not known to the MapperRegistry.
猜你喜欢

clickhouse笔记03-- Grafana 接入ClickHouse

【视频】马尔可夫链原理可视化解释与R语言区制转换MRS实例|数据分享
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/6e/9e0abf8db5ec93080033bd89605ac2.jpg)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing

A turbulent life

Zero basic learning canoe panel (16) -- clock control/panel control/start stop control/tab control

录制和剪辑视频,如何解决占用空间过大的问题?

Docker learning - redis cluster -3 master and 3 slave - capacity expansion - capacity reduction building

AtCoder Beginner Contest 261 F // 树状数组

零基础学习CANoe Panel(12)—— 进度条(Progress Bar)

【CSDN 年终总结】结束与开始,一直在路上—— “1+1=王”的2021总结
随机推荐
录制和剪辑视频,如何解决占用空间过大的问题?
业务可视化-让你的流程图'Run'起来(3.分支选择&跨语言分布式运行节点)
Seven lines of code made station B crash for three hours, but "a scheming 0"
Requirements specification template
Zero basic learning canoe panel (13) -- trackbar
Docker学习 - Redis集群-3主3从-扩容-缩容搭建
【AI4Code】《Contrastive Code Representation Learning》 (EMNLP 2021)
pytorch创建自己的Dataset加载数据集
JS convert pseudo array to array
[CSDN year-end summary] end and start, always on the way - "2021 summary of" 1+1= Wang "
Zero basic learning canoe panel (14) -- led control and LCD control
【CSDN 年终总结】结束与开始,一直在路上—— “1+1=王”的2021总结
485通讯( 详解 )
Vim技巧:永远显示行号
Shell common script: get the IP address of the network card
A hard journey
如何理解Keras中的指标Metrics
程序的内存布局
Docekr学习 - MySQL8主从复制搭建部署
Docker learning - redis cluster -3 master and 3 slave - capacity expansion - capacity reduction building