A Philosophy of Software Design

Walter Bright newshound2 at digitalmars.com
Sun May 24 21:00:35 UTC 2026


On 5/24/2026 7:51 AM, H. S. Teoh wrote:
> On the contrary, you yourself said back in the day that exception
> handling was a good idea because it forced the program to stop upon
> encountering an I/O error.  As opposed to a C program ignoring the
> return value of printf(), et al, and blindly barging forward when an
> unexpected condition like a disk full error started happening.

Yes, I did say that. But as I also wrote, my thinking has evolved over time and 
experience.

printf does two things:

1. format arguments into a character stream

2. write the stream to stdout.

#2 is where the errors happen. #1 is without error (at least in D!).

So, I have advocated the "sink" or "output range" approach, which would look 
like this:

```
printf(sink, format, args...);
```

printf shouldn't know anything about sink, it should just send the characters to 
it. You can see this in the dmd implementation, where OutBuffer is the sink for 
formatted characters, and ErrorSink is used to accept messages about errors. By 
consolidating output into sinks, one does not have to check for an error at 
every invocation of printf.

I still use printf in dmd, but only for the porpoise of printing debugging 
information so I can debug the compiler. I use snprintf() for when things 
matter, because snprintf() accepts a (primitive) sink as an argument.



More information about the Digitalmars-d mailing list