RT-Thread 编程风格_rtthread代码风格(1),挑战华为社招

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Golang全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Go语言开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024b (备注go)
img

正文

/* IPC object init */
static rt_err_t _ipc_object_init()

/* UART driver ops */
static rt_err_t _uart_configure()
static rt_err_t _uart_control()

调用注册设备接口的函数命名:使用 rt_hw_class_init() 格式,举例:

int rt_hw_uart_init(void)
int rt_hw_spi_init(void)

8.注释编写

请使用英文做为注释,使用中文注释将意味着在编写代码时需要来回不停的切换中英文输入法从而打断编写代码的思路。并且使用英文注释也能够比较好的与中国以外的技术者进行交流。

语句注释

源代码的注释不应该过多,更多的说明应该是代码做了什么,仅当个别关键点才需要一些相应提示性的注释以解释一段复杂的算法它是如何工作的。对语句的注释只能写在它的上方或右方,其他位置都是非法的。

/* 你的英文注释 */

函数注释

注释以 /** 开头,以 */ 结尾,中间写入函数注释,组成元素如下,每个元素描述之间空一行,且首列对齐:

  • @brief + 简述函数作用。在描述中,着重说明该函数的作用,每句话首字母大写,句尾加英文句号。
  • @note + 函数说明。在上述简述中未能体现到的函数功能或作用的一些点,可以做解释说明,每句话首字母大写,句尾加英文句号。
  • @see + 相关 API 罗列。若有与当前函数相关度较高的 API,可以进行列举。
  • @param + 以参数为主语 + be 动词 + 描述,说明参数的意义或来源。
  • @return + 枚举返回值 + 返回值的意思,若返回值为数据,则直接介绍数据的功能。
  • @warning + 函数使用注意要点。在函数使用时,描述需要注意的事项,如使用环境、使用方式等。每句话首字母大写,句尾加英文句号。

注释模版请参见:rt-thread/src/ipc.c 源码文件,英文注释请参考使用 grammarly 以及谷歌翻译。

/**

  • @brief The function will initialize a static event object.
  • @note For the static event object, its memory space is allocated by the compiler during compiling,
  •       and shall placed on the read-write data segment or on the uninitialized data segment.
    
  •       By contrast, the rt_event_create() function will allocate memory space automatically
    
  •       and initialize the event.
    
  • @see rt_event_create()
  • @param event is a pointer to the event to initialize. It is assumed that storage for the event
  •       will be allocated in your application.
    
  • @param name is a pointer to the name that given to the event.
  • @param value is the initial value for the event.
  •       If want to share resources, you should initialize the value as the number of available resources.
    
  •       If want to signal the occurrence of an event, you should initialize the value as 0.
    
  • @param flag is the event flag, which determines the queuing way of how multiple threads wait
  •       when the event is not available.
    
  •       The event flag can be ONE of the following values:
    
  •           RT_IPC_FLAG_PRIO          The pending threads will queue in order of priority.
    
  •           RT_IPC_FLAG_FIFO          The pending threads will queue in the first-in-first-out method
    
  •                                     (also known as first-come-first-served (FCFS) scheduling strategy).
    
  •           NOTE: RT_IPC_FLAG_FIFO is a non-real-time scheduling mode. It is strongly recommended to
    
  •           use RT_IPC_FLAG_PRIO to ensure the thread is real-time UNLESS your applications concern about
    
  •           the first-in-first-out principle, and you clearly understand that all threads involved in
    
  •           this event will become non-real-time threads.
    
  • @return Return the operation status. When the return value is RT_EOK, the initialization is successful.
  •       If the return value is any other values, it represents the initialization failed.
    
  • @warning This function can ONLY be called from threads.
    */
    rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)
    {

    }

9.缩进及分行

缩进请采用 4 个空格的方式。如果没有什么特殊意义,请在 “{” 后进行分行,并在下一行都采用缩进的方式,例如:

if (condition)
{
/* others */
}

唯一的例外是 switch 语句,switch-case 语句采用 case 语句与 switch 对齐的方式,例如:

switch (value)
{
case value1:
break;
}

case 语句与前面的 switch 语句对齐,后续的语句则采用缩进的方式。分行上,如果没有什么特殊考虑,请不要在代码中连续使用两个以上的空行

10.大括号与空格

从代码阅读角度,建议每个大括号单独占用一行,而不是跟在语句的后面,例如:

if (condition)
{
/* others */
}

匹配的大括号单独占用一行,代码阅读起来就会有相应的层次而不会容易出现混淆的情况。空格建议在非函数方式的括号调用前留一个空格以和前面的进行区分,例如:

if (x <= y)
{
/* others */
}

for (index = 0; index < MAX_NUMBER; index ++)
{
/* others */
}

建议在括号前留出一个空格(涉及的包括 if、for、while、switch 语句),而运算表达式中,运算符与字符串间留一个空格。另外,不要在括号的表达式两侧留空格,例如:

if ( x <= y )
{
/* other */
}

这样括号内两侧的空格是不允许的。

11.trace、log信息

在 RT-Thread 中,普遍使用的 log 方式是 rt_kprintf。rt_kprintf 在 RT-Thread 被实现成一个采用轮询、非中断方式的字串输出,能够适合于在中断这类"即时"显示日志的场合。因为这种轮询方式的存在,也必然会影响到日志输出的时序关系。

建议在代码中不要频繁的使用 rt_kprintf 作为日志输出,除非你真正的明白,你的代码运行占用的时间多一些也没什么关系。

日志输出应该被设计成正常情况下是关闭状态(例如通过一个变量或宏就能够开启),并且当真正输出日志时,日志是易懂易定位问题的方式。"天书式"的日志系统是糟糕的,不合理的。

12.函数

在内核编程中,函数应该尽量精简,仅完成相对独立的简单功能。函数的实现不应该太长,函数实现太长,应该反思能够如何修改(或拆分)使得函数更为精简、易懂。

13.对象

RT-Thread 内核采用了 C 语言对象化技术,命名表现形式是:对象名结构体表示类定义、对象名 + 动词短语形式表示类方法,例如:

struct rt_timer
{
struct rt_object parent;
/* other fields /
};
typedef struct rt_timer
rt_timer_t;

结构体定义 rt_timer 代表了 timer 对象的类定义;

rt_timer_t rt_timer_create(const char* name,
void (timeout)(void parameter),
void* parameter,
rt_tick_t time, rt_uint8_t flag);
rt_err_t rt_timer_delete(rt_timer_t timer);
rt_err_t rt_timer_start(rt_timer_t timer);
rt_err_t rt_timer_stop(rt_timer_t timer);

rt_timer + 动词短语的形式表示能够应用于 timer 对象的方法。

在创建一个新的对象时,应该思考好,对象的内存操作处理:是否允许一个静态对象存在,或仅仅支持从堆中动态分配的对象。

14.格式化代码

格式化代码是指通过脚本自动整理你的代码,并使其符合 RT-Thread 的编码规范。本文提供以下两种自动格式化代码方法,可以自行选择或配合使用。

使用 astyle 格式化

用 astyle 自动格式化代码,参数如下:

–style=allman
–indent=spaces=4
–indent-preproc-block
–pad-oper
–pad-header
–unpad-paren
–suffix=none
–align-pointer=name
–lineend=linux
–convert-tabs
–verbose

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Go)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
ux
–convert-tabs
–verbose

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024b (备注Go)
[外链图片转存中…(img-tptHZKCM-1713411159644)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值