tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Fwd: Re: fmtcheck() query



    Date:        Tue, 12 Dec 2017 22:45:38 +0900
    From:        Rin Okuyama <rokuyama%rk.phys.keio.ac.jp@localhost>
    Message-ID:  <55c0e39e-e5de-1f06-a83e-e7cdc0f05afa%rk.phys.keio.ac.jp@localhost>

  | These examples may be insufficient to conclude that unused args should
  | be accused. However, what situations do you expect? The example of
  | file(1) is enough for you? Am I missing something again?

Missing, probably not, and I didn't know about file (hadn't looked to see
where fmtcheck() was used - though I did see the fix installed) - the case
I am think of is one that we typically handle with an "if" and two printf
statements, but which could be done with one with some increased flexibility.

Eg: if one wants to print something like

	"There are %d processes remaining\n", npids

that's easy enough, but that then prints

	There are 0 processes remaining

which would be much better written as

	There are no processes remaining

Inserting the "no" (or a null string if npids > 0) (and handling making
"processes" become singular when npids == 1) are easy, because with strings
we can always "print" an empty string, but making the "0" (the %d) go away
(print nothing) is not.

That's a case where just ignoring the npids arg and using a format that
does not have the %d in it would be useful, and using one that includes
the %d when the number should be printed.

Another example is printing times ass something like %d hrs %d mins %d secs
when the number of hours is > 0, or as %d mins %d secs when hours==0 and
mins > 0, or %d secs otherwise.   Again it is easy to amke a ?:
expression that selects a suitable format string, but not easy if the
hrs and mins args are always required to be used.

Naturally, for simple cases like these appear to be, just using the if
seems like a better solution, but it is a real nuisance when this variable
part, which is all that has been included above, is just one of a dozen
different args that are being used.

And while in many cases you'd just split the big printf into a bunch of
smaller ones, that's not always possible - like in cases where the
function isn't one of the printf's, but is syslog() (or something like it)
where the entire message is required to be in one call.

And apart from all that, I just generally prefer flexibility over rigidity
in interfaces - lots of rope with which to hang yourself, or to produce
something of beauty.

kre



Home | Main Index | Thread Index | Old Index