Naming

  • Choosing Names
  • Types
  • Data Objects
  • Macroes, Functions
  • Type Prefixes
  • Type Prefixes For Variable Names
  • General Name Prefixes
  • Symbols Used By Windows-Applications

  • Choosing Names

    1. Names correspond to the purpose of the construct they are applied to.
    2. If possible, names are never abbreviated. If they are too long (compiler refuses the name or cannot tell apart), meaningful and mnemonic abbreviations may be used.
      Counter-example: is 'tmt' used for transmit or timeout?.
      Exception: ‘historically grown’ standard terms ('rcv', 'snd', etc.)
    3. Underscores are forbidden within names. They are used to seperate module prefixes from identifiers.
      1. Forbidden: Read_Master_Data()
        Allowed:
        ReadMasterData();
        Accnt_ReadMasterData();
    4. Names are chosen principally from the programmers native language. Exceptions are technical terms.
    5. Multi-lingual names (GetStapelEintrag) are strictly forbidden.

      Names or parts of names that have the significance of key words are chosen in english. These exceptions are explicitly pointed out. There are no other exceptions.

    6. If there arise translation problems, common terms are to be collected (in a glossary) and to be defined as a guideline (translated, if neccessary). Those translations are based on technical dictionaries or other documents from professional technical translators.
    7. Abbreviations from external systems or libraries may be used. There is no need to redefine terms like MFC or OWL.

    Types

    1. Names defined with typedef are written in uppercase and may contain underscores. (except structs, classes and pointers to structs or classes).
    1. Names of enumeration types are formed like typedefs. The names of public enumeration types have a module prefix. Their denoter's names begin with a descriptive prefix derived from the enumeration type's name. Since the denoters have the significance of public constants, their names are written in uppercase letters.
      1. typedef enum { F_RED, F_GREEN, F_BLUE } COLOR;
        typedef enum { AC_READONLY, AC_WRITEONLY, AC_RANDOM } MEM_ACCESS;

    2. Names of structs and classes start with an uppercase C and indentify classifictions of nouns. They must not contain underscores. Multiple words are distiguished by using uppercase letters.
      1. typdef struct CComplex // though typedef, mixed case for structs!
        {
            float m_fReal;
            float m_fImag;
        } CComplex;

        class CDatabase;
        class CFastMemory;

        typedef CComplex* PComplex;
        typedef CFastMemory* PFastMemory;

    Data Objects

    1. Variable names always start with a lowercase letter (the module prefix is not part of the name).
    2. Variable names identify nouns. If neccessary, an article and/or adjectives may preceed the noun. (e.g. to fulfill the preceeding demand).
      1. int nUpperLimit;
        CDocument theDocument;
        void* pDataBuffer;

    3. Names of private variables start with an underscore.
      1. int _nTotalNumerOfErrors = 0;

    4. If a public variable is inevitable, its name gets a module prefix.
      1. int* Mmgr_pTheOnlyMemoryPool;

    5. Names of constants created with #define or public declared constants are written in upercase letters and may contain underscores. Names of public constants may also follow this convention.
      1. #define UPPER_LIMIT 1234
        const int UPPER_LIMIT = 1234;
        const int nUpperLimit = 1234;
        const int Stack_nUpperLimit = 1234;

    6. Variables must not be used for different purposes within their scopes.
    7. Variable names start with a type prefix. The actual name starts with an uppercase letter followed by lowercase letters. Multiple words are distiguished by using uppercase letters. Underscores must not be used. Exception: loop variables (i, j, …)
      1. int nFehler;
        int nGesuchterWert;
        LPCSTR lpszBenutzerName;

    8. Names of data elements get the prefix m_.
      1. class CErrorState
        {
            int m_nStateCode;
            LPCSTR m_lpszErrorMessage
        };

    Macroes, Functions

    1. (C) Names of Macroes are written in uppercase letters and may contain underscores.
      1. #define ABS(x) (0 > (x) ? -(x) : (x))

      (C++) Using macroes is inadmissable. Use inline- or template functions instead.

    2. Names of functions start with an uppercase letter, followed by lowercase letters. Uppercase letters are used within compund names. Underscores are not allowed. Names of public functions get a module prefix.
      1. void SearchTree(const PNode pExpressionTree);
        BOOL Msg_MessageIsPresent(void);

    3. Names of functions without a return value or that return a state value start with a verb. Prefer the imperative to the infinitive (wouldn't be a doubt in english, but other languages like German will seduce the programmer to fail).
    4. not TagesdatumSetzen(const PGmsDatum pDatum),
      but SetzeTagesdatum(const PGmsDatum pDatum);
      void SendePaket(const PGmsPaket pPaket);
      ERRORTYPE PruefeName(LPCSTR pName);
    1. Names of functions with a return value start with a noun or with an adjective that describes the returned value.
      1. ALERT NextAlert(const PTIME pStartingTime);
        LPCSTR UserName(const int nUserId);

      and not:

        LPCSTR ReadUserName(int nUserId)!

    2. Names of functions with a boolean return value start with Is or Has:
      1. bool IsValidName(LPCSTR pName);
        bool HasChildren(void);

    3. Names of conversion functions are formed as „Source"ToDestination".
      1. int HexToBinary(LPCSTR lpszHexNumber);

    4. (C, Pascal) An initialization funktion is a function that creates a data structure from the structure's components. Initialization functions are different from conversion functions, because they yield a data structure. Names of initialization functions start with Build. the name starts with New, if a pointer to the new object is returned. The latter return a pointer to the allocated memory, which is to be deallocated by the function's caller by using a deallocation function. Names of deallocation functions start with Delete. These name prefixes have the significance of key words (they correspond to the C++ operators new and delete) and are therefore always written in english.
      1. void BuildTime /* (C) */
            (
                PTime this,
                const int nHours,
                const int nMinutes,
                const int nSeconds
            );

        PTime NewTime
            (
                const int nHours,
                const int nMinutes,
                const int nSeconds
            );

        void DeleteTime(PTime pTime);

      (C++, Delphi) Initialization functions are inadmissable. Use constructors, cast operators and destructors instead (C++: in context with new and delete operators). Namen of functions that allocate heap memory for the returned object, which must be deallocated by the caller, start with New.

    Type Prefixes

    Essential parts of the succeeding tables have been suggested by Microsoft for Windows applications [Petz90].

    Type Prefixes For Variable Names

    prefix

    type

    description

    example

    c char 8-bit character cRank
    b bool boolean bActivated
    q bit bit (C51) qActivated
    n int integer (size depends on operating system) nLength
    l long 32-bit integer signed lOffset
    u UINT unsigned value (size depends on operating system) uCount
    ul unsigned long long unsigned value (depends on operating system) ulRange
    f float 32-bit IEEE floating point fSine
    df double double precision IEEE floating point dfDistance
    p * generic pointer (MS-DOS or Windows up to V3.11: according to memory model) pDoc
    r & references (except const &) rDoc
    sz char[] '\0'-terminated string szName
    psz char* pointer to '\0'-terminated string pszName
    s string Pascal string sName
    1. Instances of userdefined structs or classes may, but are not forced to get a specific prefix.
    2. Variables representing an enum type, get the prefix n, since their sizes usually depend on the (system dependent) size of an integer.
    3. Prefixes as for modules may be useful for user defined classes or structs. This prefix is mentioned in the class header.
      1. struct CConfigurationData;
        CConfigurationData cnfDevice1;

    4. Other system or compiler dependent features that are not mentioned here (like pointers to special memory areas) are to be listed in project specific guidelines.

    General Name Prefixes

    The sample prefix Mod replaces a useful abbreviation of the module name (preferribly 3-5 characters).

    prefix

    type

    example

    C class or Struct CDocumentHandler
    P pointer type for a pointer to class or struct PDocumentHandler
    R reference type for references to a class or struct RDocumentHandler
    m_ data element m_pDocHandler
    a vector (array) caName[]
    Mod_ public funktion or data type (C++, Delphi: except class members) Math_ReadData()
    Math_nErrorCount
    _ private funktion or variable _DateToInteger()
    _lpszProtocol

    Symbols Used By Windows-Applications

    prefix

    type of symbol

    example

    range

    IDR_ identifier to miscallenous resources of different types IDR_MAINFRAME 1…0x6FFF
    IDD_ dialogue resource IDD_SPELLINGCHECKER 1…0x6FFF
    HIDD_ help context of a dialogue resource HIDD_SPELLINGCHECKER 0x20001…0x26FFF
    IDB_ bitmap resource IDB_LOGO 1…0x6FFF
    IDC_ cursor resource IDC_PEN 1…0x6FFF
    IDI_ icon resource IDI_NOTEPAD 1…0x6FFF
    ID_ menu or toolbar command ID_CMD_SPELLINGCHECKER 0x8000…0xDFFF
    HID_ help context of a command HID_CMD_SPELLINGCHECKER 0x18000…0x1DFFF
    IDP_ message box prompt IDP_INVALID_PARTNO 8…0xDFFF
    HIDP_ help context of a message box HIDP_INVALID_PARTNO 0x30008…0x3DFFF
    IDS_ string resource IDS_COPYRIGHT 1…0x7FFF
    IDC_ control of a dialogue box IDC_RECALC 8…0xDFFF


    [continue with "Programming Style Guide"] | [TOC] | [Introduction] | [Layout] | [Programming Style Guide] | [Annexes]


    Copyright © 1997-98 by Uwe Sauerland