Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagecpp
// C code
typedef struct
{
    int bar;
} FooState;                // OK

typedef enum
{
    FooErrInvalidMoonPhase,
    FooErrUnmetExpectations
} FooErrors;               // OK

struct FooData_t           // Wrong
{
    double baz;
};

Functions

Function names should be English verbs. Usage of nouns or adjectives is prohibited, e.g. distance() is not allowed, but computeDistance() is.

Function names and method names are camelCased, the first character must be lowercase. If a function name contains an acronym, the acronym itself should be capitalized, e.g., beginUSBTransfer().

...

Code Block
languagecpp
/*
 * Copyright (c) 2014 Zubax Robotics, zubax.com
 * Distributed under the MIT License, available in the file LICENSE.
 * Author: Pavel Kirienko <pavel.kirienko@zubax.com>
 */

#pragma once

#include <cmath>

namespace foo
{


using Scalar = double;

class [[nodiscard]] Point
{
    doubleScalar x_ = 0.0;       // Note default initialization
    doubleScalar y_ = 0.0;       // ditto

public:
    Point() {= }default;

    Point(doubleScalar arg_x, doubleScalar arg_y) :
        x_(arg_x),
        y_(arg_y)
    { }

    double distance[[nodiscard]] Scalar computeDistanceTo(const Point& other) const
    {
        const auto dx = x_ - other.x_;    // Note const
        const auto dy = y_ - other.y_;    // ditto
        return std::sqrt(dx * dx + dy * dy);
    }

    int compareX(const Point& other) const
    {
        if (x_ < other.x_)
        {                                  // Note braces
            return -1;
        }
        else if (x_ > other.x_)
        {
            return 1;

       }
        else
        {
            return 0;
        }
    }
};

inline void foo(intstd::int32_t& inout_bar)
{
    switch (inout_bar)
    {
    case 0:
    {
        ++inout_bar;
        break;
    }
    case 1:
    {
        --inout_bar;
        // FALLTHROUGH
    }
    default:
    {
        inout_bar += inout_bar;
        break;
    }
    }
}

} // namespace foo

...