标签、函数的命名约定 [英] Naming convention for labels, functions

查看:50
本文介绍了标签、函数的命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点困惑,但仍然没有找到明确的答案:我应该将标签命名为label1"还是下划线_label1"?最流行和最受认可的方式是什么?如果我没记错的话,我什至见过一个带有点.label1"的.

I'm confused a bit and still haven't found an explicit answer: should I name labels as just "label1" or with underscore "_label1"? What's the most popular and recognizable way? I've even seen one with dot ".label1" if I remember correctly.

同样的问题适用于函数:function1"还是_function1"?

The same question goes for functions: "function1" or "_function1"?

推荐答案

从代码可维护性的角度来看,前导点和下划线不会向标签添加任何有用的信息,因此如果没有特别需要这些前缀(请参阅下面),你应该摆脱它们.您可能认为它看起来很花哨"并不足以成为使用它们的理由.

From a code maintainability viewpoint, leading dots and underscores don't add any useful information to a label, so if there is no specific need for those prefixes (see below), you should get rid of them. Just the fact that you may think "it looks fancy" is not enough reason to use them.

汇编器有时使用点来指定标签是本地,即不作为符号导出到目标文件.这对于不使用不需要的符号污染全局名称空间很有用.

Dots are sometimes used by assemblers to specify that a label is local, i.e. not to be exported as a symbol to the object file. This can be useful in order to not pollute the global name space with symbols you do not need.

其他汇编程序使用点来表示汇编指令,并且根本不允许在符号中使用点.

Other assemblers use dots to denote an assembler directive and do not allow dots in symbols at all.

下划线通常由必须与高级语言集成的汇编代码使用.一些 C 编译器在所有导出的符号前面加上下划线,为了能够从 C 代码中调用,汇编函数需要遵循这个规则.

Underscores are typically used by assembler code that has to integrate with higher-level languages. Some C compilers prefix all exported symbols with underscores, and in order to be able to be callable from C code, assembler functions need to follow this rule.

通常,您的汇编程序(和/或编译器,以防您与编译代码集成)手册应该能够告诉您特定的汇编程序支持或需要哪些变体.如果手册没有明确告诉您何时以及为什么应该使用这样的前缀,请不要使用它们 - 汇编器标签是自我记录代码的一个(最小也是最重要的)部分.如果你不需要,不要把它浪费在无意义的前缀上.

Generally, your assembler (and/or compiler, in case you integrate with compiled code) manual should be able to tell you what variations your specific assembler supports or requires. And if the manual doesn't explicitly tell you when and why you should use such prefixes, simply don't use them - Assembler labels are one (the minimal and most important) part of self-documenting your code. Don't waste that for meaningless prefixes if you don't need to.

这篇关于标签、函数的命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆