You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@mesos.apache.org by "Benjamin Bannier (JIRA)" <ji...@apache.org> on 2015/10/19 10:53:05 UTC
[jira] [Commented] (MESOS-3754) Update the style guide with a rule
regarding the use of default case in switch statements
[ https://issues.apache.org/jira/browse/MESOS-3754?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14963000#comment-14963000 ]
Benjamin Bannier commented on MESOS-3754:
-----------------------------------------
For this to really pay off one probably would also want to request that {{enum}} values shall preferably (always?) be branched on with {{switch}} instead of e.g. plain {{if}}.
Also, when switching over plain integer, non-{{enum}} types one should probably *always* add a {{default}} case to allow reasoning about the code locally.
> Update the style guide with a rule regarding the use of 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)