v3 flag enabled
Log In
Log In

Design Best Practices- Blog

Design Best Practices

Use these guidelines as recommendations based on past author’s experiences of preparing content for a library submission.

Overview

When designing a new library:

Source Files

Naming

Use the naming conventions of the C++ Standard Library (See https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#_naming_conventions_rationale">Naming Conventions Rationale):

  • Names (except as noted below) should be all lowercase, with words separated by underscores.

  • Acronyms should be treated as ordinary names (e.g. xml_parser instead of XML_parser).

  • Template parameter names begin with an uppercase letter.

  • Macro names all uppercase and begin with BOOST_.

  • Choose meaningful names - explicit is better than implicit, and readability counts. There is a strong preference for clear and descriptive names, even if lengthy.

Naming Consistency

As library developers and users have gained experience with Boost, the following consistent naming approach has come to be viewed as helpful, particularly for larger libraries that need their own header subdirectories and namespaces.

  • The library is given a name that describes the contents of the library. Cryptic abbreviations are strongly discouraged. Following the practice of the C++ Standard Library, names are usually singular rather than plural. For example, a library dealing with file systems might chose the name "filesystem", but not "filesystems", "fs" or "nicecode".

  • The library’s primary directory (in parent boost-root/libs) is given that same name. For example, boost-root/libs/filesystem.

  • The library’s primary header directory (in boost-root/libs/name/include) is given that same name. For example, boost-root/libs/filesystem/boost/filesystem.

  • The library’s primary namespace (in parent ::boost) is given that same name, except when there’s a component with that name (e.g., boost::tuple), in which case the namespace name is pluralized. For example, ::boost::filesystem.

  • The first letter of the library name is capitalized.

  • A period between "Boost" and the library name (e.g., Boost.Bind) is used if and only if the library name is not followed by the word "library".

  • The word "library" is not part of the library name and is therefore lowercased.

Here are a few example sentences of how to apply these conventions:

  • "Boost.Bind was written by Peter Dimov."

  • "The Boost Asio library was written by Christopher Kohlhoff."

  • "I regularly use Spirit, a Boost library written by Joel de Guzman and Hartmut Kaiser."

Filenames

Naming requirements ensure that file and directory names are relatively portable, including to ISO 9660:1999 (with extensions) and other relatively limited file systems. Superscript links are provided to detailed rationale for each choice.

  • Names must contain only lowercase ASCII letters ('a'-'z'), numbers ('0'-'9'), underscores ('_'), hyphens ('-'), and periods ('.'). Spaces are not allowed.

    • Some legacy file systems require single-case names. Single-case names eliminate casing mistakes when moving from case-insensitive to case-sensitive file systems.

    • To quote the POSIX standard, "Filenames should be constructed from the portable filename character set because the use of other characters can be confusing or ambiguous in certain contexts."

  • Directory names must not contain periods ('.').

    • Strict implementations of ISO 9660:1999 and some legacy operating systems prohibit dots in directory names. The need for this restriction is fading, and may be removed in time.
  • The first and last character of a file name must not be a period ('.').

    • POSIX has special rules for names beginning with a period. Windows prohibits names ending in a period.
  • The first character of names must not be a hyphen ('-'), as this would be too confusing or ambiguous in certain contexts.

  • The maximum length of directory and file names is 31 characters. We had to draw the line somewhere, and so the limit imposed by a now obsolete Apple file system was chosen years ago.

  • The total path length must not exceed 207 characters (ISO 9660:1999).

Other conventions ease communication:

  • Files intended to be processed by a C++ compiler as part of a translation unit should have a three-letter filename extension ending in "pp" (typically .cpp and .hpp). Other files should not use extensions ending in "pp". This convention makes it easy to identify all of the source in Boost.

  • All libraries have at their highest level a primary directory named for the particular library. See https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#_naming_consistency">Naming Consistency. The primary directory may have sub-directories.

Testing and Error Handling

Assertions

It is recommended you add runtime assertions to your code (including library headers). Avoid C’s assert macro and use Boost’s BOOST_ASSERT macro (in boost/assert.hpp) instead as it is more configurable.

Make sure your code compiles in the presence of the min() and max() macros. Some platform headers define min() and max() macros which cause some common C++ constructs to fail to compile. To protect your code from inappropriate macro substitution:

  • If you want to call std::min() or std::max():

    • If you do not require argument-dependent look-up, use (std::min)(a,b).

    • If you do require argument-dependent look-up, you should:

      1. #include <boost/config.hpp>

      2. Use BOOST_USING_STD_MIN(); to bring std::min() into the current scope.

      3. Use min BOOST_PREVENT_MACRO_SUBSTITUTION (a,b); to make an argument-dependent call to min(a,b).

  • If you want to call std::numeric_limits<int>::max(), use (std::numeric_limits<int>::max)() instead.

  • If you want to call a min() or max() member function, instead of doing obj.min(), use (obj.min)().

  • If you want to declare or define a function or a member function named min or max, then you must use the BOOST_PREVENT_MACRO_SUBSTITUTION macro. Instead of writing int min() { return 0; } you should write int min BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; }. This is true regardless if the function is a free (namespace scope) function, a member function or a static member function, and it applies for the function declaration as well as for the function definition.

Redirection

The primary directory should always contain a file named index.html. Authors have requested this so that they can publish URL’s in the form https://www.boost.org/libs/lib-name with the assurance a documentation reorganization won’t invalidate the URL. Boost’s internal tools are also simplified if a library’s documentation is always reachable via the simplified URL.

The primary directory index.html file should do an automatic redirection to the doc/html subdirectory. For example, the json library contains the following index.html file:

<html>
    <head>
        <title>Boost.JSON</title>
        <meta http-equiv="refresh" content="0; URL=./doc/html/index.html">
    </head>
    <body>
        Automatic redirection failed, please go to
        <a href="./doc/html/index.html">./doc/html/index.html</a>
        <hr>
        <tt>
        Boost.JSON<br>
        <br>
        Copyright&nbsp;(C)&nbsp;2019&nbsp;Vinnie&nbsp;Falco<br>
        Copyright&nbsp;(C)&nbsp;2020&nbsp;Krystian&nbsp;Stasiowski<br>
        <br>
        Distributed under the Boost Software License, Version 1.0.
        (See accompanying file LICENSE_1_0.txt or copy at
        <a href=http://www.boost.org/LICENSE_1_0.txt>http://www.boost.org/LICENSE_1_0.txt</a>) <br>
        <br>
        </tt>
    </body>
</html>

Rationale

Rationale is defined as "The fundamental reasons for something; basis" by the American Heritage Dictionary.

Beman Dawes comments: "Failure to supply contemporaneous rationale for design decisions is a major defect in many software projects. Lack of accurate rationale causes issues to be revisited endlessly, causes maintenance bugs when a maintainer changes something without realizing it was done a certain way for some purpose, and shortens the useful lifetime of software."

Rationale is fairly easy to provide at the time decisions are made, but hard to accurately recover even a short time later. Rationale for some of the requirements and guidelines follows.

Exception Specification Rationale

Exception specifications (ISO 15.4) are sometimes coded to indicate what exceptions may be thrown, or because the programmer hopes they will improve performance. But consider the following member from a smart pointer:

T& operator*() const throw()  { return *ptr; }

This function calls no other functions; it only manipulates fundamental data types like pointers Therefore, no runtime behavior of the exception-specification can ever be invoked. The function is completely exposed to the compiler; indeed it is declared inline. Therefore, a smart compiler can easily deduce that the functions are incapable of throwing exceptions, and make the same optimizations it would have made based on the empty exception-specification. A "dumb" compiler, however, may make all kinds of pessimizations.

Next Post

  • Libraries
    The section outlines the criteria that a library must satisfy to be accepted by Boost. It states that the license must conform to Boost’s license requirements, excluding restricted licenses such as GPL or LGPL. Clear copyright ownership must be demonstrated, and the library should be of general interest and portable across platforms. Compliance with Boost’s organization standards is expected after acceptance, while adherence to design best practices and documentation guidelines is required beforehand. Contributors are expected to engage in mailing‑list discussions and refine the library as needed. A tip notes that submissions beginning with a claim of just having started to read the mailing list often fail. Prospective submitters are reminded to verify ownership, include copyright notices in all files, and obtain employer releases when necessary.