By chosing the C++ programming language, for its new Moments photo-sharing application, Facebook is able to maintain a single code base for much of the app, which runs on both iOS and Android devices.
"It is somewhat of a surprising choice," admitted Ashwin Bharambe, one of the Facebook developers who created Moments, about the use of C++. "There are more and more people trying to do this in order to share code across different platforms."
Launched Monday, Moments is designed to collect and display photos taken by a group of people, such as a group of friends hanging out at a barbecue, so they can be viewed in a single location.
Today one of the most challenging issues mobile development teams must face in building such apps is that they must run on multiple, incompatible platforms. Maintaining separate code bases for Android, Windows and iOS can be particularly time-consuming when new features need to be added, given that all three code bases will need to be updated independently.
A number of companies, such as Xamarin and Microsoft itself, offer platforms that allow developers to maintain a single code-base for a cross-platform application, though the developer gets locked into relying on the platform, and can only use the functionalities the platform itself provides. Another approach is to use the HTML5 set of open standards, though performance can suffer from this approach.
Instead, the Facebook development team went with C++.
Initially developed in the late 1970s, C++ is a powerful general-purpose programming language, and is still widely used for writing mission critical and performance-sensitive applications. It is less widely used, however, for mobile platforms, given that it can be a challenge to program in because it forces the developer to deal with memory management and other subtle nuances in abstraction.
The Facebook team found the extra burden to be worth the effort. With C++, "a lot of our more complicated logic can be written once and run on both iOS and Android," Bharambe said. A single code base allows the group to frequently update the app, as well as offer high performance.
Facebook relied on a number of external technologies to help with the implementation.
From Dropbox, the company borrowed a piece of software called Djinni, which converts C++ data models into Java code, the runtime language for Android.
For iOS apps, Facebook took advantage of the similarities between C++ and Objective-C, which is Apple's preferred language for building iPhone and iPad apps. The team was able to use lightweight wrappers to get the C++ code to work in the Objective-C environment.
The team also used functional programming methodologies to make the resulting code easier to understand by others. Functional programming eliminates the use of variables by treating the code as a series of mathematical functions instead.
The Facebook engineers behind Moments discussed some of the other technology choices they made in a blog item posted Monday.