Design Best Practices
Design Best Practices
Use these guidelines as recommendations based on past author’s experiences of preparing content for a library submission.
-
https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#_overview">Overview
-
https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#_source_files">Source Files
-
https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#_naming">Naming
-
https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#_testing_and_error_handling">Testing and Error Handling
-
https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#redirection">Redirection
-
https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#_rationale">Rationale
-
https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#_good_and_less_good_examples_of_api_design">Good and Less-Good Examples of API Design
-
https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#_see_also">See Also
Overview
When designing a new library:
-
Aim first for clarity and correctness; optimization should be only a secondary concern in most Boost libraries.
-
Aim for ISO Standard C++. Than means making effective use of the standard features of the language, and avoiding non-standard compiler extensions. It also means using the https://en.cppreference.com/w/cpp/standard_library">Standard Library where applicable.
-
Headers should be good neighbors. See https://www.boost.org/doc/contributor-guide/design-guide/headers.html">Headers and https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#_naming_consistency">Naming Consistency.
-
Follow quality programming practices. Recommended texts include Effective C++ 2nd Edition and More Effective C++, both by Scott Meyers and published by Addison Wesley.
-
Use the C++ Standard Library or other Boost libraries, but only when the benefits outweigh the costs. Except in special cases, do not use libraries other than the C++ Standard Library or Boost.
-
Read https://www.boost.org/doc/user-guide/implementation-variations.html">Implementation Variation Techniques to see how to supply performance, platform, or other implementation variations.
-
Read the guidelines for https://www.boost.org/doc/contributor-guide/design-guide/separate-compilation.html">Separate Compilation, to see how to ensure that compiled link libraries meet user expectations.
Source Files
-
Begin all source files (including programs, headers, scripts, etc.) with:
-
A comment line describing the contents of the file.
-
Comments describing copyright and licensing: again, refer to https://www.boost.org/doc/contributor-guide/requirements/license-requirements.html">License Requirements. Note that developers are allowed to provide a copy of the license text in LICENSE_1_0.txt, LICENSE.txt or LICENSE file within repositories of their libraries.
-
A comment line referencing your library on the Boost web site. For example
// See https://www.boost.org/libs/foofor library home page.Where foo is the directory name (see below) for the library. As well as aiding users who come across a Boost file detached from its documentation, some of Boost’s automatic tools depend on this comment to identify which library header files belong to.
-
-
Although some Boost members use proportional fonts, tabs, and unrestricted line lengths in their own code, Boost’s widely distributed source code should follow more conservative guidelines:
-
Use fixed-width fonts. See https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#_source_code_fonts_rationale">Source Code Fonts Rationale.
-
Use spaces rather than tabs. See https://www.boost.org/doc/contributor-guide/design-guide/design-best-practices.html#_tabs_rationale">Tabs Rationale.
-
Limit line lengths to 80 characters.
-
-
End all documentation files (HTML or otherwise) with a copyright message and a licensing message. Refer to published library documentation for examples.
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."
-