Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 7 Mar 2002 12:54:22 +0100 (CET)
From:      Harti Brandt <brandt@fokus.gmd.de>
To:        Mike Barcroft <mike@FreeBSD.ORG>
Cc:        Harti Brandt <brandt@fokus.gmd.de>, "David O'Brien" <obrien@FreeBSD.ORG>, Mark Murray <markm@FreeBSD.ORG>, <cvs-committers@FreeBSD.ORG>, <cvs-all@FreeBSD.ORG>
Subject:   Re: cvs commit: src/usr.bin/rwall rwall.c
Message-ID:  <20020307124800.S99061-100000@beagle.fokus.gmd.de>
In-Reply-To: <20020307063841.D81803@espresso.q9media.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 7 Mar 2002, Mike Barcroft wrote:

MB>Harti Brandt <brandt@fokus.gmd.de> writes:
MB>> The point of (void) is that the programmer who writes the code tells
MB>> another programmer who reads the code: "Yes, I know, this function returns
MB>> a value, but I ignore it and I know what I'm doing so don't be surprised."
MB>
MB>I always thought that explanation was rather silly.  Why not just put
MB>a comment at the top that says, "I realize I don't check every return
MB>value, that is intentional." :)

It is a common misconception, that programming is communication between a
programmer and a computer. It is more relevant, that another programmer
can read the code and understand it without. For easy understanding
locality of the context is essential. When reading (void)func(...), I
immediately see, that the return value is intentionally ignored. When
there is a comment on top of the file saying 'the return value of func
is sometimes ignored' I have to lookup that comment when I see func(...)
(and I have to remeber that there was such a comment. I aggree however,
that there is a class of functions like printf(), where one should have
a general comment in his mind 'the return value of function printf is
almost ever ignored'. snprintf is however not in this class, see your
comment below...

 MB>
MB>I did find the explanation I was looking for in
MB>Message-ID: <20010817005031.S29341-100000@besplex.bde.org>:
MB>: It by prevents warnings like the following from `lint -h':
MB>:
MB>:     "snprintf returns a value which is sometimes ignored"
MB>:     "snprintf returns a value which is always ignored"
MB>:
MB>: This is the opposite of getting in the way of linting.
MB>
MB>> In the given case it is obvious that the snprintf shouldn't fail.
MB>
MB>In this case yes, in general snprintf()'s return value should always
MB>be checked unless truncation is irrelevant.  For cases in which
MB>snprintf() can't fail, sprintf() is a good choice.

Exactly. snprintf suggests to the reader of the code, that truncation or
errors may happen and he wonders why one doesn't check the return code.

harti
-- 
harti brandt, http://www.fokus.gmd.de/research/cc/cats/employees/hartmut.brandt/private
              brandt@fokus.fhg.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?20020307124800.S99061-100000>