A Philosophy of Software Design
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Sun May 24 06:18:36 UTC 2026
On 24/05/2026 5:57 PM, Walter Bright wrote:
> Microsoft is not wrong. You're not wrong, either.
My PoV is that you gotta use the right tool for the job.
Each strategy has its own strengths and weaknesses, and all of them
should be supported. As required.
> But consider the copyright dates on the books: 1993, 2003, 2004. I know
> my ideas on what is better certainly have evolved over the years. A
> number of problems have come up with exceptions. I suppose it's like
> macros - they are a great idea, until you've used them for 10 years, and
> then they don't look so good.
In this particular case, the ideas presented in those books haven't
evolved since then. And the conclusion is to use a mix of strategies
based upon what the code needs to do.
I've done plenty of research over the past few years on the topic, and
haven't found anything notable.
> Let's take Unicode again. The current method in Phobos is autodecode,
> which turned out to be a bad idea. It also throws an exception on
> malformed code points.
Funny that you mention that.
"When a conversion algorithm encounters such unconvertible data, the
usual practice is either to throw an exception or to use a defined
substitution character to represent the unconvertible data. In the case
of conversion to one of the encoding forms of the Unicode Standard, the
substitution character is defined as U+FFFD REPLACEMENT CHARACTER.
For conversion between different encoding forms of the Unicode Standard,
“U+FFFD Substitution of Maximal Subparts” in Section 3.9, Unicode
Encoding Forms defines a practice for the use of U+FFFD which is
consistent with the W3C standard for encoding. It is useful to apply the
same practice to the conversion from non-Unicode encodings to an
encoding form of the Unicode Standard.
This practice is more secure because it does not result in the
conversion consuming parts of valid sequences as though they were
invalid. It also guarantees at least one replacement character will
occur for each instance of an invalid sequence in the original text.
Furthermore, this practice can be defined consistently for better
interoperability between different implementations of conversion."
- Unicode standard 17, page 323, 5.22 U+FFFD Substitution in Conversion.
https://www.unicode.org/versions/Unicode17.0.0/UnicodeStandard-17.0.pdf
TLDR: exceptions and replacement characters are both nominal approaches
in Unicode handling.
> The first problem is you cannot use any string code in Phobos without
> supporting exceptions and memory allocations. The second problem is
> throwing an exception is the wrong solution.
It is not necessarily the wrong decision, which is why its so predominant.
We have had trouble with it sure, and we would like to change it. But
the problem domain does suggest it /can/ be the right choice. Even if I
personally wouldn't do it with exceptions. There are some very nice
boundaries in the algorithms and plenty of opportunities for
specialization of functions including of decoding for specific error
handling requirements.
> Consider displaying text in your editor. Do you want the editor to throw
> an exception if the text has bad code points in it? Do you want the
> browser to throw an exception if the html has bad code points in it? No.
> Better to render the bad code point as the invalid code point.
If that exception is escaping the editor component rendering and causing
the program to crash, that is a MAJOR BUG. Exceptions are supposed to be
caught and handled much more locally in components like this, if there
is potential for them to be thrown. Under no circumstances should that
exception impact the user beyond a error alert. You should try Intellij
out some time, sadly the D plugin still produces exceptions but yet its
still quite functional. When applications are written properly, they
handle exceptions gracefully and don't just fail at the first bad input.
> If you really, really need to throw an exception, run the text through a
> *separate* filter to throw when it sees an invalid code point.
>
> (Personally, I really do not want my string handling functions throwing
> exceptions. Like searching for a substring - what good would an
> exception be?)
>
> Anyhow, I recommend checking the book I mentioned. It's only ten bucks!
> I'm pretty sure it will be worth your while.
I considered it, but the $70 price tag ended that thought process beyond
adding to a wish list lol.
More information about the Digitalmars-d
mailing list