Re: String with integers?
* Jorgen Grahn:
On Wed, 01 Apr 2009 10:24:28 +0200, Alf P. Steinbach <alfps@start.no> wrote:
* James Kanze:
...
(The basic problem, of course, is that
there are many valid conversions of an int to a string. In
anything but toy programs, you generally have to specify
some of the details.)
...
Whether the program is toy or the next Photoshop has nothing to do with it; in
both kinds of program the default simple conversion is the one most often
required, and the zillions-of-options one the one least often required.
I find that with integers, I usually have to specify the width, for
alignment in tables.
Well, that's also the case for strings. And when you can do that for strings,
then why should that functionality be duplicated for the conversion of integer
to string? That redundant functionality just adds complexity and (when it's
used) makes it harder to do simple things like aligning headers in columns, and
collides directly with the principle of separation of concerns.
And speaking of separation of concerns, if you need a tabular presentation, why
try to shoehorn also that into conversion integer -> string.
Why not, regarding that presentation issue, generate just a CSV file/stream of
data, or XML, whatever pure data format (one concern, data) and run it through a
filter generating e.g. XHTML (second concern, presentation)? It's simpler. And
it's far more flexible. And it allows you to avoid loss of precision. And it's
better for compatibility with other programs and future versions of current one.
Often hex too, although I guess most other
applications do not print a lot of hex. With float and double, I
think you almost *always* want to specify the precision, so you do not
mislead your users by printing lots of digits which are deep within
the numerical error.
Yah.
The nice thing about C++ is that we can choose, and many choose to not use
iostreams except for -- heh, toy programs. ;-)
With other peoples' classes, you have no choice. If they can be
printed at all, it's by placing them on an ostream. There is no other
universally accepted way.
Huh.
The PC I'm writing this on is chock full of classes whose objects can generate
textual representations of themselves in various ways, and so it is on every
Windows PC; there are thousands of classes. Some of them are COM classes, others
are .NET classes. I've yet to see any of them printing to C++ std::ostream.
So there. :)
Not that I necessarily recommend either technology mentioned above for any given
purpose.
It's just that, by simple inspection of nearest computer, your "universally
accepted way" seems to actually be very much far less than universally accepted.
Cheers & hth.,
- Alf
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!