Michael Park created MESOS-3754:
-----------------------------------
Summary: Update the style guide to omit the default case in switch
statements
Key: MESOS-3754
URL: https://issues.apache.org/jira/browse/MESOS-3754
Project: Mesos
Issue Type: Bug
Components: documentation
Reporter: Michael Park
This is the continuation of the initial discussions started on MESOS-2664.
The motivation is to rely on the compiler for compile-time errors for cases
missing in the {{switch}} statement, rather than aborting in the {{default}}
case at runtime.
The pattern we want to avoid is a {{switch}} statement that fully enumerates
the cases of an {{enum}} *and* having a {{default}} case that aborts at
runtime. The preferred approach is to omit the {{default}} case.
This pattern can be seen across the codebase, one example is:
{code}
switch (volume.mode()) {
case Volume::RW: volumeConfig += ":rw"; break;
case Volume::RO: volumeConfig += ":ro"; break;
default:
LOG(FATAL) << "Unknown Volume mode: " << volume.mode();
break;
}
{code}
The proposal is not to disallow uses of {{default}} cases, but to only use them
when it actually carries some meaningful fallback behavior.
This use of {{default}} leads to the following advantages:
1. If we miss any of the cases, you get notified via a compiler-error.
2. If a new value is added to an {{enum}}, the compiler reports all of the
places that needs to be updated to handle the new value.
Ideally, the compiler would also prevent us from covering all enumerations
*and* providing a {{default}}. Since this is not an available option, we aim to
capture this as a style guideline.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)