What are the naming conventions commonly use in C? I know there are at least two:
C中常用的命名规则是什么?我知道至少有两个:
- GNU / linux / K&R with lower_case_functions
- GNU / linux / K&R和小写的case_function。
- ? name ? with UpperCaseFoo functions
- 吗?的名字吗?UpperCaseFoo功能
I am talking about C only here. Most of our projects are small embedded systems in which we use C.
这里只讨论C。我们的大多数项目都是使用C语言的小型嵌入式系统。
Here is the one I am planning on using for my next project:
这是我计划在我的下一个项目中使用的一个:
C Naming Convention
C命名约定
Struct TitleCase
Struct Members lower_case or lowerCase
Enum ETitleCase
Enum Members ALL_CAPS or lowerCase
Public functions pfx_TitleCase (pfx = two or three letter module prefix)
Private functions TitleCase
Trivial variables i,x,n,f etc...
Local variables lower_case or lowerCase
Global variables g_lowerCase or g_lower_case (searchable by g_ prefix)
10 个解决方案
#1
91
The most important thing here is consistency. That said, I follow the GTK+ coding convention, which can be summarized as follows:
这里最重要的是一致性。也就是说,我遵循GTK+编码约定,可以总结如下:
- All macros and constants in caps:
MAX_BUFFER_SIZE
,TRACKING_ID_PREFIX
. - 所有宏和常量都在大写:MAX_BUFFER_SIZE、TRACKING_ID_PREFIX中。
- Struct names and typedef's in camelcase:
GtkWidget
,TrackingOrder
. - camelcase中的Struct名称和typedef: GtkWidget, TrackingOrder。
- Functions that operate on structs: classic C style:
gtk_widget_show()
,tracking_order_process()
. - 在structs上操作的函数:经典C样式:gtk_widget_show(), tracking_order_process()。
- Pointers: nothing fancy here:
GtkWidget *foo
,TrackingOrder *bar
. - 指针:这里没什么特别的:GtkWidget *foo, TrackingOrder *bar。
- Global variables: just don't use global variables. They are evil.
- 全局变量:不要使用全局变量。他们是邪恶的。
- Functions that are there, but shouldn't be called directly, or have obscure uses, or whatever: one or more underscores at the beginning:
_refrobnicate_data_tables()
,_destroy_cache()
. - 在那里的函数,但不应该直接调用,或者有一些模糊的用途,或者其他的:在开始时一个或多个下划线:_refrobnicate_data_tables(), _destroy_cache()。
#2
25
"Struct pointers" aren't entities that need a naming convention clause to cover them. They're just struct WhatEver *
. DON'T hide the fact that there is a pointer involved with a clever and "obvious" typedef. It serves no purpose, is longer to type, and destroys the balance between declaration and access.
“结构指针”不是需要命名约定子句来覆盖它们的实体。它们只是结构而已。不要隐藏一个事实,即有一个指针与一个聪明而“明显”的typedef有关。它没有任何用途,只是更长时间地输入,并破坏声明和访问之间的平衡。
#3
12
Well firstly C doesn't have public/private/virtual functions. That's C++ and it has different conventions. In C typically you have:
首先,C没有公共/私有/虚拟函数。这是c++,它有不同的惯例。在C语言中,你通常有:
- Constants in ALL_CAPS
- 常数ALL_CAPS
- Underscores to delimit words in structs or function names, hardly ever do you see camel case in C;
- 用下划线分隔结构或函数名中的单词,在C中几乎看不到驼峰情况;
- structs, typedefs, unions, members (of unions and structs) and enum values typically are in lower case (in my experience) rather than the C++/Java/C#/etc convention of making the first letter a capital but I guess it's possible in C too.
- struct、typedefs、union、members (union和struct的成员)和enum值通常是小写的(根据我的经验),而不是使用c++ /Java/ c#等约定将第一个字母作为大写,但我想在C中也是可能的。
C++ is more complex. I've seen a real mix here. Camel case for class names or lowercase+underscores (camel case is more common in my experience). Structs are used rarely (and typically because a library requires them, otherwise you'd use classes).
c++是更复杂的。我在这里看到了真正的混合。类名或小写+下划线的驼色格(驼色格在我的经验中更常见)。结构很少被使用(通常是因为库需要它们,否则就会使用类)。
#4
8
I would recommend against mixing camel case and underscore separation (like you proposed for struct members). This is confusing. You'd think, hey I have get_length
so I should probably have make_subset
and then you find out it's actually makeSubset
. Use the principle of least astonishment, and be consistent.
我建议不要混合使用驼色大小写和下划线分隔(就像您对struct成员所建议的那样)。这是令人困惑的。你可能会想,嘿,我有get_length所以我应该有make_子集然后你发现它实际上是makeSubset。使用最少惊讶的原则,并保持一致。
I do find CamelCase useful to type names, like structs, typedefs and enums. That's about all, though. For all the rest (function names, struct member names, etc.) I use underscore_separation.
我发现CamelCase对于输入名称非常有用,比如struct、typedefs和enums。不过,这是所有。对于所有的rest(函数名、结构成员名称等),我使用了underscore_separation。
#5
8
Coding in C#, java, C, C++ and objective C at the same time, I've adopted a very simple and clear naming convention to simplify my life.
同时使用c#、java、C、c++和objective C编写代码,我采用了一个非常简单明了的命名约定来简化我的生活。
First of all, it relies on the power of modern IDEs (such as eclipse, Xcode...), with the possibility to get fast information by hovering or ctrl click... Accepting that, I suppressed the use of any prefix, suffix and other markers that are simply given by the IDE.
首先,它依赖于现代ide的强大功能(如eclipse、Xcode…),可以通过悬停或ctrl单击来获取快速信息。接受这一点,我禁止使用IDE中简单给出的前缀、后缀和其他标记。
Then, the convention:
然后,该公约:
- Any names MUST be a readable sentence explaining what you have. Like "this is my convention".
- 任何名字必须是一个可读的句子来解释你所拥有的。比如"这是我的惯例"
- Then, 4 methods to get a convention out of a sentence:
- THIS_IS_MY_CONVENTION for macros, enum members
- 对于宏、枚举成员的THIS_IS_MY_CONVENTION
- ThisIsMyConvention for file name, object name (class, struct, enum, union...), function name, method name, typedef
- 用于文件名、对象名(类、结构、枚举、联合…)、函数名、方法名、类型定义的ThisIsMyConvention
-
this_is_my_convention global and local variables,
parameters, struct and union elements - this_is_my_convention全局变量和局部变量、参数、结构和联合元素
- thisismyconvention [optional] very local and temporary variables (such like a for() loop index)
- thisismyconvention[可选]非常本地和临时变量(例如for()循环索引)
- 然后,4种方法得到大会的一个句子:THIS_IS_MY_CONVENTION宏,枚举成员ThisIsMyConvention文件名,对象名称(类、结构、枚举、工会…),函数名,方法名,typedef THIS_IS_MY_CONVENTION全球和本地变量、参数、结构和联合元素ThisIsMyConvention(可选的)本地和临时变量(如()循环指数)
And that's it.
就是这样。
It gives
它给
class MyClass {
enum TheEnumeration {
FIRST_ELEMENT,
SECOND_ELEMENT,
}
int class_variable;
int MyMethod(int first_param, int second_parameter) {
int local_variable;
TheEnumeration local_enum;
for(int myindex=0, myindex<class_variable, myindex++) {
localEnum = FIRST_ELEMENT;
}
}
}
#6
5
Here's an (apparently) uncommon one, which I've found useful: module name in CamelCase, then an underscore, then function or file-scope name in CamelCase. So for example:
这里有一个(显然)不常见的,我发现它很有用:CamelCase中的模块名、下划线、CamelCase中的函数名或文件范围名。举个例子:
Bluetooth_Init()
CommsHub_Update()
Serial_TxBuffer[]
#7
2
I'm confused by one thing: You're planning to create a new naming convention for a new project. Generally you should have a naming convention that is company- or team-wide. If you already have projects that have any form of naming convention, you should not change the convention for a new project. If the convention above is just codification of your existing practices, then you are golden. The more it differs from existing de facto standards the harder it will be to gain mindshare in the new standard.
我被一件事搞糊涂了:你计划为一个新项目创建一个新的命名约定。一般来说,您应该在整个公司或团队范围内使用命名约定。如果已经有项目具有任何形式的命名约定,则不应该为新项目更改约定。如果上面的约定只是对现有实践的编纂,那么您就是黄金。它越不同于现有的实际标准,就越难在新标准中获得人们的关注。
About the only suggestion I would add is I've taken a liking to _t at the end of types in the style of uint32_t and size_t. It's very C-ish to me although some might complain it's just "reverse" Hungarian.
关于我要添加的唯一建议是,我喜欢uint32_t和size_t类型末尾的_t。这对我来说很像c,尽管有些人可能会抱怨说它只是“反向”匈牙利语。
#8
2
You know, I like to keep it simple, but clear... So here's what I use, in C:
你知道,我喜欢简单明了……这就是我在C中的用法:
-
Trivial Variables:
i,n,c
,etc... (Only one letter. If one letter isn't clear, then make it a Local Variable) - 简单变量:我,n、c等……(只有一个字母。如果一个字母不清楚,那么将它设置为局部变量)
-
Local Variables:
lowerCamelCase
- 局部变量:lowerCamelCase
-
Global Variables:
g_lowerCamelCase
- 全局变量:g_lowerCamelCase
-
Const Variables:
ALL_CAPS
- 常量变量:ALL_CAPS
-
Pointer Variables: add a
p_
to the prefix. For global variables it would begp_var
, for local variablesp_var
, for const variablesp_VAR
. If far pointers are used then use anfp_
instead ofp_
. - 指针变量:向前缀添加一个p_。对于全局变量,它是gp_var,对于局部变量p_var,对于const变量p_var。如果使用远指针,则使用fp_而不是p_。
-
Structs:
ModuleCamelCase
(Module = full module name, or a 2-3 letter abbreviation, but still inCamelCase
.) - Structs: ModuleCamelCase (Module = full Module名称,或2-3个字母的缩写,但仍在CamelCase)。
-
Struct Member Variables:
lowerCamelCase
- 结构体成员变量:lowerCamelCase
-
Enums:
ModuleCamelCase
- 枚举:ModuleCamelCase
-
Enum Values:
ALL_CAPS
- 枚举值:ALL_CAPS
-
Public Functions:
ModuleCamelCase
- 公共功能:ModuleCamelCase
-
Private Functions:
CamelCase
- 私有函数:CamelCase
-
Macros:
CamelCase
- 宏:CamelCase
I typedef my structs, but use the same name for both the tag and the typedef. The tag is not meant to be commonly used. Instead it's preferrable to use the typedef. I also forward declare the typedef in the public module header for encapsulation and so that I can use the typedef'd name in the definition.
我定义了我的结构,但是标记和类型定义都使用相同的名称。标签不是常用的。相反,它可以使用typedef。我还在公共模块头中转发声明typedef用于封装,以便在定义中使用typedef名称。
Full struct
Example:
完整的结构的例子:
typdef struct TheName TheName;
struct TheName{
int var;
TheName *p_link;
};
#9
0
You should also think about the order of the words to make the auto name completion easier.
您还应该考虑单词的顺序,以便自动完成名称。
A good practice: library name + module name + action + subject
一个好的实践:库名+模块名+动作+主题
If a part is not relevant just skip it, but at least a module name and an action always should be presented.
如果某个部分不相关,就跳过它,但至少应该显示模块名和操作。
Examples:
例子:
- function name:
os_task_set_prio
,list_get_size
,avg_get
- 函数名:os_task_set_prio, list_get_size, avg_get。
- define (here usually no action part):
OS_TASK_PRIO_MAX
- 定义(这里通常没有动作部分):OS_TASK_PRIO_MAX
#10
0
There could be many, mainly IDEs dictate some trends and C++ conventions are also pushing. For C commonly:
可能有很多,主要是ide指示了一些趋势,c++约定也在推动。一般对于C:
- UNDERSCORED_UPPER_CASE (macro definitions, constants, enum members)
- UNDERSCORED_UPPER_CASE(宏定义,常量,enum成员)
- underscored_lower_case (variables, functions)
- underscored_lower_case(变量、函数)
- CamelCase (custom types: structs, enums, unions)
- CamelCase(定制类型:structs, enums, union)
- uncappedCamelCase (oppa Java style)
- uncappedCamelCase(哥哥Java风格)
- UnderScored_CamelCase (variables, functions under kind of namespaces)
- UnderScored_CamelCase(变量,名称空间下的函数)
Hungarian notation for globals are fine but not for types. And even for trivial names, please use at least two characters.
匈牙利符号表示全局变量是可以的,但是表示类型就不行。即使是微不足道的名字,也请至少使用两个字符。
#1
91
The most important thing here is consistency. That said, I follow the GTK+ coding convention, which can be summarized as follows:
这里最重要的是一致性。也就是说,我遵循GTK+编码约定,可以总结如下:
- All macros and constants in caps:
MAX_BUFFER_SIZE
,TRACKING_ID_PREFIX
. - 所有宏和常量都在大写:MAX_BUFFER_SIZE、TRACKING_ID_PREFIX中。
- Struct names and typedef's in camelcase:
GtkWidget
,TrackingOrder
. - camelcase中的Struct名称和typedef: GtkWidget, TrackingOrder。
- Functions that operate on structs: classic C style:
gtk_widget_show()
,tracking_order_process()
. - 在structs上操作的函数:经典C样式:gtk_widget_show(), tracking_order_process()。
- Pointers: nothing fancy here:
GtkWidget *foo
,TrackingOrder *bar
. - 指针:这里没什么特别的:GtkWidget *foo, TrackingOrder *bar。
- Global variables: just don't use global variables. They are evil.
- 全局变量:不要使用全局变量。他们是邪恶的。
- Functions that are there, but shouldn't be called directly, or have obscure uses, or whatever: one or more underscores at the beginning:
_refrobnicate_data_tables()
,_destroy_cache()
. - 在那里的函数,但不应该直接调用,或者有一些模糊的用途,或者其他的:在开始时一个或多个下划线:_refrobnicate_data_tables(), _destroy_cache()。
#2
25
"Struct pointers" aren't entities that need a naming convention clause to cover them. They're just struct WhatEver *
. DON'T hide the fact that there is a pointer involved with a clever and "obvious" typedef. It serves no purpose, is longer to type, and destroys the balance between declaration and access.
“结构指针”不是需要命名约定子句来覆盖它们的实体。它们只是结构而已。不要隐藏一个事实,即有一个指针与一个聪明而“明显”的typedef有关。它没有任何用途,只是更长时间地输入,并破坏声明和访问之间的平衡。
#3
12
Well firstly C doesn't have public/private/virtual functions. That's C++ and it has different conventions. In C typically you have:
首先,C没有公共/私有/虚拟函数。这是c++,它有不同的惯例。在C语言中,你通常有:
- Constants in ALL_CAPS
- 常数ALL_CAPS
- Underscores to delimit words in structs or function names, hardly ever do you see camel case in C;
- 用下划线分隔结构或函数名中的单词,在C中几乎看不到驼峰情况;
- structs, typedefs, unions, members (of unions and structs) and enum values typically are in lower case (in my experience) rather than the C++/Java/C#/etc convention of making the first letter a capital but I guess it's possible in C too.
- struct、typedefs、union、members (union和struct的成员)和enum值通常是小写的(根据我的经验),而不是使用c++ /Java/ c#等约定将第一个字母作为大写,但我想在C中也是可能的。
C++ is more complex. I've seen a real mix here. Camel case for class names or lowercase+underscores (camel case is more common in my experience). Structs are used rarely (and typically because a library requires them, otherwise you'd use classes).
c++是更复杂的。我在这里看到了真正的混合。类名或小写+下划线的驼色格(驼色格在我的经验中更常见)。结构很少被使用(通常是因为库需要它们,否则就会使用类)。
#4
8
I would recommend against mixing camel case and underscore separation (like you proposed for struct members). This is confusing. You'd think, hey I have get_length
so I should probably have make_subset
and then you find out it's actually makeSubset
. Use the principle of least astonishment, and be consistent.
我建议不要混合使用驼色大小写和下划线分隔(就像您对struct成员所建议的那样)。这是令人困惑的。你可能会想,嘿,我有get_length所以我应该有make_子集然后你发现它实际上是makeSubset。使用最少惊讶的原则,并保持一致。
I do find CamelCase useful to type names, like structs, typedefs and enums. That's about all, though. For all the rest (function names, struct member names, etc.) I use underscore_separation.
我发现CamelCase对于输入名称非常有用,比如struct、typedefs和enums。不过,这是所有。对于所有的rest(函数名、结构成员名称等),我使用了underscore_separation。
#5
8
Coding in C#, java, C, C++ and objective C at the same time, I've adopted a very simple and clear naming convention to simplify my life.
同时使用c#、java、C、c++和objective C编写代码,我采用了一个非常简单明了的命名约定来简化我的生活。
First of all, it relies on the power of modern IDEs (such as eclipse, Xcode...), with the possibility to get fast information by hovering or ctrl click... Accepting that, I suppressed the use of any prefix, suffix and other markers that are simply given by the IDE.
首先,它依赖于现代ide的强大功能(如eclipse、Xcode…),可以通过悬停或ctrl单击来获取快速信息。接受这一点,我禁止使用IDE中简单给出的前缀、后缀和其他标记。
Then, the convention:
然后,该公约:
- Any names MUST be a readable sentence explaining what you have. Like "this is my convention".
- 任何名字必须是一个可读的句子来解释你所拥有的。比如"这是我的惯例"
- Then, 4 methods to get a convention out of a sentence:
- THIS_IS_MY_CONVENTION for macros, enum members
- 对于宏、枚举成员的THIS_IS_MY_CONVENTION
- ThisIsMyConvention for file name, object name (class, struct, enum, union...), function name, method name, typedef
- 用于文件名、对象名(类、结构、枚举、联合…)、函数名、方法名、类型定义的ThisIsMyConvention
-
this_is_my_convention global and local variables,
parameters, struct and union elements - this_is_my_convention全局变量和局部变量、参数、结构和联合元素
- thisismyconvention [optional] very local and temporary variables (such like a for() loop index)
- thisismyconvention[可选]非常本地和临时变量(例如for()循环索引)
- 然后,4种方法得到大会的一个句子:THIS_IS_MY_CONVENTION宏,枚举成员ThisIsMyConvention文件名,对象名称(类、结构、枚举、工会…),函数名,方法名,typedef THIS_IS_MY_CONVENTION全球和本地变量、参数、结构和联合元素ThisIsMyConvention(可选的)本地和临时变量(如()循环指数)
And that's it.
就是这样。
It gives
它给
class MyClass {
enum TheEnumeration {
FIRST_ELEMENT,
SECOND_ELEMENT,
}
int class_variable;
int MyMethod(int first_param, int second_parameter) {
int local_variable;
TheEnumeration local_enum;
for(int myindex=0, myindex<class_variable, myindex++) {
localEnum = FIRST_ELEMENT;
}
}
}
#6
5
Here's an (apparently) uncommon one, which I've found useful: module name in CamelCase, then an underscore, then function or file-scope name in CamelCase. So for example:
这里有一个(显然)不常见的,我发现它很有用:CamelCase中的模块名、下划线、CamelCase中的函数名或文件范围名。举个例子:
Bluetooth_Init()
CommsHub_Update()
Serial_TxBuffer[]
#7
2
I'm confused by one thing: You're planning to create a new naming convention for a new project. Generally you should have a naming convention that is company- or team-wide. If you already have projects that have any form of naming convention, you should not change the convention for a new project. If the convention above is just codification of your existing practices, then you are golden. The more it differs from existing de facto standards the harder it will be to gain mindshare in the new standard.
我被一件事搞糊涂了:你计划为一个新项目创建一个新的命名约定。一般来说,您应该在整个公司或团队范围内使用命名约定。如果已经有项目具有任何形式的命名约定,则不应该为新项目更改约定。如果上面的约定只是对现有实践的编纂,那么您就是黄金。它越不同于现有的实际标准,就越难在新标准中获得人们的关注。
About the only suggestion I would add is I've taken a liking to _t at the end of types in the style of uint32_t and size_t. It's very C-ish to me although some might complain it's just "reverse" Hungarian.
关于我要添加的唯一建议是,我喜欢uint32_t和size_t类型末尾的_t。这对我来说很像c,尽管有些人可能会抱怨说它只是“反向”匈牙利语。
#8
2
You know, I like to keep it simple, but clear... So here's what I use, in C:
你知道,我喜欢简单明了……这就是我在C中的用法:
-
Trivial Variables:
i,n,c
,etc... (Only one letter. If one letter isn't clear, then make it a Local Variable) - 简单变量:我,n、c等……(只有一个字母。如果一个字母不清楚,那么将它设置为局部变量)
-
Local Variables:
lowerCamelCase
- 局部变量:lowerCamelCase
-
Global Variables:
g_lowerCamelCase
- 全局变量:g_lowerCamelCase
-
Const Variables:
ALL_CAPS
- 常量变量:ALL_CAPS
-
Pointer Variables: add a
p_
to the prefix. For global variables it would begp_var
, for local variablesp_var
, for const variablesp_VAR
. If far pointers are used then use anfp_
instead ofp_
. - 指针变量:向前缀添加一个p_。对于全局变量,它是gp_var,对于局部变量p_var,对于const变量p_var。如果使用远指针,则使用fp_而不是p_。
-
Structs:
ModuleCamelCase
(Module = full module name, or a 2-3 letter abbreviation, but still inCamelCase
.) - Structs: ModuleCamelCase (Module = full Module名称,或2-3个字母的缩写,但仍在CamelCase)。
-
Struct Member Variables:
lowerCamelCase
- 结构体成员变量:lowerCamelCase
-
Enums:
ModuleCamelCase
- 枚举:ModuleCamelCase
-
Enum Values:
ALL_CAPS
- 枚举值:ALL_CAPS
-
Public Functions:
ModuleCamelCase
- 公共功能:ModuleCamelCase
-
Private Functions:
CamelCase
- 私有函数:CamelCase
-
Macros:
CamelCase
- 宏:CamelCase
I typedef my structs, but use the same name for both the tag and the typedef. The tag is not meant to be commonly used. Instead it's preferrable to use the typedef. I also forward declare the typedef in the public module header for encapsulation and so that I can use the typedef'd name in the definition.
我定义了我的结构,但是标记和类型定义都使用相同的名称。标签不是常用的。相反,它可以使用typedef。我还在公共模块头中转发声明typedef用于封装,以便在定义中使用typedef名称。
Full struct
Example:
完整的结构的例子:
typdef struct TheName TheName;
struct TheName{
int var;
TheName *p_link;
};
#9
0
You should also think about the order of the words to make the auto name completion easier.
您还应该考虑单词的顺序,以便自动完成名称。
A good practice: library name + module name + action + subject
一个好的实践:库名+模块名+动作+主题
If a part is not relevant just skip it, but at least a module name and an action always should be presented.
如果某个部分不相关,就跳过它,但至少应该显示模块名和操作。
Examples:
例子:
- function name:
os_task_set_prio
,list_get_size
,avg_get
- 函数名:os_task_set_prio, list_get_size, avg_get。
- define (here usually no action part):
OS_TASK_PRIO_MAX
- 定义(这里通常没有动作部分):OS_TASK_PRIO_MAX
#10
0
There could be many, mainly IDEs dictate some trends and C++ conventions are also pushing. For C commonly:
可能有很多,主要是ide指示了一些趋势,c++约定也在推动。一般对于C:
- UNDERSCORED_UPPER_CASE (macro definitions, constants, enum members)
- UNDERSCORED_UPPER_CASE(宏定义,常量,enum成员)
- underscored_lower_case (variables, functions)
- underscored_lower_case(变量、函数)
- CamelCase (custom types: structs, enums, unions)
- CamelCase(定制类型:structs, enums, union)
- uncappedCamelCase (oppa Java style)
- uncappedCamelCase(哥哥Java风格)
- UnderScored_CamelCase (variables, functions under kind of namespaces)
- UnderScored_CamelCase(变量,名称空间下的函数)
Hungarian notation for globals are fine but not for types. And even for trivial names, please use at least two characters.
匈牙利符号表示全局变量是可以的,但是表示类型就不行。即使是微不足道的名字,也请至少使用两个字符。