git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] branch.c: simplify chain of if statements

From
DFDragos Foianu <dragos.foianu@gmail.com>
Date
Mar 16, 2014, 21:22 UTC
Message-ID
<1395004962-18200-1-git-send-email-dragos.foianu@gmail.com>

This patch uses a table-driven approach in order to make the code cleaner. Although not necessary, it helps code reability by not forcing the user to read the print message when trying to understand what the code does. The rebase check has been moved to the verbose if statement to avoid making the same check in each of the four if statements.

Signed-off-by: Dragos Foianu <dragos.foianu@gmail.com>
---
 branch.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
diff --git a/branch.c b/branch.c
index 723a36b..e2fe455 100644
--- a/branch.c
+++ b/branch.c
@@ -54,6 +54,14 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
 	struct strbuf key = STRBUF_INIT;
 	int rebasing = should_setup_rebase(origin);
 
+	const char *verbose_prints[4] = {
+		"Branch %s set up to track remote branch %s from %s%s",
+		"Branch %s set up to track local branch %s%s",
+		"Branch %s set up to track remote ref %s%s",
+		"Branch %s set up to track local ref %s%s"
+	};
+	char *verbose_rebasing = rebasing ? " by rebasing." : ".";
+
 	if (remote_is_branch
 	    && !strcmp(local, shortname)
 	    && !origin) {
@@ -78,25 +86,17 @@ void install_branch_config(int flag, const char *local, const char *origin, cons
 
 	if (flag & BRANCH_CONFIG_VERBOSE) {
 		if (remote_is_branch && origin)
-			printf_ln(rebasing ?
-				  _("Branch %s set up to track remote branch %s from %s by rebasing.") :
-				  _("Branch %s set up to track remote branch %s from %s."),
-				  local, shortname, origin);
+			printf_ln(_(verbose_prints[0]),
+				local, shortname, origin, verbose_rebasing);
 		else if (remote_is_branch && !origin)
-			printf_ln(rebasing ?
-				  _("Branch %s set up to track local branch %s by rebasing.") :
-				  _("Branch %s set up to track local branch %s."),
-				  local, shortname);
+			printf_ln(_(verbose_prints[1]),
+				local, shortname, verbose_rebasing);
 		else if (!remote_is_branch && origin)
-			printf_ln(rebasing ?
-				  _("Branch %s set up to track remote ref %s by rebasing.") :
-				  _("Branch %s set up to track remote ref %s."),
-				  local, remote);
+			printf_ln(_(verbose_prints[2]),
+				local, remote, verbose_rebasing);
 		else if (!remote_is_branch && !origin)
-			printf_ln(rebasing ?
-				  _("Branch %s set up to track local ref %s by rebasing.") :
-				  _("Branch %s set up to track local ref %s."),
-				  local, remote);
+			printf_ln(_(verbose_prints[3]),
+				local, remote, verbose_rebasing);
 		else
 			die("BUG: impossible combination of %d and %p",
 			    remote_is_branch, origin);
-- 
1.8.3.2
Next: Matthieu Moy
Message 1 of 7 in “branch.c: simplify chain of if statements”
  1. branch.c: simplify chain of if statementsDragos Foianu, Mar 16, 2014
  2. Matthieu MoyMar 17, 2014
  3. Eric SunshineMar 17, 2014
  4. Eric SunshineMar 17, 2014
  5. Dragos FoianuMar 17, 2014
  6. Ævar Arnfjörð BjarmasonMar 17, 2014
  7. Eric SunshineMar 18, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.