diff mbox series

[v2,3/4] t-reftable-readwrite: use 'for' in place of infinite 'while' loops

Message ID 20240809111312.4401-4-chandrapratap3519@gmail.com (mailing list archive)
State Superseded
Headers show
Series t: port reftable/readwrite_test.c to the unit testing framework | expand

Commit Message

Chandra Pratap Aug. 9, 2024, 11:05 a.m. UTC
Using a for loop with an empty conditional statement is more concise
and easier to read than an infinite 'while' loop in instances
where we need a loop variable. Hence, replace such instances of a
'while' loop with the equivalent 'for' loop.

Mentored-by: Patrick Steinhardt <ps@pks.im>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Chandra Pratap <chandrapratap3519@gmail.com>
---
 t/unit-tests/t-reftable-readwrite.c | 12 +++---------
 1 file changed, 3 insertions(+), 9 deletions(-)

Comments

Junio C Hamano Aug. 9, 2024, 7:06 p.m. UTC | #1
Chandra Pratap <chandrapratap3519@gmail.com> writes:

> Using a for loop with an empty conditional statement is more concise
> and easier to read than an infinite 'while' loop in instances
> where we need a loop variable. Hence, replace such instances of a
> 'while' loop with the equivalent 'for' loop.

Quite honestly, the above is counter-productive if pushed as a
general guideline, because the goodness of it depends what happens
in the third part of the for () control (i.e., what should happen at
the end of each iteration and if it wants to be bypassed in the
conditional inside the loop).

In this particular case, it probably is OK, but still is a
subjective, borderline Meh, to me.

I see no violation of correctness in the rewrite, though ;-)
diff mbox series

Patch

diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c
index e90f2bf9de..7daf28ec6d 100644
--- a/t/unit-tests/t-reftable-readwrite.c
+++ b/t/unit-tests/t-reftable-readwrite.c
@@ -268,15 +268,13 @@  static void t_log_write_read(void)
 	err = reftable_iterator_seek_log(&it, "");
 	check(!err);
 
-	i = 0;
-	while (1) {
+	for (i = 0; ; i++) {
 		int err = reftable_iterator_next_log(&it, &log);
 		if (err > 0)
 			break;
 		check(!err);
 		check_str(names[i], log.refname);
 		check_int(i, ==, log.update_index);
-		i++;
 		reftable_log_record_release(&log);
 	}
 
@@ -374,7 +372,7 @@  static void t_table_read_write_sequential(void)
 	err = reftable_iterator_seek_ref(&it, "");
 	check(!err);
 
-	while (1) {
+	for (j = 0; ; j++) {
 		struct reftable_ref_record ref = { 0 };
 		int r = reftable_iterator_next_ref(&it, &ref);
 		check_int(r, >=, 0);
@@ -382,8 +380,6 @@  static void t_table_read_write_sequential(void)
 			break;
 		check_str(names[j], ref.refname);
 		check_int(update_index, ==, ref.update_index);
-
-		j++;
 		reftable_ref_record_release(&ref);
 	}
 	check_int(j, ==, N);
@@ -589,15 +585,13 @@  static void t_table_refs_for(int indexed)
 	err = reftable_reader_refs_for(&rd, &it, want_hash);
 	check(!err);
 
-	j = 0;
-	while (1) {
+	for (j = 0; ; j++) {
 		int err = reftable_iterator_next_ref(&it, &ref);
 		check_int(err, >=, 0);
 		if (err > 0)
 			break;
 		check_int(j, <, want_names_len);
 		check_str(ref.refname, want_names[j]);
-		j++;
 		reftable_ref_record_release(&ref);
 	}
 	check_int(j, ==, want_names_len);