Style Guide For Version 0.400
Welcome To CIDCorp
Home of the CIDLib Class Libraries


Basic Formatting

This section covers the simple formatting conventions, such as tab usage, comma placement, whitespace, wrapping, etc... Some folks probably consider this kind of stuff to be a sign of major anal retentiveness, but I consider it quite important and follow it to a letter whenever possible.
Line Length
CIDLib code tries to stick strictly within the 80 column limit though it might occasionally stray over by a character or two. But nothing more than that.
Tabs
Hard tabs are used, with 4 spaces being inserted for a tab character. Soft tabs save space but cause inconsistent results when displayed, and they make it difficult to backspace quickly without making mistakes.
White Space
Generally, all operators and their operands are separated by spaces for easier Ctrl-Arrow movement through a line of text. There are no requirements for spaces before or after opening or closing braces or parentheses. Formulas are always explicitly parenthesized, and should not depend upon the language specific evaluation rules.
Wrapping Lines
Function parameters are wrapped in ways that might seem peculiar to you at first. They tend to take the 'scope block' approach to wrapping, and use the 'leading' style for operators, commas, logical operations, etc...

Here are some examples from the actual code.

In this case, values of some sort are being returned. The first is a cast of a set of enums into a single enum value. The second is a function call that returns a value and has enough parameters that it must be wrapped.

    // Cast a set of bit mapped enums flags
    tCIDLib::eFooSet eSet = tCIDLib::eFooSet
    (
        tCIDLib::eFooSet_Flag1
        | tCIDLib::eFooSet_Flag2
        | tCIDLib::eFooSet_Flag3
    );

    tCIDLib::TFloat8 f8Tmp = 0.5 - _f8Noise
    (
        f8X * 4 * f8Freq
        , f8Y * 4 * f8Freq
        , f8Z * 4 * f8Freq
    );
In this case, when some kind of operator is used in a compound statement and it cannot be put on one line without violating the column widget rules, it will be wrapped so that the logical operators are under the if and the indentation of the parts of the statement indicate the nesting.
    if ((f8NoiseVal >.= pbandCur->f8StartVal)
    &&  (f8NoiseVal <.= pbandCur->f8EndVal))
    {
        blah blah blah
    }
Note that those methods below that have parameters are wrapped around somewhat like an if statement. They use leading commas and everything is columnarized exactly the same.
    class   TWndBorder : public TCIDObject, public MDuplicable
    {
        public  :
            // ------------------------------------------------------
            //  Constructors and Destructors
            // ------------------------------------------------------
            TWndBorder();

            TWndBorder
            (
                const   TWndBorder&             wbordToCopy
            );

            ~TWndBorder();

            // ------------------------------------------------------
            //  Public, virtual methods
            // ------------------------------------------------------
            virtual tCIDLib::TCard4 c4Width() const = 0;

            virtual tCIDLib::TVoid Depress() = 0;

            virtual tCIDLib::TVoid DrawBorder
            (
                const   TWindow&                wndTarget
                ,       TPaintBrush&            ptbToUse
                , const TArea&                  areaBorder
            )   = 0;

            virtual tCIDLib::TVoid Raise() = 0;
    };
This case shows how class initializers are wrapped in the constructors for all classes. They are always wrapped even if they could possibly fit onto the same line as the function parameters. The parameters are strictly columnarized, and both the parameters and the initializers use the leading style commas.
    TBandItem::TBandItem(   const   tCIDLib::TFloat8 f8End
                            , const tCIDLib::TFloat8 f8Start
                            , const TFRGBClr&        frgbEnd
                            , const TFRGBClr&        frgbStart) :
        f8EndVal(f8End)
        , f8StartVal(f8Start)
        , frgbEndClr(frgbEnd)
        , frgbStartClr(frgbStart)
    {
    }

Naming Conventions

This section covers the conventions for the naming of variables, constants, functions, types, etc... This is strictly followed in order to gain maximum consistency and self documentation.
* Note that the naming conventions for the files that make up a facility (a Dll or Exe) are convered in a separate section since they are a subject unto themselves. This section just covers the contents of those files.
General Philosophy
The names of almost everything should mixed case. Mixed case is more compact as fixed case names with underscores, and just as readable (and much more attractive in my opinion.) Names should not be abbreviated unless the length would be burdensome. If abbreviated, then consistency is the selection of abbreviations is very important.
Type Names
There are just a few rules for types names. The only real exception to the general rules are classes used for multiple inheritance and template classes. Here are the rules:
  • All type names start with a 'T' as the first letter. This includes class names as well as fundamental types.
  • 'Mix In' classes, classes designed for use via multiple inheritance, all start with a 'M' as the first letter.
  • Class names should be non-trivial and descriptive, though 64 characters or less.
  • Template classes end with a '_T' to indicate their special status.
  • Enumeration types start with an 'e' as their first character, as do the values that make up the enumeration. The name of the enumeration should be plural if it represents multiple values (as it usually does.) Each value of the enumeration should use a single version of the type name as a prefix.
  • All types names should be placed within a namespace so as to avoid pollution of the global space and prevent clashes (where possible with the development environment at hand.)
Here are some examples:
    //
    //  Enum type names are plural, and the values themselves
    //  use a singular version (though sometimes abbreviated) of
    //  the name as a prefix.
    //
    enum eFooCounters
    {
        eFooCounter_Burgers
        , eFooCounter_Chips
        , eFooCounter_Coke
    };

    //
    //  Here are some standard CIDLib classes used to create some
    //  legally named objects.
    //
    TArea           areaWindow;
    TPoint          pntOrigin;
    TBag_T<.TPoint>.  colOfPoints;

    // Create a class derived from the base object and a mixin    
    class TFoo : public TCIDObject, public MStreamable
    {
        // blah blah blah
    };
Variable Names
Variables (whether fundamental or object) always begin with their respective 'family hungarian prefix'. The prefix is followed by a mixed case name. The prefix for each class is noted in the header file and in the class reference for that class.

I call it a 'family prefix' because generally all classes derived from some basic class will use the base class' prefix (because all classes derived from it are really just variations of that basic type.) For instance, all windows use 'wnd' and call graphics bundles use 'gbun' etc...

Here are some examples:

    tCIDLib::TCard4     c4FooCount;
    tCIDLib::eMonths    eThisMonth(tCIDLib::eMonth_January);
    TArea               areaOfFoo(10, 10, 100, 200);

Class Member Guidelines

This section describes the stylistic rules followed by CIDLib for all of the methods and members of a class. It follows these rules quite strictly.

Unless there is a major justification to do so, all data members are to be private. The only exception is that it is acceptible to have public const static data members (unless you with to hide the representation of that member of course.) Very occasionally protected members might be used so that derived classes have high speed access to the data.

Order of Declaration

Class members are always in the same order. The order of the implementation within the Cpp files follows the same ordering as the order of declaration within the Hpp file. There might always be the occasional special case that is not covered here.

  • Public Section
    • Public, class specific types
    • Public static data members.
    • Public constructors and destructors
    • Public operators
    • Public, inherited methods
    • Public, virtual methods
    • Public, non-virtual methods
  • Protected Section
    • Friends and protected class specific types
    • Hidden constructors and operators
    • Protected, inherited methods
    • Protected, virtual methods
    • Protected, non-virtual methods
    • Protected data members (should be rare)
  • Private Section
    • Unimplemented constructors and operators
    • Private, inherited methods
    • Private, virtual methods
    • Private, non-virtual methods
    • Private data members

Naming Conventions

Class members have slightly different naming conventions that other nameable entities, because of their special place in our hearts :-)

  • Public methods and members have no special naming conventions. They just follow the normal variable naming conventions.
  • Protected methods and members have a single underscore, unless they cannot have one due to conflicts with the languge (such as hidden constructors and operators.)
  • Private members have two leading underscores.
  • The 'virtual' keyword is applied only to a method when it is 'first' declared, i.e. in the lowest level class that implements it. When a class overrides a method, it is put into the section that denotes it is an inherited method, but it is not given the virtual keyword explicitly.

Inlines

This section covers the special issues involved with inlines, and what the range of options are. Inlines are somewhat of a necessary evil with lots of special considerations, which are different according to the kind of code you are writing. If you are writing your own code, and recompiling the code base when an inline changes is easy, then the binary compatibility worries do not apply. If you are writing code for others to use, then you must be more careful. So CIDLib must be careful, but an exe based on it would not be in trouble because only it is affect by changes to itself, and can just be recompiled as needed.
  1. Stylistically, inlines should conform to the same format as it would if written out of line.
  2. All inlines should be done outside of the class definition itself. They should generally just be done at the bottom of the header.
    * This was not historically observed in CIDLib and is currently being rectified in the remaining places its not done this way.
  3. Only very trivial operations should be done as inlines. Very occasionally a more non-trivial operation can be justified, when its called often but from many places and the performance considerations are paramount.
  4. Keep in mind that inlines are compiled into client code and cannot generally be changed without recompilation of all code that depends upon it.
  5. There are situations where, once some other situation has forced you to expose some implementation detail (to be frowned on, but let's be realistic), you might be able to inline some other code without further penalty if a useful performance gain could be had. Once you've paid the devil, gets what's coming to you :-)

File, Function, Class Headers

CIDLib uses a consistent set of headers for all files, the methods and functions in those files, and for classes. They are used consistently and even though its a pain for me. Eventually, I'm going to write a small parser that will parse these headers and generate documentation for me.
File Header
    //
    // NAME: 
    //
    // DESCRIPTION: 
    //
    //
    // AUTHOR: 
    //
    // CREATE DATE: 
    //
    // COPYRIGHT: 1992..1996, 
    //
    // CAVEATS/GOTCHAS: 
    //
    // MODIFICATION LOG: 
    //
Function/Method Header
    //
    // FUNCTION/METHOD NAME:
    //
    // DESCRIPTION:
    //
    // ---------------------------------------
    //   INPUT: None
    //
    //  OUTPUT: None
    //
    //  RETURN: None
    //
Class Header
    // -------------------------------------------------------------------
    //   CLASS: 
    //  PREFIX: 
    // -------------------------------------------------------------------

General Guidelines

This section gives some common advice that is followed by all CIDLib code and recommended for user code based upon it. These are just quick reminders. These concepts will be discussed in more depth in other places.
  1. All classes should either explicitly declare default and copy constructors and assignment operators, or hide them by explicitly declaring them in the private section of the class and not implementing them.
  2. Make all method and function parameters const that can be const and still have the method or function do what it needs to do. This applies even to by value fundamental values. Even though these parameters are copied onto the stack the conceptual consistency is important. And it insures that input parameters are always the same throughout the life of the function or method. Not realizing that a parameter has been used as a temporary variable can be a source of maintenance errors.
  3. In even more general terms, use const wherever it is possible without constricting the code such that it cannot do what it is supposed to. Const can be used in many places and it catches sooo many errors that would otherwise silently occur.
  4. Avoid global objects. Global objects can be a source of very obscure bugs. They can be used (particularly objects of very fundamental class types) but thought should be given to another means of implementation. Some obvious exceptions are things like semaphores which must exist before any code can attempt to lock them, ect... In particular don't have any global object depend upon the existence of any other global object (unless that other object is in an underlying Dll, in which case its constructor should have already occured.)
  5. When possible, always write reentrant code. Just always think that way. However, that does not mean making every single object multi-thread safe. Doing so it just too much of a burden for most apps. This is an important design tightrope to walk.
  6. It is acceptible to return references or pointers to members of an object if it is necessarily; however, they should return constant pointers or references when possible. Try to provide const and non-const versions of methods or operators that do this, which will allow the compiler to pick the most restrictive one possible for the circumstances at hand.
  7. Unless very difficult to do otherwise, always design classes so that they have a reasonable default construction. This is required if the class is streamable (though it can be a protected constructor.)
  8. Unless very difficult to do otherwise, always fully construct objects during construction. The only exception to this in CIDLib is windows. Windows are first constructed then created. This avoids a big problem with virtual methods during construction.
  9. When possible, prefer the use of janitors over the use of try/catch blocks.
  10. Use pure exception based error handling always. CIDLib is fundamentally based on the exception scheme and provides lots of janitor types classes to make it safe and effectively for you to do the same.
  11. Where possible, use very simple, 'by value' semantics for classes. Only avoid this when the overhead becomes too high to afford. Classes with by value semantics are very natural to work with and don't cause so many potential memory leak problems.
  12. Only use inlines when it makes good sense, i.e. for very trivial operations such as 'pass through' inlines or 'getter' methods. Understand that an inline is pretty much forever because of binary compatibility issues.

CIDCorp
Dean Roddey
Internet: droddey@ng.netgate.net
CIS: 72170,1614