On 2015-03-23 at 00:15, krzaq wrote:
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'm surprised nobody commented on "no prior knowledge". How are you supposed to 
guess what strcmp(buf, PREFIX) does if you don't know the function? Why is it being 
compared to 0? What's this -1 magic number? What does read do? What is EOF? Unless you're 
born with C/POSIX programming knowledge, this is prior knowledge. I know C well enough, 
but it took me some time to understand what it does.

Indeed. I know of strcmp (because of prior knowledge) but had to have a look at "man 
2 read" just in case to verify if read was used correctly. Of course there is also 
this conveniently omitted part in throw(...). Throw what? Use switch to go over errno and 
pick the suitable Exception or rather pick one generic Exception and put the output of 
strerror as its msg? Why should we be bothered with such low-level tasks?

And honestly, compared to
  File("/tmp/a").byChunk(4096).joiner.startsWith(s)
you can *easily* guess that you have a file - do some nonobvious magic on it - 
and check if *it* starts with `s` just by reading it as plain English.

Now you've injured yourself with your own weapon. I can guess that File(path) 
opens the file for reading (probably because of other language libraries) and 
that byChunk(size) reads it one chunk at a time (but frankly only because it 
looked similar to byLine which I've known before), but what the hell is joiner? 
Does it glue ranges together so that they appear as a single contiguous one? 
Oh, wait, I may have actually read about it somewhere already. But if I didn't, 
I wouldn't have a clue.

What should start with s? The file, any chunk, the joiner - whatever it meant? 
It is much clearer than the loop, but I'm not sure I'd guess what it does, 
because of the two middle elements in the UFCS chain. This *nonobvious magic* 
may have transformed the contents of the file in a way that makes startsWith(s) 
do something different.

Reply via email to