什么命名约定一个C API [英] What naming convention for a C API

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

问题描述

我们正在对C语言编写的一个游戏引擎,目前我们使用以下命名约定。

We are working on a game engine written in C and currently we are using the following naming conventions.

ABClass object;
ABClassMethod(object, args)

AB 作为我们的preFIX。

AB Being our prefix.

我们的API,即使在对象上工作,不具备继承,多态或任何东西。我们所有的只是数据类型和方法对他们的工作。

Our API, even if working on objects, does not have inheritance, polymorphism or anything. All we have is data types and methods working on them.

我们的常量都命名为: AB_ConstantName 和preprocessor宏被命名为喜欢 AB_API_BEGIN 。我们不使用的功能像宏。

Our Constants are named alike: AB_ConstantName and Preprocessor macros are named like AB_API_BEGIN. We don't use function like macros.

我不知道如何,这是一个C API配件。此外,您可能注意到,整个API是包装成 LUA ,你既可以使用从 C中的API LUA 。大部分时间,发动机将从LUA被使用。

I was wondering how this was fitting as a C API. Also, you may note that the entire API is wrapper into lua, and you can either use the API from C or lua. Most of the time the engine will be used from lua.

推荐答案

无论API,你会拿出,为您的用户心理的理智(和你的),保证它的整个code一致。

Whatever the API you'll come out with, for your users' mental sanity (and for yours), ensure that it's consistent throughout the code.

一致性,对我来说,包括三件事情:

Consistency, to me, includes three things:


  1. 命名即可。表壳和使用下划线应该加以规范。例如:ABClass()是一个公的象征,而AB_Class()不是(在这个意义上,它的可能的是可见的(无论何种原因)给其他模块,但它保留供内部使用<。 BR>
    如果你有ABClass(),你永远不应该有abOtherClass()或AbYet_anotherClass()

  1. Naming. Case and use of the underscore should be regulated. For example: ABClass() is a "public" symbol while AB_Class() is not (in the sense that it might be visible (for whatever reason) to other modules but it's reserved for internal use.
    If you have "ABClass()", you should never have "abOtherClass()" or "AbYet_anotherClass()"

名词和动词。如果事情被称为点必须的总是的是点,而不是PNT或P或类似的。结果
标准C库,例如,既有putc将()和的putchar()(是的,他们的的不同,但名称不知道哪个人写在标准输出上)。
也动词应该是一致的:避免具有CreateNewPoint(),BuildCircle()和NewSquareMake()的同时

Nouns and verbs. If something is called "point" it must always be "point" and not "pnt" or "p" or similar.
Standard C library, for example, has both putc() and putchar() (yes, they are different but the name doesn't tell which one writes on stdout). Also verbs should be consistent: avoid having "CreateNewPoint()", "BuildCircle()" and "NewSquareMake()" at the same time!

参数位置即可。如果一组相关功能的采用类似的参数(如字符串或文件),确保它们具有相同的地位。再次C标准库做的fwrite()和fprintf()的不好:一是具有文件作为最后一个参数,其他作为第一个

Argument position. If a set of related function takes similar arguments (e.g. a string or a file) ensure they have the same position. Again the C standard library do a poor job with fwrite() and fprintf(): one has the file as the last argument, the other as the first one.

剩下的就是多到你的口味,你可能有其他的限制。

The rest is much up to your taste and any other constraint you might have.

例如,你提到你正在使用的Lua:继约定类似于Lua的人可以加分,如果程序员同时暴露于这两种API

For example, you mentioned you're using Lua: Following a convention that is similar to the Lua one could be a plus if programmers have to be exposed to both API at the same time.

这篇关于什么命名约定一个C API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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