News

Facebook Project's Cross-Platform Tool of Choice: C++

Never mind Sencha, Xamarin, PhoneGap, Appcelerator Titanium and dozens of other wannabe cross-platform mobile development tools -- a recent Facebook project resorted to a crusty old standby programming language to target iOS and Android platforms in a recent project: C++.

Facebook engineers gave developers a peek "under the hood" into their process for creating its Moments app to share photos with friends, to more easily distribute group shots to all parties involved, for example.

The choice for C++ took some developers by surprise, even leading to a Slashdot post, where some developers discussed the pros and cons of the language for such an application, in between ripping Facebook for privacy concerns, of course.

"There are many alternatives for sharing code between mobile platforms," said engineers Ashwin Bharambe, Zack Gomez and Will Ruben in a blog post Monday. "We wanted to optimize for fast iteration, app performance, and native look and feel. After weighing the alternatives, we chose to write the UI in platform-specific code and the business logic in shared code using C++."

Facebook said the vast majority of the business logic is shared code. In terms of overall lines of code, the shared code accounts for about one-third of the iOS and Android apps' total.

While many newer -- perhaps even "sexier" -- language options provide modern constructs that might seem more applicable for a cross-platform mobile app, Facebook said C++'s own modernizations make it suitable for the task.

"Traditionally, C++ is known for providing high performance while lacking easy memory management and higher-level abstractions," Facebook said. "However, using modern C++11 features such as std::shared_ptr reference counting, lambda functions and auto variable declarations, we were able to quickly implement highly performant, memory-safe code."

In order to keep the "C++ API boundary simple," the engineers said they used an architecture similar to Flux, which in-house coders use to create client-side Web applications, along with one-way data flow, functional style coding and immutable data objects. Coincidentally, those are all approaches that Facebook also uses in its revolutionary React JavaScript-centric approach created for the Web but recently targeted to mobile development. React is typically used for UIs to achieve native functionality and look and feel, but Facebook didn't specify what was actually used to write the platform-specific code for Moments.

For other aspects of the Moments project, Facebook used Djinni, an open source tool from Dropbox, convert view models to Java from the C++ shared code base for the Android app. "For our iOS view models, we take advantage of Objective-C's closeness to C++ to directly interact with the C++ view models via lightweight wrappers," the engineers said. "These designs let us focus on the business logic and UI without worrying about boilerplate code to connect the two."

The engineers describe several other development choices and tools, explaining how they opted out of using Bluetooth for Moments for several reasons, including balky iOS/Android interactions. Location functionality was also explored, but they found it to be too imprecise in crowded environments.

One thing the app does rely heavily on is cutting-edge facial recognition capability provided by Facebook's AI Research Lab. "For Moments, we built on top of the latest work by Facebook's AI Research Lab that was already powering tag suggestions on Facebook," the trio said. "This work includes one of the most accurate facial recognition systems in the world, matching human-level performance."

The post includes many more behind-the-scenes details, with a whole other post devoted to explaining the design process for Moments.

"We're really excited to launch Moments today in the U.S. on both iOS and Android," the developers said. "We hope you find the app as useful as we do, and we look forward to hearing your feedback. We will continue to move quickly, of course, to ship you improvements to the core Moments experience and some exciting new features."

About the Author

David Ramel is an editor and writer at Converge 360.