Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 2 Jan 2002 09:41:00 +0100 (CET)
From:      Harti Brandt <brandt@fokus.gmd.de>
To:        Marcel Moolenaar <marcel@xcllnt.net>
Cc:        cvs-all@FreeBSD.ORG, <cvs-committers@FreeBSD.ORG>
Subject:   Re: cvs commit: src/share/man/man3 queue.3
Message-ID:  <20020102093537.X1528-100000@beagle.fokus.gmd.de>
In-Reply-To: <20011231190126.A755@dhcp01.pn.xcllnt.net>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, 31 Dec 2001, Marcel Moolenaar wrote:

MM>On Mon, Dec 31, 2001 at 05:14:08PM -0800, Julian Elischer wrote:
MM>>
MM>> > I'd consider this a misuse of _FOREACH, because the something that's
MM>> > being done is definitely not done for each p. Even some_test_on() is
MM>> > not performed on each p. I'm with phk here.
MM>> >
MM>> > Note that if it's simply too easy to use _FOREACH in these cases, we
MM>> > may want to consider creating a more suitable abstraction, like:
MM>> >
MM>> > 	TAILQ_UNTIL(p, &list, p_next)
MM>> > 		some_test_on(p);
MM>> > 	if (p == 0)
MM>> > 		return ENOTFOUND;
MM>> > 	/* do something */
MM>> > 	return 0;
MM>> >
MM>> > Just a thought.
MM>>
MM>> So you think that the final value of p should be undefined,
MM>> and there is no  built in way to see if there were any elements in the
MM>> list at all or whether we completed the list?
MM>
MM>Theoretically speaking, yes.
MM>
MM>> I'm just documenting what the code does, and how people have used it.
MM>
MM>I understand that and didn't really object to that.
MM>
MM>> if you think that phk is correct we should add an example to the man page
MM>> showing:
MM>>
MM>>
MM>> TAILQ_FOREACH(p, &list, p_next)
MM>> 	if (some_test_on(p))
MM>> 		goto fred;
MM>> 	goto jim;
MM>> fred:	print ("we got one");
MM>> jim:
MM>
MM>What's wrong with the following if _FOREACH is preferred:
MM>
MM>	TAILQ_FOREACH(p, &list, p_next) {
MM>		if (some_test_on(p)) {
MM>			/* Do something */
MM>			break; /* optional */
MM>		}
MM>	}

This makes things harder to read if /* Do something */ is longer than a
screen. Well, I think, that relying on the pointer beeing NULL if running
to the end of the list just perfectly mirrors the usual C-idiom:

	for (i = 0; i < n; i++)
		if (test(i))
			break;

	if (i == n)
		...

The TAILQ_UNTIL(...) would even more obscure things...

harti
-- 
harti brandt, http://www.fokus.gmd.de/research/cc/cats/employees/hartmut.brandt/private
              brandt@fokus.gmd.de


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe cvs-all" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20020102093537.X1528-100000>