Boost logo

Boost :

Subject: Re: [boost] code naming conventions
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2016-03-06 18:03:48


On 2016-03-06 22:38, Michael Marcin wrote:
> 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.

[snip]

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

   // type takes the most natural name
   enum class format {
       rgba,
       rgb,
       floating_point, // avoid reserved keywords when possible
       float_ // ...or add a trailing underscore when not
   };

   class image {
   public:
       // use a verb to form method names
       format get_format() const noexcept
       { return m_format; }

       // typically use an abbreviation for argument
       // and local variable names
       void set_format( format fmt ) noexcept
       { m_format = fmt; }
   private:
       // use m_ prefix for member variables
       format m_format;
   };

   format bits_to_format( int bits )
   {
       format fmt = format::rgba;
       if ( bits == 24 ) {
           fmt = format::rgb;
       }
       return fmt;
   }

   // I used to use the T suffix for typename template parameters
   template< typename FormatT >
   std::ostream& print_format( std::ostream&, FormatT fmt );

   // ...and V for non-type template parameters, but I'm in the
   // process of dropping it
   template< format FormatV >
   std::ostream& print_format( std::ostream& );

   // I'm not using concepts but I guess the suffixes would work
   template< Format FormatT >
   std::ostream& print_format( std::ostream&, FormatT fmt );


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