
{"id":2017,"date":"2015-08-20T23:31:12","date_gmt":"2015-08-20T23:31:12","guid":{"rendered":"http:\/\/www.codergears.com\/Blog\/?p=2017"},"modified":"2015-08-20T23:43:17","modified_gmt":"2015-08-20T23:43:17","slug":"whats-the-characteristics-of-the-facebook-c-open-source-projects","status":"publish","type":"post","link":"https:\/\/codergears.com\/Blog\/?p=2017","title":{"rendered":"Some characteristics of the Facebook  C++ open source projects."},"content":{"rendered":"<p>Facebook as Google and Microsoft chose to open source many of their \u00a0projects 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:<\/p>\n<ul>\n<li>They have resources to employ experts like Andrei Alexandrescu.<\/li>\n<li>They have resources to \u00a0invest in the quality and they refactor regularly.<\/li>\n<li>The source code is well tested in production as explained by Jordan Delong in its <a href=\"https:\/\/www.facebook.com\/notes\/facebook-engineering\/folly-the-facebook-open-source-library\/10150864656793920\">Folly article<\/a>:<\/li>\n<\/ul>\n<blockquote><p><span style=\"color: #141823;\">The utilities contained in Folly are things we use heavily in production\u2014this 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.<\/span><\/p><\/blockquote>\n<ul>\n<li>They have enough feedbacks from the users to improve their projects.<\/li>\n<\/ul>\n<p>Many \u00a0C++ open source projects are available from their <a href=\"https:\/\/github.com\/facebook\">GitHub repository<\/a>. We can enumerate: folly, hhvm, osquery, rocksdb, proxygen, fbthrift and fatal.<\/p>\n<p>Let&#8217;s discover some common characteristics of these projects:<!--more--><\/p>\n<p><strong>1- Build\u00a0process<\/strong><\/p>\n<p>For each project the CMake tool is used to build it.\u00a0CMake 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 \u00a0for their cross plateform projects.<\/p>\n<p><strong>2- Compiler adopted<\/strong><\/p>\n<p>The Facebook developement teams love the gcc compiler, its their first citizen compiler. The minimum gcc version required is\u00a04.8 which implements the C++11 features.<\/p>\n<p><strong>3- Plateform supported<\/strong><\/p>\n<p>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&#8217;t think they have one machine using Windows \ud83d\ude42<\/p>\n<p><strong>4- BSD is the most adopted license<br \/>\n<\/strong><\/p>\n<p>Many licenses kind are used, it depends on the project . We can enumerate these licenses kind:<\/p>\n<ul>\n<li>Apache license<\/li>\n<li>Php license<\/li>\n<li>BSD license<\/li>\n<li><span style=\"color: #333333;\">University of Illinois\/NCSA Open Source License<\/span><\/li>\n<\/ul>\n<p>However the most adopted one is BSD.<\/p>\n<p><strong>5-Documentation<\/strong><\/p>\n<p>Each project contains a directory named docs which includes\u00a0many .md pages.<\/p>\n<p><a href=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/fb.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2051\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/fb.png\" alt=\"fb\" width=\"948\" height=\"528\" \/><\/a><\/p>\n<p>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 \u00a0source projects hosted on GitHub.<\/p>\n<p>Some projects contains advanced docs concerning its\u00a0technical aspects like the case of the\u00a0<a href=\"https:\/\/github.com\/facebook\/folly\/blob\/master\/folly\/docs\/FBVector.md\">folly \u00a0vector.<\/a><\/p>\n<p><strong>6- Test and validation<\/strong><\/p>\n<p>Each project contains a directory named test. However no specific tehnology is used to\u00a0test and validate the project. For some projects googletest is used , for others the tests are down by pyhon scripts.<\/p>\n<p><strong>7- Modularity: Physical vs Logical<\/strong><\/p>\n<p style=\"color: #333333;\">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.<\/p>\n<p style=\"color: #333333;\">We can modularize a project using\u00a0two approaches:<\/p>\n<ul>\n<li><em>Physically<\/em><span style=\"color: #333333;\">: By using directories and files, this modularity is provided by the operating system and can be applied to any language.<\/span><\/li>\n<li><em>Logically<\/em><span style=\"color: #333333;\">: By using namespaces, component, classes, structs and functions, this technique depends on the language capabilities.<\/span><\/li>\n<\/ul>\n<p style=\"color: #333333;\">For some Facebook projects \u00a0the namespaces are used to modularize them like the case of Folly and rocksdb, for some \u00a0others the physical approach was\u00a0adopted and only one namespace includes all the classes like the case of hhvm.<\/p>\n<p style=\"color: #333333;\"><strong>8- Namespaces usage<\/strong><\/p>\n<p style=\"color: #333333;\">Namespaces are also used for these two other reasons:<\/p>\n<p><strong>Anonymous namespace.<\/strong><\/p>\n<p>Namespace with no name avoids making global static variable. The \u201canonymous\u201d namespace you have created will only be accessible within the file you created it in.<\/p>\n<p><strong>Hiding details by convention<\/strong><\/p>\n<p>For templated libraries where the code is implemented in header files, it\u2019s<span style=\"color: #555555;\">\u00a0interesting 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.\u00a0In\u00a0C# the \u201cinternal\u201d keyword did the job, but in C++ there\u2019s no way to hide public types to the library user.<\/span><\/p>\n<p>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\u2019t form part of the public API) but that have to be publicly available into a separate sub-namespace, by convention named detail. For example\u00a0Folly use this convention to hide its details.<\/p>\n<p style=\"color: #333333;\"><strong>9- Facebook adopted the modern C++<\/strong><\/p>\n<p style=\"color: #333333;\">Here\u2019s what say Andrei Alexandrescu about the Modern C++ design:<\/p>\n<blockquote style=\"color: #333333;\"><p>Modern C++ Design defines and systematically uses\u00a0generic components\u00a0&#8211; highly flexible design artifacts that are mixable and matchable to obtain rich behaviors with a small, orthogonal body of code.<\/p><\/blockquote>\n<p style=\"color: #333333;\">\u00a0Three\u00a0assetions are interesting in his point of view:<\/p>\n<ul style=\"color: #333333;\">\n<li>Modern C++ Design defines and\u00a0<strong>systematically uses\u00a0<\/strong><strong>generic components<\/strong>.<\/li>\n<li><span style=\"font-style: italic;\">highly\u00a0<strong>flexible<\/strong>\u00a0design.<\/span><\/li>\n<li><span style=\"font-style: italic;\">obtain rich behaviors with a\u00a0<strong>small, orthogonal<\/strong>\u00a0body of code.<\/span><\/li>\n<\/ul>\n<p>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 <a href=\"https:\/\/google-styleguide.googlecode.com\/svn\/trunk\/cppguide.html#Template_metaprogramming\">C++ Style Guide<\/a>.<\/p>\n<p><strong>10- Facebook early adpted C++11<\/strong><\/p>\n<p>As soon as the gcc compiler\u00a0team 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 <a href=\"http:\/\/www.codergears.com\/Blog\/?p=431\">previous post<\/a> \u00a0is the best example of its use, almost all the C++11 features were used.<\/p>\n<p>Here&#8217;s a snipet code from the Folly library:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.codergears.com\/Blog\/wp-content\/uploads\/c0.png\" alt=\"\" \/><\/p>\n<p><strong>11- STL is widely used<\/strong><\/p>\n<p><span style=\"color: #252525;\">The\u00a0<\/span><b style=\"color: #252525;\">Standard Template Library<\/b><span style=\"color: #252525;\">\u00a0(<\/span><b style=\"color: #252525;\">STL<\/b><span style=\"color: #252525;\">), part of the\u00a0<\/span>C++ Standard Library<span style=\"color: #252525;\">, 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.<\/span><\/p>\n<p>STL is widely used in all Facebook C++ projects. Almost all the STL components are used.<\/p>\n<p><strong>12- Quality driven developement<\/strong><\/p>\n<p>If we\u00a0explore the Facebook source code, we are impressed by the quality of their implementation. Each method body is characterized by:<\/p>\n<ul style=\"color: #333333;\">\n<li>Has\u00a0only few lines of code.<\/li>\n<li>The signature is well defined.<\/li>\n<li>It\u2019s well commented.<\/li>\n<li>It\u2019s well indented.<\/li>\n<li>The variable names are very clear.<\/li>\n<li>The coupling with other methods is low.<\/li>\n<\/ul>\n<p>And many best practices are adopted, some of them could be found <a href=\"https:\/\/github.com\/facebook\/hhvm\/blob\/master\/hphp\/doc\/coding-conventions.md\">here<\/a>. Moreover their C++ teams use <a href=\"https:\/\/github.com\/facebook\/flint\">flint\u00a0<\/a>to check their codes against some coding rules.<\/p>\n<p><strong>Summary:<\/strong><\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Facebook as Google and Microsoft chose to open source many of their \u00a0projects 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 \u00a0invest in the &hellip; <a href=\"https:\/\/codergears.com\/Blog\/?p=2017\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Some characteristics of the Facebook  C++ open source projects.&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-2017","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/2017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2017"}],"version-history":[{"count":45,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/2017\/revisions"}],"predecessor-version":[{"id":2069,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=\/wp\/v2\/posts\/2017\/revisions\/2069"}],"wp:attachment":[{"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codergears.com\/Blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}