Show HN: AI explanations for other people’s code (whatdoesthiscodedo.com)

186 points by flurly ↗ HN
Hi HN,

As a developer, I much prefer to write code than read other people’s code. But most projects I work on involve other developers so it’s hard to avoid. Often I find it really hard to quickly parse other people’s code so I thought maybe ChatGPT could help.

ChatGPT does a really nice job of giving clear explanations when you paste in code and ask it to explain it. But the interface is a bit of a pain to use if you’re using it many times a day. It’s also hard to share explanations with co-workers. So I built whatdoesthiscodedo.com. Just paste your code and get your ChatGPT explanation and a sharable link you can give to coworkers.

I’m hopeful it can also be helpful to folks who aren’t professional developers. My co-founder at my main company, Beam Analytics, is more of a product guy so only needs to write scripts etc. Before, he’d often just find code online to copy and then struggle to make it work. But with this tool, he says he’s getting better intuition in understanding the code he’s using, and how to modify it.

We’d love your feedback. Email us at hi (at) beamanalytics.io or DM me on twitter @TheBuilderJR

135 comments

[ 5.2 ms ] story [ 187 ms ] thread
(comment deleted)
It works pretty well!

I would add a big red disclaimer such as: “be careful: putting here code source from your job is probably highly against the rules of your company, and you may get FIRED!” ^^

Thanks Sebastian! That's a good point. I've also thought about writing a classifier to detect sensitive information like API Keys or passwords. Let me see what I can add without regressing the UI too much!
Also, I guess it’s not very clear that each piece of code will be shared on the “explanations” page. That sounds pretty dangerous ;)

Maybe add a checkbox to specify wether you want your code to be on this page or not?

Good call! That serves both purposes. Implementing right now :)
I really want to see it explain the famous Quake fast inverse square root, but apparently it's extremely rate limited right now
Should be back up... for now!

  The specific operations involve subtracting the integer value from a predetermined constant (0x5f3759df), shifting it right by 1, converting it back to a float, and then performing one iteration of a Newton-Raphson approximation using the threehalfs constant and the original input value. There is also a commented-out line for performing a second iteration, but it is not necessary for most use cases.

So this is actually the perfect counter example to all the other "impressive" ones as this particular explanation can't be derived from a pure reading of the code.

It correctly recognized what it was given as the quake inverse square root algorithm and plagiarized bits of the explanation thus sounding l33t!

Still, very useful.

It is interesting to see what happens when we rename the function and all the variables.

https://whatdoesthiscodedo.com/g/9f22a12

    The code defines a function called "myfunc" that takes a single input parameter n of type float and returns a value of type float. The function performs some numerical operations on the input value in order to calculate an output value.

    First, the input value is multiplied by 0.5 and stored in a variable called y. The input value itself is also stored in a variable called z.

    Next, the function does some bit manipulation using pointers to convert the value of y from a float to a long integer, perform some bitwise operations on it, and then convert it back to a float. This step is not immediately clear what it's doing without knowing the specific algorithm being used.

    Finally, the function calculates a new value for z using the original input value n, the modified value of y, and a constant value called tihihi. The exact formula used to calculate the new value of z involves multiplying a term that depends on y and n by a constant value tihihi, and then multiplying the result by the modified value of z.

    Overall, the purpose and meaning of this code is not immediately clear without additional context or knowledge of the specific algorithm being implemented.

In this way we can sort of gleam what part of the understanding comes from the AI encountering that code snippet before, and what part comes from a more fundamental understanding.
The explanation isn't very elaborate, e.g. it does not explain why it uses that specific constant number.
Looks like the site is melting a bit from being on the front page. Bear with me as I try to diagnose and fix the bottleneck!
Perhaps you can ask GPT to fix it ;)
With all the hype and euphoria over ChatGPT, all we get is a site which is asking you to paste in code you probably copied internally from work or StackOverflow to give an opaque explanation about what is wrong with it.

Even most of the time, it generates broken code as you attempt to use more sophisticated language features (especially Rust) and confidently generates code that doesn't compile or is flat out incorrect.

I guess you're spending more time reviewing the broken AI code more than actually writing the code itself. On top of that, no-one here would be comfortable to paste in code that is internal to their work on a random website either.

> With all the hype and euphoria over ChatGPT, all we get is a site which is asking you to paste in code you probably copied internally from work or StackOverflow to give an opaque explanation about what is wrong with it.

I can see how that might be useless to you and to quite a few others but we have many distributed down the learning curve. Someone who can deliver a better explanation might be expensive and their time shouldn't be wasted on questions the model can answer reasonably.

> Even most of the time, it generates broken code as you attempt to use more sophisticated language features (especially Rust) and confidently generates code that doesn't compile or is flat out incorrect.

I think you are throwing out the circuit board with the bath water. Go hook up a compiler to it? Patches are welcome?

> I guess you're spending more time reviewing the broken AI code more than actually writing the code itself.

This seems to me a task that, unlike writing code, many people could do in parallel. You could even measure peoples performance by counting the issues they didn't find.

> On top of that, no-one here would be comfortable to paste in code that is internal to their work on a random website either.

Perhaps, I cant talk for others, but much of my stuff is done on the front end.

> With all the hype and euphoria over ChatGPT

This thing is going to take your job, and here you are downplaying it.

I write Rust on a daily basis and have gotten plenty of good suggestions.

Have you seen how fast this is accelerating? All the money and attention in the world is going to descend upon AI, and it will just keep advancing faster and faster.

The ChatGPT hype squad sure said the same thing towards dentists, doctors, surgeons, investment bankers and judges.

Might as well say ChatGPT will replace bricklayers at this point.

I tried it with some of the Perl source code in, and... it actually impressed me by explaining what a few of the variables mean, e.g. that "PL_lex_inpat" means "currently parsing inside a pattern (i.e., regex)."

On the other hand, it finished up with: "In summary, this code checks whether certain tokens are present in the input stream, and takes some actions or returns a boolean value accordingly." That's pretty much the grammatically correct but vacuous word-paste I expect from High Schoolers on a deadline and large language models.

I'm surprised and impressed. Given this code, which is probably obvious to a human who knows the context, I did not expect a clear and accurate "answer" from this tool.

    h1s = doc.xpath('//h1')

    h1 = h1s.first

    r = /(\d{1,2} (januari|februari|maart|april|mei|juni|juli|augustus|september|oktober|november|december) \d{4})/i

    h1.text.match(r)&.captures&.first
->

This code performs the following actions:

It selects all the h1 elements in an HTML document using XPath and assigns them to the variable "h1s".

It selects the first element from the "h1s" array and assigns it to the variable "h1".

It creates a regular expression that matches a date format consisting of a two-digit day, followed by the name of the month in Dutch, followed by a four-digit year. The regular expression is assigned to the variable "r".

It searches for a match of the regular expression in the text content of the "h1" element.

If a match is found, it extracts the captured groups (in this case, only one group corresponding to the entire matched pattern) and returns the string value of the first captured group.

In summary, the code attempts to extract a date in a specific format from the text content of the first "h1" element in an HTML document.

It's good.

I fed it the urlparse function from here:

https://github.com/chbarts/web/blob/master/web.c

And it came up with this:

https://whatdoesthiscodedo.com/g/e2a519c

This is a C function that takes a URL as input and parses it to extract the host, path and port.

The function starts by initializing variables i and j. Then it checks if the length of the URL is less than 7 characters. If so, it sets i to 0. Otherwise, it checks if the URL starts with "http://" using the strncmp function. If it does start with "http://", then i is set to 7. Otherwise, i is set to 0.

The next block of code checks if i is 0 and if the URL contains "://". If this condition is true, then the function returns -1 indicating an error.

The next line sets the host variable to point to the string starting at position i in the URL.

The following loop iterates over the characters in the URL from position i until it reaches either a forward slash (/), a colon (:), or the end of the string. The switch statement then handles each case separately.

If the loop finds a forward slash, it replaces it with a null character (\0) and sets port to null, indicating that no port was specified in the URL. It then sets the path variable to point to the string immediately following the null character.

If the loop finds a colon, it replaces it with a null character and increments i. It then sets port to point to the string starting at position i in the URL. The loop then continues to iterate over the remaining characters in the URL until it reaches a forward slash or the end of the string. If it finds a forward slash, it replaces it with a null character and sets the path variable to point to the string immediately following the null character. Otherwise, it sets path to point to the end of the URL.

If the loop finds the end of the string, it sets *port to null and sets path to point to the end of the URL.

Finally, the function returns 0 to indicate success. The host, path, and port variables are passed as pointers, so they will be modified by the function and their values can be accessed outside of the function.

It even does Common Lisp:

Admittedly, this is ultimately from Rosetta Code, but it's somewhat modified:

https://rosettacode.org/wiki/MAC_vendor_lookup#Common_Lisp

But I fed it this code:

    (defun mac-lookup (mac)
      (check-type mac string "A MAC address as a string")
      (multiple-value-bind (vendor status)
          (drakma:http-request (format nil "http://api.macvendors.com/~a" mac))
        (if (= 200 status)
            (format t "~a~C~a~%" mac #\Tab vendor)
            (if (= 429 status)
                (format *error-output* "error: Too many requests at ~a~%" mac)
                (format *error-output* "error: ~a~%" mac)))))

And it got this out of it:

https://whatdoesthiscodedo.com/g/5ae8501

This code defines a function named "mac-lookup" that takes a MAC address as a string argument. It then checks the type of the argument to ensure it is a string.

The function then sends an HTTP request to the API at http://api.macvendors.com with the provided MAC address appended to the URL. The response is stored in two variables, vendor and status, using the multiple-value-bind function.

If the status code of the response is 200, the function outputs the MAC address, a tab character, and the vendor name to the console.

If the status code is 429, which indicates too many requests have been made, the function outputs an error message to the error-output stream indicating that there were too many requests made for the provided MAC address.

Otherwise, if any other status code is received, the function outputs a generic error message to the error-output stream indicating that there was an error for the provided MAC address.

Correctly identifies some 3D game code:

    float x = Mathf.Cos(time * frequency) * amplitude;
    float y = Mathf.Sin(time * frequency * 2) * amplitude * 0.5f;
    float z = Mathf.Cos(time * frequency * 0.5f) * amplitude * 0.75f;
    return (x + y + z) * damping;
https://whatdoesthiscodedo.com/g/1af27b5

It would be cool if it identified the game engine (clue "Mathf"), or said something about the programming language. What it actually simulates is more obscure (would a human find out?), but got a general idea. Nice work!

> Overall, this code could be used to create a simple oscillating motion for an object in a game or simulation.

Damn, I was gonna say "it wiggles the object" but this covers it just fine. Between this and the GPT4 examples where it explains the humour behind a random joke, I can't help but wonder how much longer the "it's just some statistics" crowd will be able to keep it up.

It's a pretty useless statement anyway unless you are working in philosophy or a related field.

As with any technology the critical questions revolve about it's usefulness, it's limitations and it's risks.

Whether it's "just statistics" or whether some arbitrary goalposts were moved by someone is only useful for internet points. A breakthrough development does not need defending, if thrives no matter the head wind.

I like it. Bookmarked. Will use again for sure. As others have said, I was wondering what the privacy implications are, as I don't want to share what I paste there when it's code copied from work. Privacy policies are not helpful, just say plainly if what I paste there is viewable by others or not, or is stored somewhere.
Everything sent to openAI becomes licensed to openAI for training to help your competitors potentially
Yeh I hadn't noticed the other page where everything submitted is published!

I was upscaling old family photos on replicate.com the other night, and paused to wonder if the photos I put there will end up somewhere else. I'm still not clear about the fate of either the original uploaded, or the resulting image.

You shouldn’t even consider pasting $work code into a random site on the internet, full stop.
Depends. The code I was pasting is public anyway (website javascript), so quite harmless for this purpose. It contained some paths in the code that would identify where it came from, which I removed.
I'm surprised and impressed. I pasted in the BF hello world example code and it actually parsed it and wrote it out very clearly.

https://whatdoesthiscodedo.com/g/b928d0f

Well done. Very very well done.

There are tons of explanations out there for how that code works, it's not super surprising it can explain it since it is most likely regurgitating it verbatim. I'd be interested to see the result with a less common bit of BF code though
This looks fantastic! I guess you're calling the recently announced ChatGPT APIs under the hood, just curious, how much is this costing you right now?

Are you planning to leave this running as a free service? Surely it will be costing you a lot in terms of API pricing?

Most similar services running on the ChatGPT API seem to be open source or let you paste in your own API key, I'm guessing to make sure the users are paying for the API usage.

The web page is not working for me, but I figured out how to access it anyways.

I put the code:

  /F ARGUMENTS 0 get (r) file def
  ARGUMENTS length 1 gt {F ARGUMENTS 1 get cvi 175 mul setfileposition} if
After 6.567 seconds, the explanation comes:

> This code defines a PostScript function called "F" that takes an argument and sets it as the name of a file to be opened. Then, it checks if there is at least one more argument in "ARGUMENTS", the array where the function's arguments are stored. If so (i.e., if the length of ARGUMENTS is greater than 1), the code converts the second argument from string to integer with "cvi", multiplies it by 175 and sets the file position to that value.

It is almost right but not quite. It is a PostScript code (it gets that correct), but it does not define a function called F; it defines F to refer to the file that it just opened. ARGUMENTS is an array of the command-line arguments of the PostScript program (the ability for PostScript programs to take command-line arguments is a feature specific to Ghostscript, although even in a different PostScript interpreter you could just define ARGUMENTS yourself anyways), not the arguments of the function. The rest is correct. (Of course, the above code is not a complete program.)

Can you elaborate on what’s not working?
It does not display a form field to fill in the code, regardless of if JavaScripts are enabled or disabled. (But, I figured out how to access it anyways, without much difficulty. So, the fact that the web page is not working is not much of a problem, really.)

(The web pages for displaying the existing explanations does work, though.)

Interesting. I can’t reproduce. Is it consistent across browsers? Can you try turning off Adblock?
I’m not putting my code on that! I can’t imagine anyone being allowed to upload production code
That's why the title says other people's production code /s
If your code is private then you really shouldn't send it to them, but if you send code that is public anyways then it should not be a problem to send it there too
I cannot wait until Github-GPT fixes all the bugs in our event driven micro-services
I used it on a very simple ray triangle intersection function, which at first it did surprisingly well. But then I changed the name of the function to something completely different and all hell broke loose. The output was total nonsense despite the code being exactly the same.
Love it. I put in a function that was written in poly (a synthetic biology library in Golang):

https://whatdoesthiscodedo.com/g/40c3316

It was able to figure out the sequences referred to are DNA sequences from context, which I find neat.

Pleasantly surprised at it catching a memory leak:

https://whatdoesthiscodedo.com/g/28d19ac

Although, I should add that it incorrectly states that the last memory allocation will be preserved. It will not be preserved since the pointer goes out of scope.

> As a developer, I much prefer to write code than read other people’s code. But most projects I work on involve other developers so it’s hard to avoid. Often I find it really hard to quickly parse other people’s code

Another solution to this problem is documentation, which is as much about coding as coding is.

If code was named well instead of using shortened and abbreviated names which serve zero purpose, documented well, and crafted well, then you wouldn't have these problems and wouldn't need machine learning to hack away at explanations. The one thing software doesn't need is more layers between what a person thinks the code does and what it actually does.

> The one thing software doesn't need is more layers between what a person thinks the code does and what it actually does.

I get what you’re saying, but also can’t help but think how glad I am to not write assembly anymore. It’s abstractions all the way down anyway. If adding more layers can lower the barrier to code, the I’m all for it — and yes, including all the baggage it’ll bring along. It’ll suck for a while, but it’ll eventually empower a newer generation to do things they otherwise couldn’t have.

It's not necessarily the layers of abstraction. I would consider this to be a layer of indirection. A human writing code, then a machine learning algorithm learning from other human written code, and then another human using that machine learning algorithm to understand the original code seems like a rather needless and inefficient layer of indirection.