Some characteristics of the Facebook C++ open source projects.

Facebook as Google and Microsoft chose to open source many of their  projects which is a good news for many companies and developers. Indeed having access to the source code from big companies is always interesting for many reasons:

  • They have resources to employ experts like Andrei Alexandrescu.
  • They have resources to  invest in the quality and they refactor regularly.
  • The source code is well tested in production as explained by Jordan Delong in its Folly article:

The utilities contained in Folly are things we use heavily in production—this is code that runs on thousands of servers doing work on behalf of 900 million users every day. These utilities are loosely connected, but the over-arching theme for all of the components is high performance at scale. Some of them will show a fairly specialized focus, like reducing contention or packing things into small amounts of memory.

  • They have enough feedbacks from the users to improve their projects.

Many  C++ open source projects are available from their GitHub repository. We can enumerate: folly, hhvm, osquery, rocksdb, proxygen, fbthrift and fatal.

Let’s discover some common characteristics of these projects:

1- Build process

For each project the CMake tool is used to build it. CMake is cross-platform free and open-source software for managing the build process of software using a compiler-independent method. CMake was adopted by many big companies, even Microsoft use it now  for their cross plateform projects.

2- Compiler adopted

The Facebook developement teams love the gcc compiler, its their first citizen compiler. The minimum gcc version required is 4.8 which implements the C++11 features.

3- Plateform supported

For almost all C++ projects only Linux distributions and Mac OSX are supported. No support for Windows which is not an issue for Facebook. I don’t think they have one machine using Windows 🙂

4- BSD is the most adopted license

Many licenses kind are used, it depends on the project . We can enumerate these licenses kind:

  • Apache license
  • Php license
  • BSD license
  • University of Illinois/NCSA Open Source License

However the most adopted one is BSD.

5-Documentation

Each project contains a directory named docs which includes many .md pages.

fb

Markdown is a lightweight markup language with plain text formatting syntax designed so that it can be converted to HTML and many other formats using a tool by the same name. Markdown is the popular way to document open  source projects hosted on GitHub.

Some projects contains advanced docs concerning its technical aspects like the case of the folly  vector.

6- Test and validation

Each project contains a directory named test. However no specific tehnology is used to test and validate the project. For some projects googletest is used , for others the tests are down by pyhon scripts.

7- Modularity: Physical vs Logical

Modularity is a software design technique that increases the extent to which software is composed from separate parts, you can manage and maintain modular code easily.

We can modularize a project using two approaches:

  • Physically: By using directories and files, this modularity is provided by the operating system and can be applied to any language.
  • Logically: By using namespaces, component, classes, structs and functions, this technique depends on the language capabilities.

For some Facebook projects  the namespaces are used to modularize them like the case of Folly and rocksdb, for some  others the physical approach was adopted and only one namespace includes all the classes like the case of hhvm.

8- Namespaces usage

Namespaces are also used for these two other reasons:

Anonymous namespace.

Namespace with no name avoids making global static variable. The “anonymous” namespace you have created will only be accessible within the file you created it in.

Hiding details by convention

For templated libraries where the code is implemented in header files, it’s interesting to find a way to inform the library user that he dont need to use directly some specific types because they concern only the implementation. In C# the “internal” keyword did the job, but in C++ there’s no way to hide public types to the library user.

A common idiom in modern C++, pioneered by the developers of the Boost libraries, is to separate symbols that form part of the implementation of your module (that is, don’t form part of the public API) but that have to be publicly available into a separate sub-namespace, by convention named detail. For example Folly use this convention to hide its details.

9- Facebook adopted the modern C++

Here’s what say Andrei Alexandrescu about the Modern C++ design:

Modern C++ Design defines and systematically uses generic components – highly flexible design artifacts that are mixable and matchable to obtain rich behaviors with a small, orthogonal body of code.

 Three assetions are interesting in his point of view:

  • Modern C++ Design defines and systematically uses generic components.
  • highly flexible design.
  • obtain rich behaviors with a small, orthogonal body of code.

The Facebook development teams chose to use widely the generic progamming approach. But in general this choice come with a price, the code become not easy to read and maintain specially if advanced generic programming techniques are used. For this reason Facebook teams chose the KISS principle and their developers prefer to avoid the complicated template programming as also google recommend in their C++ Style Guide.

10- Facebook early adpted C++11

As soon as the gcc compiler team added the C+11 support the Facebook developement teams decided to move forward to this new standards and use widely the new features, Folly as explained in this previous post  is the best example of its use, almost all the C++11 features were used.

Here’s a snipet code from the Folly library:

11- STL is widely used

The Standard Template Library (STL), part of the C++ Standard Library, offers collections of algorithms, containers, iterators, and other fundamental components, implemented as templates, classes, and functions essential to extend functionality and standardization to C++. STL main focus is to provide improvements implementation standardization with emphasis in performance and correctness.

STL is widely used in all Facebook C++ projects. Almost all the STL components are used.

12- Quality driven developement

If we explore the Facebook source code, we are impressed by the quality of their implementation. Each method body is characterized by:

  • Has only few lines of code.
  • The signature is well defined.
  • It’s well commented.
  • It’s well indented.
  • The variable names are very clear.
  • The coupling with other methods is low.

And many best practices are adopted, some of them could be found here. Moreover their C++ teams use flint to check their codes against some coding rules.

Summary:

Exploring the source code developed by teams from big companies is always a good approach to improve your programming skills, generally their projects are very well implemented and tested. We can learn many best practices by exploring their projects.

Leave a Reply

Your email address will not be published. Required fields are marked *