Samuel,

One thing I don't recall seeing in this thread is a discussion of the
semantics of the methods names.

I find it helpful to consider #asWhatever to be a conversion method, used
to convert one object to another essentially compatible class. e.g.
#asFloat sent to an integer would be expected to yield a floating point
number with the same value, but #asInteger sent to a floating point number
would lose precision but might still be sensible for some purposes. [more
on this below]

But, #printString and its ilk (#displayString, #storeString, others) are
not intended to be conversion methods. They are to provide an external
representation of a format suitable for the intended audience. Typically,
(and I say that guardedly), #printString's intended audience is the
programmers working on the system and #displayString is intended for the
system's users.

In simple cases, #printString is reversible and can be parsed to recover
the value of the original object. In general, it doesn't include enough
information to do so. The same is true of #displayString. If we consider a
slightly more complex example than a number, consider a User Profile. Its
#printString might be 'UserProfile(John Doe)'. Its #displayString might be
'John Doe'. Neither of those provide all the details of the object,
although they could be parsed and so used to look up an existing object.


----------
Continuing from [more on this below] above...

I find #asString is one of the most problematic methods that I have ever
seen. It is deceptive, an "attractive nuisance", as one colleague liked to
say. Clarity comes from being explicit in one's programming. Say what you
mean and mean what you say. If you are trying to convert from one object
form to another, use a conversion method whose name is precise. If you are
trying to provide information for a programmer about an object,
#printString is the choice.(e.g. in a stack dump).

As a general rule, #asWhatever methods that are generally applicable to the
set of possible values. e.g. number conversion methods are a good example,
even though there are exceptions (loss of precision, NaN, and Inf are some).


I have seen a lot of #asWhatever methods added to the String hierarchy, but
almost all of them are dubious, in my opinion of course. Most strings are
not numbers, for example. String>>#asNumber is one of the worst things I
have ever seen, both from a consistency perspective and an implementation
perspective. 'abc123def456' asNumber has more variant results than I can
enumerate.



On Mon, Oct 14, 2019 at 9:28 AM Samuel Teixeira Santos <arcano...@gmail.com>
wrote:

> Very clear to me now.
>
> Thank you and to others too by your previous answers
>

Reply via email to