Skip to content

Proposal: #ifdef reduction

Justin Wilson edited this page Nov 17, 2023 · 1 revision

Many features are implemented through conditionally compiled code. The various conditional sections are detrimental in two ways. First, they are difficult to reason about. Often, a developer will make a change that then fails in CI due to a configuration corner-case. Second, it increases the number of configuration parameters and builds that should be tested.

When possible, optional features should be delivered in the plugin style where the code is not conditional and dispatches to a default no-op implementation. If the optional feature is built, the user should be able to switch between the implementations. This pattern could be used for DDS Security.

When conditional code is necessary, the code guarded by the condition should be as large as possible or factored appropriately. That is, the goal is to have conditions that guard functions, classes, and entire files instead of individual statements and small blocks.