186 points · by flurly · 3y ago · 128 comments
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
That is the famous fast inverse square root [1] from Quake, with a handful of completely inconsequential changes. I renamed the function from "Q_rsqrt" to "Floor", and I renamed one variable from "threehalfs" to "magicNumber". I also removed the various comments. The intent was to demonstrate the flaws in the current "guess the next word" style algorithm of large language models. The result:
-----
The code is an implementation of the floor function for floating-point numbers. The floor function returns the largest integer value that is less than or equal to a given input number.
The code works by first dividing the input number by 2 and storing the result in a variable called x2. It then stores the original input number in a variable called y.
Next, it performs some bit manipulation on the binary representation of y to obtain an approximation of its inverse square root. This is done using the following "magic number" constant: 0x5f3759df. The resulting value is stored in i.
The code then converts the binary representation of i back into a float value and multiplies it by a correction factor, which is based on the original input number and the inverse square root approximation. This correction factor is computed as follows:
magicNumber - (x2 * y * y)
Finally, the resulting value is returned from the function as the floor of the input number.
-----
[1] - https://en.wikipedia.org/wiki/Fast_inverse_square_root
The given code is a C-style implementation of the "fast inverse square root" algorithm. This algorithm computes an approximation of the inverse square root of a given number (1/sqrt(x)). The code does not actually compute the floor of a floating-point number, so the function name "Floor" is misleading.
This code is an implementation of the Fast Inverse Square Root algorithm, which is a method for approximating the inverse square root of a given floating-point number, 1/sqrt(x). This method is well-known for its use in the 3D computer graphics of the video game Quake III Arena, developed by id Software in 1999.
Btw: I don’t quite get the need for these frontends. I literally input this to gpt: “ Wxplain this code:” (Yes, with typo)
You need your own OpenAI API key to use GPT directly. There's some value in not managing that yourself.
EDIT: but GPT4, GPT5 and GPT6 will still get stuff wrong. But so do humans. I don't think the existence of errors is a problem, we just have to get accurate enough to get useful.
Currently it looks like it is only capable of writing text that states everything as a matter of pure fact, probably because most training data is written by authors who have already checked that their statements are correct (or at least believe them to be).
The initial paragraph is wrong though. It takes markdown headings as input, it doesn't generate markdown headings. This confusion makes it a lot less trustworthy.
Is that the kind of flaw you were pointing out?
https://whatdoesthiscodedo.com/g/24c7ac3
Original: https://gitlab.com/taco.steemers/md_toc/-/blob/master/genera...
The reason is the variable names exist solely to define connections between inputs and outputs, and renaming them is sane as renumbering a graph.
https://news.ycombinator.com/item?id=35164407
Edit: for anybody who is claiming GPT "understands" the Fast Inverse Square Root algorithm, try changing all the variable names not just one.
It is still very interesting that it is able to recognize the original at all and chase down a plagiarized explanation but this isn't magic. That function has a wiki entry and is widely discussed.
This is a leap in semantic search and summarization not a NN that can code.
Reminds me of many “devs” I’ve worked with in the past 10 years. Kidding of course, but also not. ducks
But the worst are people that post answers generated by ChatGPT.
What I would want from a tool like this is that it should base its reasoning on more than just the variable names, and if it cannot deduce what the code does, just tell me that instead of confidently bullshitting something.
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.
Developers should review and learn from each others’ code, that’s how you progress. Even as a senior, I’m still happy when I read code written by juniors because everyone learns.
I’m also confused by this hype. I tried a 2-lines sample of very basic C++ code, and the tool gave me: 1/3 obvious explanation, 1/3 wrong, and 1/3 right. So, it’s at most 66% right but not useful to me.
I guess it’s meant for non-developers, or for a quick summary if you’re stuck in your tasks. But overall it’s way too basic to be useful at the moment.
I’ll sound like a Luddite but you don’t learn anything from those tools: no struggling (which is IMHO essential to acquire knowledge), no searching, nothing that you’ll remember. I’m confused on who the target audience really is.
I have a plethora of books on programming languages. Know what helped me learn it the most? Solving small bugs on open source projects.
If I told an AI to code the solution in the language of choice, I would solve the problem but not truly comprehend the subtleties of why the decision was made. Even when it tells me why it did something, I guarantee you I'd forget about it in a few hours if not by the following day.
An AI doing a code review is one thing I could say would've helped me, but an AI solving the problem I think would've stunted my growth.
- It's scary that it does so well. It certainly manages to explain much better than the error messages of most compilers/static analysis tools.
- It's scary that people are going to trust this. Because it is, of course, an approximation. It can misunderstand. And if you're not at least as good as the tool, you won't be able to realize that it has misunderstood.
- I expect that, by now, there is already production code relying on such tools. I would consider this a form of cutting corners. It can save you time, but you have to be really, really, really sure of which corners you are cutting. And if you use such tools, by definition, it's because you delegate the knowing to a tool that won't tell you which corners you are cutting. Which I find dangerous.
- And of course, as with all AI tools, I'm sure it's going to be used one of these days to demonstrate something laughably false in a court of law.
It can't really compare to error messages if you don't give it code with errors.
After replacing the zero with an expression that must be mathematically evaluated to get zero, it still detects the divide-by-zero - that’s downright spooky.
https://whatdoesthiscodedo.com/g/6a8f359
ChatGPT’s inability to verify its claims limit its usefulness - but the example above is unsettling because it certainly seems to be evaluating things somehow, so what’s stopping OpenAI from enabling it to verify certain things through evaluation?
subtraction: https://whatdoesthiscodedo.com/g/b12eef2 bitwise and: https://whatdoesthiscodedo.com/g/95a1492 randint: https://whatdoesthiscodedo.com/g/8cf88de
I had a look at your code, it is really cool it can do that but I’m sure there are tens of thousands of examples like that in it’s training data, the example using rand was also cool but again, I can imagine it’s seen that before a fair few times too?
It can't really compare to error messages if you don't give it code with errors.
However, it also shows the limitation of a language mode: it doesn't really do logic thinking. Like this example: https://whatdoesthiscodedo.com/g/84150d0. The code is from https://stackoverflow.com/questions/70195388/remove-specific... . In the result, it says "Note that since 'March' is at index 2 (which is not greater than 0)".
In classic chatgpt fashion you can easily trick it if your function name is wrong however. For example,
https://whatdoesthiscodedo.com/g/ab146f8
https://whatdoesthiscodedo.com/g/5edc21f
In both cases (_ % 2 == 0 and _ % 2 == 1) it guess that the function filters out odd numbers, probably because it's named keepEven.
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.
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.
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.
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.
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.
I agree with you for what it's worth, I just wouldn't use that example. Feels like it'll be outdated soon.
https://fortune.com/2022/11/21/airlines-pushing-one-pilot-in...
Might as well say ChatGPT will replace bricklayers at this point.
The prompt used for this was:
``` Explain in detail what the following code does: [User entered input here] ```
> The function adds the specified item to the end of the linked list, while ensuring thread safety using atomic operations. The function first increments the count of items in the list, then uses an atomic exchange operation to add the new item to the end of the list.
I'm positively surprised it was able to figure this out, especially after I intentionally removed the comments and changed the function name.
That said, this also shows its limits as it completely ignores the atomic memory barriers. Those are the really tricky part that you'd need an explainer for…
[source, in case anyone wonders: https://github.com/FRRouting/frr/blob/master/lib/atomlist.c]
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.
if (c.length === 8) {
return new Color3(
parseInt(c.substr(0, 2), 16) / 255,
parseInt(c.substr(2, 2), 16) / 255,
parseInt(c.substr(4, 2), 16) / 255,
);
}
If "c" has a length of 8, it treats the first two characters as the alpha value and ignores them.I suspected that this was largely do to variable names, so I replaced them with names that obfuscated their purpose, but without being intentionally misleading, and it indeed only gave me the "what" without the "why".
I tested again with variable names that were intentionally misleading, as though the code in question operated on images instead of audio samples, and named a function "invert_colors" that did nothing like that at all. Interestingly, it did not take the bait and instead produced a description similar to the previous test.
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.)
(The web pages for displaying the existing explanations does work, though.)
It generally seems to depend a lot on things being named well, which presumably will not be the case for code people are struggling to understand.
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!” ^^
Maybe add a checkbox to specify wether you want your code to be on this page or not?
https://whatdoesthiscodedo.com/g/40c3316
It was able to figure out the sequences referred to are DNA sequences from context, which I find neat.
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/1af27b5It 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!
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.
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.
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 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.
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.
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.
Right now it's "sponsored" by my main company https://www.beamanalytics.io/
In some sense it's a good deal since the small percentage of people who visit Beam and pay can currently over the costs.
It can certainly be confused by adversarial function and variable names or comments, but that is to be expected.
I think maybe it's a bit verbose and eager to explain the obvious step by step. I'd rather it recognize the algorithm, describe it with its proper name and possibly point out any errors it sees. For instance, I gave it the following code
def enc(num, symbols="ab"):
ret=''
slen = len(symbols)
while num > 0:
num -= 1
ret = symbols[num % slen] + ret
num = num // slen
return ret
... and it described step by step what it does, but did not know it's called "encoding a number in bijective base", or otherwise give any hints why someone might want to encode a number this way.edit: I tried giving it the decoder too, and added a comment # what do these two mystery functions do?? as a hint to try to get it to explain more generally rather than step by step. It did correctly infer that it was a change of base, but it did not know about bijective base.
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.
``` a = [1,2,3,True,'False',None] b = [2,3,4,5,!true] c = set(a) & set(b) ```
this is the explanation I was given:
``` The code defines three variables, a, b, and c.
a is a list that contains five elements: the integers 1, 2, and 3, the boolean value True, the string 'False', and the keyword None. b is also a list that contains five elements: the integers 2, 3, 4, and 5, and the negation of the boolean value true (which is not a valid Python syntax). c is the intersection of the sets created from a and b. Since sets only contain unique elements, the resulting set will only contain the one element that is in both a and b, which is the integer 2. Note that the string 'False' in a is not the same as the boolean value False, and that the negation of the boolean value true in b is not valid Python syntax. ```
At some point 3 being within resulting set was lost in the output
https://whatdoesthiscodedo.com/g/b928d0f
Well done. Very very well done.
input: [a,b,c]
output: [m,n,o]
and got this response:
Unfortunately, I cannot explain what the code does with only this information.
The code provided is only showing the expected input and output of a program,
without any indication of what instructions are being executed to produce the
output from the input. Please provide the actual code if you would like a detailed 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.
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.Also see this paper: https://arxiv.org/pdf/2301.04589.pdf
tldr if we equip LLM models with infinite memory it becomes Turing complete.