[PATCH] ui-refs.c: Simplify and inline cmp_age()

Lukas Fleischer cgit at cryptocrack.de
Tue Feb 4 20:22:05 CET 2014


In comparison functions, only the sign of the return value matters -- no
need to have extra checks to ensure the return value always is +/-1 when
the actual parameters differ. After removing all the checks, the
function body boils down to

    return age2 - age1;

so we can as well replace all invocations with that subtraction.

Signed-off-by: Lukas Fleischer <cgit at cryptocrack.de>
---
 ui-refs.c | 18 ++----------------
 1 file changed, 2 insertions(+), 16 deletions(-)

diff --git a/ui-refs.c b/ui-refs.c
index 147b665..8a4ce74 100644
--- a/ui-refs.c
+++ b/ui-refs.c
@@ -11,20 +11,6 @@
 #include "html.h"
 #include "ui-shared.h"
 
-static int cmp_age(int age1, int age2)
-{
-	if (age1 != 0 && age2 != 0)
-		return age2 - age1;
-
-	if (age1 == 0 && age2 == 0)
-		return 0;
-
-	if (age1 == 0)
-		return +1;
-
-	return -1;
-}
-
 static int cmp_ref_name(const void *a, const void *b)
 {
 	struct refinfo *r1 = *(struct refinfo **)a;
@@ -38,7 +24,7 @@ static int cmp_branch_age(const void *a, const void *b)
 	struct refinfo *r1 = *(struct refinfo **)a;
 	struct refinfo *r2 = *(struct refinfo **)b;
 
-	return cmp_age(r1->commit->committer_date, r2->commit->committer_date);
+	return r2->commit->committer_date - r1->commit->committer_date;
 }
 
 static int get_ref_age(struct refinfo *ref)
@@ -59,7 +45,7 @@ static int cmp_tag_age(const void *a, const void *b)
 	struct refinfo *r1 = *(struct refinfo **)a;
 	struct refinfo *r2 = *(struct refinfo **)b;
 
-	return cmp_age(get_ref_age(r1), get_ref_age(r2));
+	return get_ref_age(r2)- get_ref_age(r1);
 }
 
 static int print_branch(struct refinfo *ref)
-- 
1.8.5.3



More information about the CGit mailing list