From: Junio C Hamano <gitster@pobox.com>
To: Michael Lohmann <mi.al.lohmann@gmail.com>
Cc: phillip.wood123@gmail.com, git@vger.kernel.org,
phillip.wood@dunelm.org.uk, wanja.hentze@bevuta.com
Subject: Re: [PATCH v2] builtin/revert.c: refactor using an enum for cmd-action
Date: Thu, 11 Jan 2024 11:50:46 -0800 [thread overview]
Message-ID: <xmqqsf33fy3t.fsf@gitster.g> (raw)
In-Reply-To: <20240111174727.55189-1-mi.al.lohmann@gmail.com> (Michael Lohmann's message of "Thu, 11 Jan 2024 18:47:27 +0100")
Michael Lohmann <mi.al.lohmann@gmail.com> writes:
> struct option base_options[] = {
> - OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), 'q'),
> - OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), 'c'),
> - OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), 'a'),
> - OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), 's'),
> + OPT_CMDMODE(0, "quit", &cmd, N_("end revert or cherry-pick sequence"), ACTION_QUIT),
> + OPT_CMDMODE(0, "continue", &cmd, N_("resume revert or cherry-pick sequence"), ACTION_CONTINUE),
> + OPT_CMDMODE(0, "abort", &cmd, N_("cancel revert or cherry-pick sequence"), ACTION_ABORT),
> + OPT_CMDMODE(0, "skip", &cmd, N_("skip current commit and continue"), ACTION_SKIP),
> OPT_CLEANUP(&cleanup_arg),
> OPT_BOOL('n', "no-commit", &opts->no_commit, N_("don't automatically commit")),
> OPT_BOOL('e', "edit", &opts->edit, N_("edit the commit message")),
I actually do not terribly mind reusing the single letter option
(e.g. 'x' in "-x") and the command mode, as it is descriptive
enough. The argument that an enum allows compilers warn about
non-exhausitve switches is valid if we have such a switch.
> + switch (cmd) {
> + case ACTION_START:
> + goto skip_opt_compatibility_verification;
> + case ACTION_QUIT:
> + this_operation = "--quit";
> + break;
> + case ACTION_CONTINUE:
> + this_operation = "--continue";
> + break;
> + case ACTION_SKIP:
> + this_operation = "--skip";
> + break;
> + case ACTION_ABORT:
> + this_operation = "--abort";
> + break;
> }
>
> + verify_opt_compatible(me, this_operation,
And indeed the if/elseif cascade in the original is easier to ensure
its exhaustiveness by turning it into a switch.
HOWEVER.
If I were writing this patch, I would rather do it like so:
this_operation = NULL;
switch (cmd) {
case 'q':
this_operation = '--quit";
break;
...
case 'a':
this_operation = '--abort";
break;
default: /* everything else is compatible with others */
break;
}
if (this_operation)
verify_opt_compatible(me, this_operation, ...);
Use of enum is optional; I just didn't like too much churning to
illustrate a single idea (here, the single idea being "switch is
more appropriate then if/else cascade in this case"), and I think
this is easier to read than with enums [*].
Side note: After all the single letter option names are
meant to be mnemonic. "case 'q'" is just as descriptive as
"case ACTION_QUIT" in the context of this switch statement.
HTH.
next prev parent reply other threads:[~2024-01-11 19:50 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-11 8:04 [PATCH] builtin/revert.c: refactor using an enum for cmd-action Michael Lohmann
2024-01-11 16:57 ` Phillip Wood
2024-01-11 17:47 ` [PATCH v2] " Michael Lohmann
2024-01-11 19:50 ` Junio C Hamano [this message]
2024-01-11 20:06 ` [PATCH v3] " Michael Lohmann
2024-01-11 21:47 ` Junio C Hamano
2024-01-12 0:40 ` Junio C Hamano
2024-01-12 7:24 ` Jeff King
2024-01-12 18:13 ` Junio C Hamano
2024-01-12 19:33 ` Michael Lohmann
2024-01-11 19:37 ` [PATCH] " Junio C Hamano
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqsf33fy3t.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=mi.al.lohmann@gmail.com \
--cc=phillip.wood123@gmail.com \
--cc=phillip.wood@dunelm.org.uk \
--cc=wanja.hentze@bevuta.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).