A few notes on choosing between Go and D for a quick project

Walter Bright via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 18 15:11:03 PDT 2015


On 3/18/2015 4:53 AM, Elazar Leibovich wrote:
> On Friday, 13 March 2015 at 17:31:09 UTC, Andrei Alexandrescu wrote:
>>   File("/tmp/a").byChunk(4096).joiner.startsWith(s)
>>
> Something like
>
>      while (n != EOF) {
>          n = read(fd, buf, sizeof(buf));
>          if (n==-1) throw(...);
>          if (strcmp(buf, PREFIX) == 0) {
>               return buf;
>          }
>      }
>      return NULL;
>
> Requires no prior knowledge, and have similar effect.

The trouble with the latter example is it's full of bugs. Part of the whole 
point of the former example is it's much more likely to be correct.

* should use memcmp, not strcmp which depends on a 0 termination

* the entire buf is not necessarily read

* what happens when buf is entirely read, and strcmp reads past the end

* read() sometimes requires multiple calls to fill a buffer

* the former example is complete while the latter is missing a LOT of code, like 
opening the file, closing it, etc.

I don't want to rag too much on you for the bugs, but having buggy file reading 
loops like this is terribly commonplace. (I've done it wrong many times, too, 
and I even implemented read() and strcmp().)

Ranges+algorithms are much more likely to be correct. Yes, there is a learning 
curve to it, one has to reorient one's thinking and learn new names and what 
they do. But it's worth it, the payoff is big.


More information about the Digitalmars-d mailing list