当前位置:网站首页>If you are a C developer, look at these three explicit programming techniques
If you are a C developer, look at these three explicit programming techniques
2022-06-28 21:47:00 【wx5a20cf699eb6f】
Embedded system developers should be as clear as possible , To avoid ASSUME Syndrome and unexpected behavior in its system . In today's article , Let's look at a few areas where embedded developers can clean up code in a more explicit way . |
Embedded system developers should be as clear as possible , To avoid ASSUME Syndrome and unexpected behavior in its system . In today's article , Let's look at a few areas where embedded developers can clean up code in a more explicit way .

Mix of office supplies and gadgets on a wooden desk background. View from above.
Explicit programming techniques #1 – take extern Use with public functions
We all know that we should not use extern, Because it creates global variables , This may lead to various problems . however , The actual use extern A good place for is when creating public functions .
When you define an open function , You can use the following command Create a declaration or prototype in the header :
void Foo(void);
- 1.
It's in the title , So it's obviously a public external function . however , I've been in this situation , You are maintaining a module written by someone else , And like Bar There is no public in the header of such a function API, But its definition is as follows :
void Bar(void)
{
…
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
What is the purpose of this function ? Should it be private and have an electrostatic front ? Should it be public and defined in the title ? If embedded developers will Bar Defined as :
extern Bar(void)
{
…
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
We will know that it is public and in API Missing in , Although someone calls it , The linker can still find it .
Explicit programming techniques #2 – Use the pointer as const Pass to function , Unless they change
The pointer is dangerous , If they unexpectedly increase in some unexpected way during execution 、 Decrement or modification , They can easily lead to disaster . I often encounter function declarations like the following :
void Foo(uint32_t * Param1);
- 1.
This statement is so implicit , I read this statement , The purpose is to pass a pointer to a uint32_t, Where the pointer and the point to uint32_t Memory locations can be changed !
Is this the intention of embedded developers ? If they just want to pass a pointer to a variable , So that it is passed by reference and can be modified by the function ? This function can do this , But they also have the option to change the pointer !
The following statement is very clear to me , The pointer will not change , The value of the point can be changed :
void Foo(uint32_t * const Param1);
- 1.
The parameter is pointing to uint32_t Memory location const The pointer . The pointer cannot be changed in a function , But the point can be . therefore , If someone does the following in a function :
Param++;
- 1.
The compiler will say “ No ! error !”, Let maintainers know they shouldn't do this .
Explicit programming techniques #3 – take “no reference” Variable as const Pass on
Now? , This usually excites embedded developers , And not very good . I was told it was nonsense , But the same , It makes code clear to any developer, including novices .
The idea here is that I might have a function that declares the following :
void Foo(uint32_t Param1);
- 1.
under these circumstances , I pass parameters by copy rather than by reference , For function use . This function can theoretically perform any operation on the local copy . But the same , If someone is maintaining this code , Do they know that we want to receive parameters and use them as constants ? For me, , Unless the statement says so , Otherwise I won't have any clue :
void Foo(const uint32_t Param1);
- 1.
This tells me , This parameter is not expected to be changed or modified in the replica for local use .
These tips help to make the code clearer , And help embedded developers understand the true intent of the code . Now? , These may not be the best practices for implicit code that causes all these losses , But they do make you think you should write software as clear as possible .
This paper addresses : https://www.linuxprobe.com/three-explicit-programming-skills-c-development.html
边栏推荐
- Bitbucket 使用 SSH 拉取仓库失败的问题
- Web自动化工具选择
- How to analyze the relationship between enterprise digital transformation and data asset management?
- Leetcode: expand a binary tree into a linked list_ one hundred and fourteen
- LeetCode1114. 按序打印
- [Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)
- Is it safe to open a dig money account? Is it reliable?
- Query rewriting for opengauss kernel analysis
- E-commerce is popular, how to improve the store conversion rate?
- Mongodb - replica set and sharding
猜你喜欢

API gateway Apache APIs IX helps the evolution of snowball dual active architecture

Smarca2 antibody study: abnova smarca2 monoclonal antibody protocol

Lumiprobe proteorange protein gel dye instructions

【筆記:模擬MOS集成電路】帶隙基准(基本原理+電流模+電壓模電路詳解)

QJsonObject的使用示例

安全 创新 实践|海泰方圆受邀参加“数字时代的网信创新与价值共创”技术交流研讨会

Study on luminiprobe non fluorescent azide -- 3-azido propanol

An artifact extracted from a well-known software and paid by a group of people

Query rewriting for opengauss kernel analysis
![[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)](/img/cd/be62272d465ca990456c222b38df67.png)
[Note: analog MOS integrated circuit] bandgap reference (basic principle + current mode + voltage mode circuit explanation)
随机推荐
Openfire 3.8.2 cluster configuration
接口用例设计
Lua source code analysis: 1 Lua variable type mutability is implemented in C code.
LeetCode560. 和为K的子数组
QStringLiteral(str)
Is the rapid SSL wildcard certificate genuine in 1981
2022年股票在手机上开户安全吗?找谁可以办理?
LeetCode877. 石子游戏
视频号如何下载视频?来看超简单方法!
关于不完全类型的认识
Leetcode daily question - 515 Find the maximum value in each tree row
LeetCode121. The best time to buy and sell stocks
LeetCode116. Populate the next right node pointer for each node
Anti rabbit dylight 488 abbkine universal immunofluorescence (if) toolbox
The comprehensive application of the setstack computer (uva12096) Purple Book p116stl
17 `bs对象.节点名h3.parent` parents 获取父节点 祖先节点
Bitbucket 使用 SSH 拉取仓库失败的问题
Query rewriting for opengauss kernel analysis
16 `bs object Node name Div. attribute contents ` children descendants get child nodes and descendants
LeetCode188. 买卖股票的最佳时机IV