I’m going to set down my own preferred naming conventions, which I typically follow in code I’m writing for myself. They have a lot in common with the standards we followed in the SXE project. I’ll note where they differ and will try to provide the rationale for them.
- Camel case means that words within a name are capitalized. Acronyms are not written in all caps. e.g. camelCaseName, ioError (not IOError).
- Pascal case names are the same, but with the initial word capitalized. E.g. TcpPortNumber.
Here are the conventions:
- Package names should be short. For example, SXE uses the package name sxe for most of the functions in libsxe. Package names are important for preventing name space collisions, especially in C. Since C doesn’t have language support for packages, package names are used as prefixes, so you should typically keep them short. SXE has them follow the convention of the name they are prefixing (e.g. SXE_CONSTANT, sxe_variable). I personally prefer them to be single lower case words or to follow the camel case convention. E.g.
- Type names should be Pascal case. SXE used all caps, but that is too hard to type, and makes type names look like macro names, which is confusing. Global type names (defined in a header file) should be prefixed by the package name, with the two separated by an underscore (which takes the place of the “.” that would be used in C++, Python, or Java. E.g.
- Constants and variables should be camel case. If they are global (i.e. included in a header file), they should be prefixed with the package name. E.g.
- Enumerator (as opposed to enumeration type) names should be camel case. They should be prefixed by the enumeration type name, separated from it by an underscore. E.g.
- Members should be camel case. They should not be prefixed with the package or type name. E.g.
- Method names (names of functions that operate on a type) should be camel case, prefixed by the type name, and separated from it by an underscore. Global methods should be prefixed by the package name, but static methods need not be. Methods that need to be private but can’t be made static should be named with a leading underscore. E.g.
- (Non method) function names should be Pascal case. They should be prefixed with the package name (separated from it by an underscore) if global. Functions that need to be private but can’t be made static should be named with a leading underscore. e.g.
- Macro names (parametrized or not) should be in all caps. If they are global, they should be prefixed by the package name (either Pascal cased or all capped), separated from it with an underscore. e.g.
A note on macros: they should be avoided. Use constants, enumerations, and inline functions instead if possible. The evils of macros and the ways to avoid them is a full blog post in itself. Please comment if you’d be interested in seeing one.