Skip to content

Booleans

Joël R. Langlois edited this page Jan 21, 2018 · 4 revisions

Naming Conventions

Function names for bool are to start with a third-person indicative of the word be:

class Foo
{
public:
    bool isRunning() const;
    bool wasDoingSomethingImportant() const;
};

Boolean variables are to favour the same rules above, but in the event of a clashing name the variable must simply be a verb.

class Foo
{
public:
    Foo() :
        running (false),
        completedFirstAction (false),
        completedSecondAction (false)
    {
    }

    bool isRunning() const { return running; }
    bool hasCompletedAllActions() const { return hasCompletedFirstAction && hasCompletedSecondAction; }

private:
    bool running;
    bool hasCompletedFirstAction, hasCompletedSecondAction;
};

General Comparisons

Don't compare directly to true or false as this is implicit knowledge.

if (foo())
    //Do stuff

Use the !-operator to check for false booleans. Insert 1 space after the exclamation point.

if (! bar())
    //Do stuff
Clone this wiki locally