Ask HN: How do you coax useful code from AI?

7 points by memset 2 days ago

I spend a lot of time trying to get copilot and cursor to help me solve tedious problems. I can never get it to give me code that is anything remotely correct (in the "correctness" sense of the word.)

I think my problem is that I am "lazy", but I honestly have no idea how to prompt in a non-lazy way. I feel like if I had a specific algorithm in mind, for example, then I could just code it myself. Here is an example:

> i am building a distributed queue. i am using hashicorp memberlist to know when new nodes are added or removed. each node has a shard and replica number in its memberlist metadata. i am using hashicorp raft to set up replication.

> Currently, my memberlist only has a single member and raft is set up to bootstrap with itself as the only node. What logic do I need to write in the memberlist delegate to ensure nodes are correctly added to their shard? this should be reliable - the cluster should never be left in an unstable state, or a state where we attempt to process membership changes while a node is still in a candidate state.

What should I be doing instead?

apothegm 18 hours ago

It can’t think for you. It can type for you.

Treat it like an intern and a rubber duck.

It may be able to suggest the names of existing algorithms, but unless they’re absurdly common or simple it’s unlikely to be able to implement them for you.

If you need to come up with your own algorithm, prompt it to ask you questions to clarify your own thinking.

Document in plain English how you want your algorithm to work.

Stub out APIs yourself, and add comments describing what each function/method should do. Have the AI come up with and implement test cases. Then have it implement the methods to pass those test cases. Then debug.

bloomingkales 2 days ago

Program with the LLM the way you should program. Have it always build the most naive implementation, then ask it to clean it up, then ask it to add one feature at a time, then ask for optimizations, then ask for alternatives, then ask if you are going down the right path or not. Keep working incrementally.

LLM outputs are like clay, you have to sculpt it.

TheBruceHimself 2 days ago

Copilot is quite good at inferring functionality from comments in code. At least this is what I found. If you break down your function, or whatever it is you are great at making, into steps or sub-processes with comments outlining what each part does, Copilot works a lot better. So for example, I want to sort the lines in a file via a linked list: I might have my first comment say “step1: load text file called bla.TXT into the linked list with one line per node” then leave a gap where code should go before writing a second comment which may go something like “step 2: iteratively go through the list, swapping nodes if they are not in ascending alphabetical order. Do this until the list is ordered”. From there prompting the AI to complete the code based on the comments works fairly well.

I will say that Copilot is sometimes embarrassingly wrong. It’s strange how brilliant it can be at times and how incredibly dumb it can be other times. I think that just comes as part of the technology right now, but I have made it write quite complex code using the above approach. This approach does, however, require you to understand how the problem should be solved as to have Copilot understand enough to take over.

bjourne 2 days ago

Maybe you are expecting too much? I use copilot and similar tools to write simple code where I know exactly what should be done, I've just forgot the details. Like "Using Plotly and Python write a stacked bar chart where each stack contains three elements. Place the figure legend in the top left corner."

readyplayernull 2 days ago

I don't like current AI in IDE integrations, it's like inviting someone to help you build Lego's with boxing gloves.

Better ask Claude simple questions in the browser, review the answer, research a little when in doubt, and translate the solution.

My prompts are minimal, just like they used to work in the old search engine days:

> Linux C++ inter process comunication

That brings me the most used mechanisms with advantages and examples.