Naming Convention: Difference between revisions

From neoGFX
Jump to navigation Jump to search
No edit summary
No edit summary
Line 8: Line 8:
! Construct !! Convention !! Example !! Note
! Construct !! Convention !! Example !! Note
|-
|-
| macro || UPPER_SNAKE_CASE || <code>MY_MACRO</code> || snake_case is used for in-class ''event definition'' macros
| macro || UPPER_SNAKE_CASE || <code>FOO_BAR</code> || snake_case is used for in-class ''event definition'' macros
|-
|-
| function name || snake_case || <code>my_function</code>
| function name || snake_case || <code>foo_bar</code>
|-
|-
| struct/class name || snake_case || <code>my_class</code>
| struct/class name || snake_case || <code>foo_bar</code>
|-
|-
| interface class name || snake_case; <code>i_</code> prefix || <code>i_my_interface</code> || an ''interface class'' does not have any member variables, containing only pure virtual functions and any associated helper functions
| interface class name || snake_case; <code>i_</code> prefix || <code>i_foo_bar</code> || an ''interface class'' does not have any member variables, containing only pure virtual functions and any associated helper functions
|-
|-
| enum class name || snake_case || <code>my_enum</code>
| enum class name || snake_case || <code>foo_bar</code>
|-
|-
| enum name || snake_case; <code>_e</code> suffix || <code>my_enum_e</code> || C-style enums
| enum name || snake_case; <code>_e</code> suffix || <code>foo_bar_e</code> || C-style enums
|-
|-
| enumerator || UpperCamelCase || <code>MyEnumerator</code>
| enumerator || UpperCamelCase || <code>FooBar</code>
|-
|-
| function parameter || CamelCase; <code>a</code> prefix || <code>aMyVariable</code> || "a" for ''argument''
| function parameter || CamelCase; <code>a</code> prefix || <code>aFooBar</code> || "a" for ''argument''
|-
|-
| local variable || lowerCamelCase || <code>myVariable</code>
| local variable || lowerCamelCase || <code>fooBar</code>
|-
|-
| member variable (class) || CamelCase; <code>i</code> prefix || <code>iMyVariable</code> || "i" for class ''instance'' variable
| member variable (class) || CamelCase; <code>i</code> prefix || <code>iFooBar</code> || "i" for class ''instance'' variable
|-
|-
| member variable (struct) || lowerCamelCase || <code>myVariable</code> || use <code>struct</code> for classes that don't have a ''class invariant''
| member variable (struct) || lowerCamelCase || <code>fooBar</code> || use <code>struct</code> for classes that don't have a ''class invariant''
|-
|-
| variable with static storage duration || CamelCase; <code>s</code> prefix || <code>sMyVariable</code>
| variable with static storage duration || CamelCase; <code>s</code> prefix || <code>sFooBar</code>
|-
|-
| variable with thread local storage duration || CamelCase; <code>t</code> prefix || <code>tMyVariable</code>
| variable with thread local storage duration || CamelCase; <code>t</code> prefix || <code>tFooBar</code>
|-
|-
| event object || UpperCamelCase || <code>MyEvent</code>
| event object || UpperCamelCase || <code>FooBar</code>
|-
|-
| event (virtual) getter function || snake_case || <code>my_event</code>
| event (virtual) getter function || snake_case || <code>foo_bar</code>
|}
|}



Revision as of 19:47, 5 January 2026

The neoGFX naming convention is based on the idea that to aid readability of C++ code it is better to encode scope rather than type in an identifier name prefix. The idea of encoding scope in identifier names was introduced by the smartphone operating system Symbian OS.

The following table lists the various C++ syntactical constructs with their associated convention:

neoGFX Naming Convention
Construct Convention Example Note
macro UPPER_SNAKE_CASE FOO_BAR snake_case is used for in-class event definition macros
function name snake_case foo_bar
struct/class name snake_case foo_bar
interface class name snake_case; i_ prefix i_foo_bar an interface class does not have any member variables, containing only pure virtual functions and any associated helper functions
enum class name snake_case foo_bar
enum name snake_case; _e suffix foo_bar_e C-style enums
enumerator UpperCamelCase FooBar
function parameter CamelCase; a prefix aFooBar "a" for argument
local variable lowerCamelCase fooBar
member variable (class) CamelCase; i prefix iFooBar "i" for class instance variable
member variable (struct) lowerCamelCase fooBar use struct for classes that don't have a class invariant
variable with static storage duration CamelCase; s prefix sFooBar
variable with thread local storage duration CamelCase; t prefix tFooBar
event object UpperCamelCase FooBar
event (virtual) getter function snake_case foo_bar

Example class:

class foo
{
public:
   foo(int aValue) : iValue{ aValue }
   {
   }
public:
   int value() const
   {
       return iValue;
   }
   void something()
   {
       thread_local qux tQux;
       /* ... code ... */
   }
private:
   int iValue;
};