C Programming: Naming Conventions

naming-conventions.pngNaming conventions are like most standards: the great thing about them is that there are so many to choose from. Most people agree that a project should have one and use it consistently, but there is little agreement on what the rules should be. Here are three possibilities, for example:

Gnome Developer C Coding Style

  1. Macros, constants, and enumerands in all caps: MAX_BUFFER_SIZE
  2. Type names in Pascal case: GtkWidget
  3. Structure names in Pascal case with a leading underscore: _PerfectHash
  4. Functions and member/variable names in lower case with words separated by underscores: gtk_widget_show(), file_name

Microsoft Naming Guidelines

  1. Variables in camel case: leadingWordLowerCase, remainingWordsUpperCase
  2. Namespaces, Types, Methods, Members, and Enumerators in Pascal case: AllWordsUpperCase

Google C++ Style Guide

  1. Type names in Pascal case with no underscores: MyExcitingClass, MyExcitingEnum
  2. Variables and Members in lowercase with underscores between words
  3. Variables declared constexpr or const, and whose value is fixed for the duration of the program, named with a leading “k” followed by mixed case: kDaysInAWeek

  4. Functions in Pascal case with no underscores, capitalizing acronyms as single words (i.e. StartRpc(), not StartRPC())
  5. Namespace names all lower case.
  6. Enumerators (for both scoped and unscoped enumerations) named either like constants or like macros: either kEnumName or ENUM_NAME
  7. Macros upper cased with words separated by underscores: MY_MACRO_THAT_SCARES_SMALL_CHILDREN

My own preferences, which I typically follow in code I’m writing for myself, have a lot in common with the standards we followed in the SXE project. They are now documented on my programming blog: Programming with Jim: Jim’s C Naming Conventions.

 

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 programming and tagged , , , , , , , , . Bookmark the permalink.

Leave a Comment

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