51 points · by swiftstart · 3y ago · 52 comments
So we decided to develop a basic web application that automatically integrates with your Github repository, generate comments, create a pull request and send you an email when it is all done.
There is definitely a lot more that can be done but we wanted to gain feedback on whether this is a problem that you face too. Do you often find it challenging to understand complex code? Do you have difficulties in writing informative comments? And if so, would you find value in a tool that can automatically generate comments for your code?
Really appreciate any feedback and suggestions! Thanks in advance!
However
There are couple reasons why I think this may be valid use case:
- Code is a static serialization artifact. It's unfortunate we still work with it directly, but that's another conversation; fact is, we are working with it directly, and if there is commentary relevant to the code, it's best placed in the code, so it remains if the tools generating them dynamically (like your "on-hover" IDE plugin idea) become unavailable or stop working (or start generating different outputs after some update).
- It's true that the comments should talk about "why" much more than "how", as the "why" is often not apparent in the code itself. However, "not apparent" doesn't mean "independent" - the code structures and the reasons for their existence are correlated. GPT-4 may have seen enough and be smart enough to actually spot those - as if it was a developer familiar with the problem space, going all "yeah, I've seen this type of code before - it's likely trying to ${high level goal}". Generating those comments would be valuable.
Of course, generated comments should be reviewed and edited at the point of generation, to make sure they're accurate and reference actual documents/tickets/design decisions. Whether or not an average developer will do that - that's another topic.
I was curious what was going on in a sine cosine lookup table and that was doing linear interpolation between the points.
Feeding it into chatgpt, added the appropriate comments for each step.
It was fairly obvious, but had taken me 10 minutes to track down and realize what was going on.
Could you hint at what it is we should be working with instead?
Views that give you a vertical slice though code, auto-inlining function calls when you need it, allowing you read and edit a large block of code, and propagating changes to their respective source locations - this is the solution for the "lots of tiny functions vs. few large ones" "clean code" pseudo-problem.
Views that let you control focus. No more constant dealing with exceptions vs. Result<T, E>, 50 shades of async, or how to mix logging into it all - problems to which modern solution seems to be all kinds of monadic bullshit that makes code completely unreadable, unless the language itself gives you some arcane syntax and semantics to hide it all. Instead, have your IDE hide all the things you don't care about - aka. cross-cutting concerns - from your view.
For example, are you working on the business logic, and focusing on what the code is trying to achieve, aka. the golden/success path? Have your IDE hide all error handling for you. Turn all the Result<T, E> return types into just T, making it look as if the code was using exception handling (and doing the handling somewhere else). Then do some vertical-slice auto-inlining to make a specific functionality more apparent. Too noisy with logging code? Turn display of that off. Conversely, if you're interested in error propagation, turn display of all the business code off.
(Think of it as Aspect-Oriented Programming on steroids, in an interactive form.)
This and much, much, more. It starts with a simple idea though: stop thinking in terms of source code as text in files. Start looking at it as semantic units (classes, functions, statements, expressions) in a database of some kind. Instead of opening a file and editing its text, you would query the database to get an abstract code graph, and feed it to a view that renders it the way you need it. "SELECT Foo Bar from Classes, JOIN Fields, JOIN Methods", feed it to an editable outline view. "SELECT" whatever else you care about, feed it through some transformer, to a different custom view. Edit it, and have it automatically apply changes/"refactorings" to affected code.
And yes, editing raw plaintext is something that's often very efficient and we have well-optimized tools for this. But this doesn't mean the plaintext in question has to correspond 1:1 with source code. Instead, you could have the class/module outline view be editable plaintext, so you could regex-replace half of it in 5 seconds, and then press a button, and it would rename and move methods and classes across the codebase, making it conform to your edited outline. Basically what dired mode does to filesystem in Emacs.
Etc.
Under mine, the AI should sign and date the comments, something like so
AI 20230524
Comments are an odd asset. Some of the most useful ones I've seen when debugging are rotted ones that describe the code that used to be there.
You'll then turn back the clock and find the bozo that "fixed" something by removing important code but for some reason left the comment behind. It's pretty common.
This AI tactic could protect against that so when the next bozo comes in with their wrecking ball fingers and leaves the ai comments behind, you can go in there with your mop and broom and recover things quicker.
What would be really nice is my favorite commenting technique which is where I write obvious but also counterintuitively very wrong code and I aggressively comment it like
"Hello, if you're here you may be asking yourself why it's not like this
(Wrong code)
I did too! That's what #231 and #302 are about! I know I know, I fell for it too. If you're going to change it, open a ticket or something because you're probably going to break it as well"
If the AI can do that then we're in gold territory
Proximity is really fucking important but of course is only a proxy for other practices and anti pattern avoidance
Regardless, if you need to engage, do so at the obvious point of engagement, not off in some test suite where you cross your fingers on predicting the future on the diligence of its upkeep.
I mean write the test, do your rain dance. It's not going to protect you against the stupidity you have to worry about, X years after you've left the building.
Your code will live longer than you think and be modified more times by more people, who you will never meet, then you realize.
You don't have to feel responsible for that. But I do and that's one of the examples of how I practice it. You're leaving notes for future archaeologists to remove their guesswork.
Again, tests are great, linters are fine, but your nth generational successors may not agree with you or how you did them or how often they should be run or... and there goes your hardwork. Don't rely on them for assuring protection past your tenure.
Future coders are probably 10 times more likely to curse you as a nuisance than appreciate your diligence. Assume they'll hate you.
There's also the AI evolution question. When we have GPT-10 next year, do we go through and regenerate all the comments? That would introduce a lot of noise into the repo's commit history and `git blame`, which I think is another indicator that the repo is not the right place to store this sort of thing. (And it'd have to be done again every time the AI got smarter...)
Having the AI perched on your shoulder and just analyzing the code as you look at it seems much simpler. Like a friendly, modern version of the pirate's parrot.
Counterpoint: comments in code reflect what the writer thought at the time of their generation - whether they were a human or an AI (and, excepting automated stupidity, there would be a human reviewing and accepting AI-generated comments). "Having the AI perched on your shoulder" is like re-reading and re-interpreting what the code means. You get the benefit of experience (and improved AI models), but you'll also miss the context, long lost to time since the code in question was written.
I'd say, we should to both. And code cleanliness... we won't make much more progress here than we've already made, not until we stop coding directly in the final, plaintext source form. There are too many conflicting concerns wrt. readability, and you can't have them individually optimized at the same time in a single piece of text.
I can definitely see the utility of a “tell me more about this method” button that gets descriptions from GPT.
I also like the “ChatGDB” style of interface where all the local UI context is added to your GPT session (eg “what is this code doing” will answer about what you have selected, in the context of the whole file, and perhaps with the ability to retrieve other files too if needed for the explanation).
So maybe training a model against some really well documented codebases might help?
Perhaps unit tests are a also a must since proper unit tests force granularity, thus in theory you’d have a good description of what the intention was + small chunks of code to correlate with.
The issue tho is that most open source code is technical code and code that relies on it serves a business function.
Either way i think the key is finding well documented well tdd’d business function code. Even if what you wish to explain is not clean and tidy but certain bits may fit in patterns that make sense when individually fit against a model. If it makes sense.
"The issue tho is that most open source code is technical code and code that relies on it serves a business function." Wow yeah never thought about it in that way before!
But keep in mind, this should be easy to do from the command line with a number of tools as long as you have a gpt-4 api key. I would probably trust gpt-3.5-turbo with this task in a pinch, but I think there would be more risk of it disrupting the original code.
Here it is with aichat [1]:
$ curl -s https://raw.githubusercontent.com/leachim6/hello-world/main/p/Python%203.py | aichat --model="gpt-4" -p "emit this exact code, but with helpful comments; don't put any comments before a #!shebang line if present"
#!/usr/bin/env python3
# This is a simple Python script that prints "Hello World" to the console.
# The first line, called the shebang, tells the operating system how to execute the script.
# In this case, it specifies that the script should be run using the Python 3 interpreter.
# The print function is used to output text to the console.
# Here, it is used to print the string "Hello World".
print("Hello World")
Or with my own tool aider [2]: $ git clone https://github.com/leachim6/hello-world.git
$ cd hello-world
$ aider "p/Python 3.py"
Added p/Python 3.py to the chat
Using git repo: .git
> add helpful comments
p/Python 3.py
<<<<<<< ORIGINAL
#!/usr/bin/env python3
print("Hello World")
=======
#!/usr/bin/env python3
# This is a simple Python script that prints "Hello World" to the console
print("Hello World") # Print "Hello World" to the console
>>>>>>> UPDATED
Applied edit to p/Python 3.py
Commit aad4afc aider: Added helpful comments to Python script.
[1] https://github.com/sigoden/aichatIt notices if your local repo is dirty and asks if you'd like to commit before proceeding with the GPT chat. It will even provide a suggestion for the commit message.
You can run aider with --no-auto-commits if you don't want it to commit to the repo. This is similar to your suggested --dangerous flag.
I have considered various magic/automatic branching strategies. But I suspect they would be too confusing. And people probably have their own preferred git workflows. I feel like it's probably better to let folks explicitly manage their branches and PRs however they like.
My aider tool tries to make this easy by leveraging git. While it automatically commits the edits from GPT, it also provides in-chat commands like /diff and /undo. These commands let you quickly check exactly what edits GPT made, and undo them if they're not correct.
Aider will notify GPT if you /undo its changes, and GPT will probably ask why and then try again with your concerns in mind.
To manage a longer chat that includes a sequence of changes, you can also use your preferred standard git workflows like branches, PRs, etc.
Perhaps GPT-5 and beyond would make this entire process 10X easier :)
That said, perhaps if the entire repository is fed into a vectorised database, a high-level overview would be possible? Just thinking aloud right now and am happy to collaborate with anyone interested in exploring this further!
Your raw sample files [1] are already highly descriptive: filenames, component names, variables etc etc. the auto-generated comments add only noise (increase file size). I would use complex, hard-to-decipher incomprehensible code to show how these comments bring sense forth. Perhaps let it loose on the Linux Kernel source, see what it does - an idea
Personally, I prefer the doc-as-you-code approach, especially using context-aware naming. When code's cryptic & large, i try to visualise it e.g. Sequence Diagram from code (intellij).
[1] https://swiftstart.notion.site/Sample-files-acfb097bbf214210...
If it provides more clarity, we are trying to tackle this in stages. Can the AI produce helpful and descriptive code for 1. Basic + Short (less than 100 lines) files? 2. Basic + Long (more than 100 lines) files? 3. Complex / Vague + Long files?
After stage 3, which I believe is what you are referring to, we then hope to explore stage 4. 4. Can AI incorporate programmers' intentions into the comments?
Can it even deobfscuate code ?
I managed to upload a file successfully by zipping a .js file. The file upload successfully, but then when I received the results a few minutes later, the zip file containing the results was empty.
If you are still experiencing this issue, please feel free to reach out to us at swiftstarters@gmail.com
Thanks!
Will be happy to help you debug this. Thanks!
I'd hate the idea of this leading anyone to avoid commenting their code ("just let the AI do it!"), since comments also need to be about the why and how to use and how not to use -- not just the what.