Boost logo

Boost :

Subject: [boost] code naming conventions
From: Michael Marcin (mike.marcin_at_[hidden])
Date: 2016-03-06 14:38:00


I've written a lot of code in the boost/standard library style.
I've become increasingly annoyed at naming my variables when coding in
this style.

Looking to the standard library implementations for guidance isn't much
help as they often use conventions like local variables are named with a
leading double underscore and member variables are named with a leading
underscore and capital letter. I.E. things I'm not allowed to do as I'm
not a standard library implementer.

Good names are generally taken by types leading to distress when trying
to name objects and functions.

Here's a concrete example:

// type takes the most natural name
enum class format {
     rgba,
     rgb,
     float // enum value wants reserved name
};

class image {
public:
     // getter wants the same name
     format format() const noexcept
     { return this->format; }

     // setter wants the same name
     void format( format format ) noexcept
     // parameter wants the same name
     { this->format = format; }
private:
     // member variable wants the same name
     format format;
};

format bits_to_format( int bits )
{
     // local variable wants the same name
     format format = format::rgba;
     if ( bits == 24 ) {
         format = format::rgb;
     }
     return format;
}

// unrestricted template parameter does *not* want the same name
template< typename Format >
std::ostream& print_format( std::ostream&, Format format );

// Concept template parameter *does* want the same name
template< Format Format >
std::ostream& print_format( std::ostream&, Format format );

What I end up doing right now is something like:

// type takes the most natural name
enum class format {
     rgba,
     rgb,
     float_ // add trailing underscore to reserved name
};

class image {
public:
     // prefix getter with get_
     format get_format() const noexcept
     { return _format; }
     // prefix setter with get_
     void set_format( format format_ ) noexcept
     // trailing underscore on parameter
     { _format = format_; }
private:
     // leading underscore on member
     format _format;
};

format bits_to_format( int bits )
{
     // cutesy abbreviation for local variable
     format fmt = format::rgba;
     if ( bits == 24 ) {
         fmt = format::rgb;
     }
     return fmt;
}

template< typename Format >
std::ostream& print_format( std::ostream&, Format format_ );

// cutesy abbreviation for constrained template type
template< Format Fmt >
std::ostream& print_format( std::ostream&, Fmt format_ );

Having to come up with cutesy abbreviations for every local variable
that are still meaningful is especially distracting.
Surely enough code has been written that this has been solved.

I looked through several boost library implementations but didn't see
anything that stuck out as a clear convention.

Adobe's ASL library seems to append _t postfix to type names and _m
postfix to member variables.
They also use very terse names for template parameter type names as well
as parameter names and local variables. Like in:

template <typename I, // I models ForwardIterator
           typename N, // N models IntegralType
           typename T, // T == result_type(P)
           typename C, // C models StrictWeakOrdering(T, T)
           typename P>
// P models UnaryFunction(value_type(I)) -> T
I lower_bound_n_(I f, N n, const T& x, C c, P p) {
     while (n != 0) {
         N h = n >> 1;
         I m = boost::next(f, h);

         if (c(p(*m), x)) {
             f = boost::next(m);
             n -= h + N(1);
         } else {
             n = h;
         }
     }
     return f;
}

What's your preferred convention?
If possible please answer by modifying the initial concrete example and
providing annotations.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk