Eof - to return or to throw?
Dawid Ciężarkiewicz
dawid.ciezarkiewicz at asn.pl
Tue Feb 13 06:46:54 PST 2007
Frits van Bommel wrote:
> You say multiple reads is much better with a try block? (And you use
> line count as a metric?)
As was thinking about multiple reads in the code. Like:
auto data = read(buf);
auto dec = make_decision(data, buf);
switch (dec) {
case Dec.FIRST:
/* read more */
auto data = read(buf);
case Dec.SECOND:
/* etc etc */
auto data = read(buf);
}
Of course real code is not that simple, but I hope this will clarify what I
mean.
> Let's read until end of file:
> ---
> int amount;
> while ((amount = io.readWithReturningEof(buf)) != EOF) {
> useData(buf, amount);
> }
> ---
> vs.
> ---
> try {
> while (true) {
> auto amount = io.readWithThrowingEof(buf);
> useData(buf, amount);
> }
> } catch (Eof e) {
> }
> ---
> 4 lines vs 7 lines, and one less indentation level when not using try
> blocks.
>
> I prefer the first one.
Yes, the first one is a little better. But mostly because throwing everyting
into while statment. Although I must admit - this is such case where
returning is more handy then throwing.
Frits van Bommel scores. :)
More information about the Digitalmars-d
mailing list