Jim’s C Naming Conventions

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.

Some terminology:

  • camelcaseCamel 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:

  1. 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. seymour
  2. 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. seymour_PerfectHash
  3. 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. seymour_PerfectHash_defaultIfNotFound
  4. 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. Color_blueGreen
  5. Members should be camel case. They should not be prefixed with the package or type name. E.g. numberOfSlots
  6. 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. PerfectHash_staticMethod, seymour_PerfectHash_get, _seymour_PerfectHash_internalMethodCalledByGlobalInlineMethod
  7.  (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. seymour_PerfectHash_Create
  8. 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. SXE_LITERAL_LENGTH

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.

Advertisements

About jimbelton

I'm a software developer, and a writer of both fiction and non-fiction, and I blog about movies, books, and philosophy. My interest in religious philosophy and the search for the truth inspires much of my writing.
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Jim’s C Naming Conventions

  1. Pingback: OOPC (Object Oriented Programming in C) Conventions | Programming with Jim

  2. Pingback: C Programming: Naming Conventions | Jim's Jumbler

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s