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

Elazar Leibovich via Digitalmars-d digitalmars-d at puremagic.com
Wed Mar 18 04:53:05 PDT 2015


On Friday, 13 March 2015 at 17:31:09 UTC, Andrei Alexandrescu 
wrote:
>
> For example the expression (assuming s is e.g. a string)
>
>   File("/tmp/a").byChunk(4096).joiner.startsWith(s)
>
> opens a file, progressively reads chunks of 4KB, stitches them 
> together at no cost, compares against a prefix until it makes a 
> decision, then closes the file and returns the result. A 
> putative Go user wouldn't even dream of using HasPrefix 
> directly on a stream coming from a file; the whole endeavor 
> would be a function that painstakingly takes all of these steps 
> by hand.

I personally, would have no idea what this piece of code is doing
upon first sight. I'll have to look at the documentation of
at least two functions to understand that, and I'll have to
think carefully about what and who would throw in case of an 
error.

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.

I'd rather have a loop written by hand in my production code any 
day,
so that when debugging it, and reading it I'll have easier time
to understand it, even though it would cost me a few more lines
when writing the code.

What if this pattern repeats a few times in the code?

I'd rather have a single function that have the explicit loop than
having this pattern in slight variations spread in the code.

Writing code is easy, maintaining it afterwards is costly.


More information about the Digitalmars-d mailing list