BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Facebook's Moments App Does C++ for Cross-Platform Development

Facebook's Moments App Does C++ for Cross-Platform Development

Leia em Português

This item in japanese

Bookmarks

One of the most recent Facebook's apps, Moments, is using C++ to share its business logic across iOS and Android, Facebook's engineers Ashwin Bharambe, Zack Gomez, Will Ruben explain. Here we review Facebook engineers rationale for that choice and its outcomes.

For their Moments app, Facebook engineers decided to move server logic to the client in order to "focus solely on the client and also to make the development-run-test cycle as short as we could". The downside of this choice was the growth of the code that they had to write for the two platforms they were initially targeting: iOS and Android.

Among the many alternatives that are available for sharing code across platforms, Facebook engineers decided they could "write the UI in platform-specific code and the business logic in shared code using C++". C++ is considered a language able to provide high performance although it lacks higher-level abstractions and forces programmers to deal with memory management. Still, Facebook programmers "were able to quickly implement highly performant, memory-safe code" by using modern C++ features such as std::shared_ptr, lambdas, and auto declarations.

Facebook engineers strove to keep the C++ layer API simple through a few fundamental choices:

  • Functional style coding, which implied "converting raw data objects into immutable view models by default."
  • One-way data flow consisting of "fire-and-forget mutations and methods to compute view models required by specific views".
  • Caching, where profiling showed it was appropriate, "to avoid recomputing unchanged intermediate results".

On the Android platform, there is the additional issue of generating bindings to platform-specific code. To this aim, Facebook engineers used Dropbox's Djinni to convert view models from C++ to Java. They also rewrote Djinni code generator to better suite their functional approach and optimize garbage collection. On iOS, Objective-C++ can be used to seamlessly integrate with C++ code.

This approach has made it possible to share across iOS and Android the most part of Moments business logic, which accounts roughly for one-third of the codebase on each platform. Finally, Facebook engineers note that thanks to this approach, they "have been able to create new features with far less work and fewer bugs" and also "to more flexibly allocate engineering time between the two platforms, allowing us to ship on both platforms simultaneously".

Rate this Article

Adoption
Style

BT