Eof - to return or to throw?

Dawid Ciężarkiewicz dawid.ciezarkiewicz at asn.pl
Tue Feb 13 07:45:31 PST 2007


Dawid Ciężarkiewicz wrote:

>> try {
>>    while (true) {
>>       auto amount = io.readWithThrowingEof(buf);
>>       useData(buf, amount);
>>    }
>> } catch (Eof e) {
>> }

Kristian Kilpi has given me an idea:

uint amount;
while (io.notInEof()) {
   amount = io.readWithThrowingEof(buf);
   useData(buf, amount);
}

With inlining this code should be as fast as:

> int amount;
> while ((amount = io.readWithReturningEof(buf)) != EOF) {
>     useData(buf, amount);
> }

and is in at the cost of one line far more readable IMO.

Io.notInEof() is not a problem - all implementation will have to have it
probably. It's worth noting that replacing readWithThrowingEof with
readWithReturningEof in this code will not change anything. So in such
simple snippet of code we could have a loop that could do the same for both
readWithThrowingEof and readWithReturningEof without difference.

If you have no comments I will have to take back your point for snippet in
which returning is better throwing. :>




More information about the Digitalmars-d mailing list