Fork me on GitHub

Since I’m writing JavaScript, I’m using VSCode, and it has copilot. I’m getting freaked out by it.


To explain… I have a constants that I declared:

const TWO_32 = 0x100000000;
It’s 2^32, and it one larger than the largest number that can fit into an unsigned int (which is the INT_MASK value). ie. it has overflowed into the 33rd bit position. I also have a function called unsignedLonger, which converts a 32-bit number into a positive number, even if the top bit is set to 1 (which normally makes it negative). This means, for instance, that 0xFFFFFFFF will be returned as 4294967295 instead of -1.


With that in mind, I was about to port this Java code (which I’m sure that copilot has already seen and analyzed):

boolean carry = (sum >>> 32 != 0);
while (xIndex > 0 && carry)
    carry = ((result[--xIndex] = x[xIndex] + 1) == 0);
Copilot suggested this:
var carry = (sum >= TWO_32);
while (xIndex > 0 && carry) {
    sum = unsignedLonger(x[--xIndex]) + 1;
    result[xIndex] = sum & INT_MASK;
    carry = (sum >= TWO_32);


This is entirely correct. But I had to check it carefully, because… what the hell!


That's neat. My feeling right now though is how often can it be wrong, and does it take me longer to validate it isn't wrong, or to just do it from scratch myself?


Like when you say you had to check it carefully, did that take you more time than it would have to convert it yourself?

Martynas M16:01:43

It's code without tests. This is why it took time. I think it should be the same as reading a function written by another person and without tests.


It does take me longer to check it, and sometimes it’s wrong. Worse, sometimes it’s subtly wrong. But I’m amazed at when it gets it right


> I think it should be the same as reading a function written by another person and without tests. That often takes me longer then writing it myself haha


I guess maybe it would work really well in a TDD workflow though, since you'd write your tests first, then auto-complete the full implementation if possible and just see if it passes your tests

👀 1
Martynas M05:01:08

I can already imagine having a seed for the generator and then only writing tests. And having a source directory which you can't ever edit... And for mutation testing... you only change the seed xD


That would kind of be crazy, in the future, programmers only write tests lol, Copilot writes all the implementation for you