Live data from Hacker News

Can LLMs write better code if you keep asking them to “write better code”?

minimaxir.com

71–80 of 461 posts

Re: Can LLMs write better code if you keep asking them to “write better code”?

#71
post #5

Something major missing from the LLM toolkit at the moment is that it can't actually run (and e.g. test or benchmark) its own code. Without that, the LLM is flying blind. I guess there are big security risks involved in making this happen. I wonder if anyone has figured out what kind of sandbox could safely be handed to a LLM.

[dead]

Re: Can LLMs write better code if you keep asking them to “write better code”?

#72
post #66

> code quality can be measured more objectively Well, that's a big assumption. Some people quality modular code is some other too much indirect code.

You can write maximally modular code while being minimally indirect. A well-designed interface defines communication barriers between pieces of code, but you don't have to abstract away the business logic. The interface can do exactly what it says on the tin.

Re: Can LLMs write better code if you keep asking them to “write better code”?

#73
post #5

Something major missing from the LLM toolkit at the moment is that it can't actually run (and e.g. test or benchmark) its own code. Without that, the LLM is flying blind. I guess there are big security risks involved in making this happen. I wonder if anyone has figured out what kind of sandbox could safely be handed to a LLM.

We have it run code and the biggest thing we find is that it gets into a loop quite fast if it doesn't recognise the error; fixing it by causing other errors and then fixing it again by causing the initial error.

Re: Can LLMs write better code if you keep asking them to “write better code”?

#74

its best to tell them how you want the code written.

At that point isn't it starting to become easier to just write the code yourself? If I somehow have to formulate how I want a problem solved, then I've already done all the hard work myself. Having the LLM just do the typing of the code means that now not only did I have to solve the problem, I also get to do a code review.

Personally I found it quite fun to give specification and have ChatGPT find me a Python code that implements it: https://chatgpt.com/share/6777debc-eaa4-8011-81c5-35645ae433... . Or the additional polygon edge smoothing code: https://chatgpt.com/share/6773d634-de88-8011-acf8-e61b6b913f...

Sure, the green screen code didn't work exactly as I wished, but it made use of OpenCV functions I was not aware of and it was quite easy to make the required fixes.

In my mind it is exactly the opposite: yes, I've already done the hard work of formulating how I want the problem solved, so why not have the computer do the busywork of writing the code down?

Re: Can LLMs write better code if you keep asking them to “write better code”?

#75
post #55

I often run into LLMs writing "beginner code" that uses the most fundamental findings in really impractical ways. Trained on too many tutorials I assume. Usually, specifying the packages to use and asking for something less convoluted works really well. Problem is, how would you know if you have never learned to code without an LLM?

I suspect it's not going to be much of a problem. Generated code has been getting rapidly better. We can readjust about what to worry about once that slows or stops, but I suspect unoptimized code will not be of much concern.

Re: Can LLMs write better code if you keep asking them to “write better code”?

#76

> “Planning” is a long-used trick to help align LLM output for a first pass — the modern implementation of “let’s think step by step.” I hadn't seen this before. Why is asking for planning better than asking it to think step by step?

This is how aider becomes really good:

- start by "chatting" with the model and asking for "how you'd implement x y z feature, without code".

- what's a good architecture for x y z

- what are some good patterns for this

- what are some things to consider when dealing with x y z

- what are the best practices ... (etc)

- correct / edit out some of the responses

- say "ok, now implement that"

It's basically adding stuff to the context by using the LLM itself to add things to context. An LLM is only going to attend to it's context, not to "whatever it is that the user wants it to make the connections without actually specifying it". Or, at least in practice, it's much better at dealing with things present in its context.

Another aspect of prompting that's often misunderstood is "where did the model see this before in its training data". How many books / authoritative / quality stuff have you seen where each problem is laid out with simple bullet points? Vs. how many "tutorials" of questionable quality / provenance have that? Of course it's the tutorials. Which are often just rtfm / example transcribed poorly into a piece of code, publish, make cents from advertising.

If instead you ask the model for things like "architecture", "planning", stuff like that, you'll elicit answers from quality sources. Manuals, books, authoritative pieces of content. And it will gladly write on those themes. And then it will gladly attend to them and produce much better code in a follow-up question.

Re: Can LLMs write better code if you keep asking them to “write better code”?

#77
post #66

> code quality can be measured more objectively Well, that's a big assumption. Some people quality modular code is some other too much indirect code.

You can write maximally modular code while being minimally indirect. A well-designed interface defines communication barriers between pieces of code, but you don't have to abstract away the business logic. The interface can do exactly what it says on the tin.

> The interface can do exactly what it says on the tin.

In theory.

Do some code maintenance and you'll soon find that many things don't do what it says on the tin. Hence the need for debug and maintenance. And then going through multiple levels of indirection to get to your bug will make you start hating some "good code".

Re: Can LLMs write better code if you keep asking them to “write better code”?

#78
post #5

Something major missing from the LLM toolkit at the moment is that it can't actually run (and e.g. test or benchmark) its own code. Without that, the LLM is flying blind. I guess there are big security risks involved in making this happen. I wonder if anyone has figured out what kind of sandbox could safely be handed to a LLM.

ChatGPT runs code. o1 even checks for runtime problems and fixes them "internally".

Re: Can LLMs write better code if you keep asking them to “write better code”?

#79

The more interesting question IMO is not how good the code can get. It is what must change for the AI to attain the introspective ability needed to say "sorry, I can't think of any more ideas."

The problem is, they do not think.

So, like many people then? Many people are even not at the level of llms but more like markov chains.

Re: Can LLMs write better code if you keep asking them to “write better code”?

#80
I'm amused that neither the LLM or the author identified one of the simplest and most effective optimizations for this code: Test if the number is max _before_ doing the digit sum. It's a free 5.5x speedup that renders some of the other optimizations, like trying to memoize digit sums, unnecessary.

On an m1 macbook pro, using numpy to generate the random numbers, using mod/div to do digit sum:

Base: 55ms

Test before digit sum: 7-10ms, which is pretty close to the numba-optimized version from the post with no numba and only one line of numpy. Using numba slows things down unless you want to do a lot of extra work of calculating all of the digit sums in advance (which is mostly wasted).

The LLM appears less good at identifying the big-o improvements than other things, which is pretty consistent with my experience using them to write code.

Post reply on HN