Re: [eclipse-users] sprintf?

From: Warwick Harvey <wh_at_crosscoreop.com>
Date: Tue, 11 Sep 2007 09:44:56 +0100
Hi Kish,

Sorry for the slow response - been more than a little busy lately.

On Mon, Aug 27, 2007 at 07:24:06PM +0100, Kish Shen wrote:
> How do you use this predicate? If you are creating a string that is made 
> up of atomic terms, it is
> probably easier to use concat_string/2. I can see it being useful for 
> printing formatted terms, but
> do you want to put this into a string often?

Most often it's when I'm writing utility predicates, and I want to provide
the user maximum flexibility in the format of something but the lower-level
services I'm using accept only strings.

Examples where I've used sprintf:

 * Writing a data collection to a set of files, where the format of the file
   names is specified by the caller (specified using a printf pattern for
   maximum flexibility).  I need to combine the format specified by the
   caller with appropriate arguments and obtain a file name.  I've used this
   in a couple of projects to format results into a set of web pages.

 * Formatting log or error messages or other output when they're not being
   immediately written to a stream (e.g. because they're going into a
   database, they're being passed to a lower-level routine that only accepts
   strings, or they can't be output immediately - e.g. because they should
   only be output if the currently executing code succeeds).

 * Formatting commands for passing to sh/1.  This probably could have been
   done with concat_string/2, at least in the particular instance where I
   used it.

 * Constructing the name of a database table where concatenating atomic
   terms is inadequate (specifically, we wanted the integer counter to be
   zero-padded so that it took up a fixed number of characters in the name).

The other thing to bear in mind is that often it's better to use something
like sprintf/3 instead of concat_string/2 - if, for example, readability is
more important than efficiency.  (At least, I usually find it more readable
- can't guarantee that everybody would agree with me though. ;) )

Cheers,
Warwick
-- 
!umop apisdn papuadsns w,I - aW dlaH aseald
Received on Tue Sep 11 2007 - 09:44:58 CEST

This archive was generated by hypermail 2.2.0 : Thu Feb 02 2012 - 02:31:58 CET