First, try to avoid the situation
The most important principle here is that Stack Overflow is not a discussion forum, but a Q&A site. Almost everything else that can be said here follows from that.
In particular: as the moderator tooling so often reminds us, comments "are not for extended discussion". They aren't really for discussion at all, except to the minimum extent necessary for the current purpose. On Meta, that's a little wider than on the main site. But on the main site we need to be fairly strict about this. Comments clutter the page and detract from the site's goal of presenting high-quality answers immediately next to high-quality questions, without chit-chat.
As a rule, comments are subject to deletion at any time, without appeal; use them accordingly.
On questions
Here, the primary purpose of comments is to give feedback about the question-asking process. If you say anything at all about how to solve the problem, it should be as part of explaining why the question should be closed:
If there's a typo (or an idiosyncratic logical error), it's fine to point out where that is, as long as you're also helping to close the question by voting or flagging.
If it's a duplicate, you might want to explain preemptively why certain apparent differences between the current question and the duplicate don't actually matter.
If it's off-topic, every now and then there's a need to explain why.
But in any event, don't just offer an answer, full or partial, in the comments. The fact that it would only take a few lines of code to solve the problem is not a reason to override that. Similarly, don't offer or discuss general improvements to the code in itself - we don't do code review; there's a whole separate Code Review SE site for that. But do propose improvements to the example - which is to say: explain why the code shown falls short as a proper minimal reproducible example, and why we don't care whether the code shown reflects the actual project - we care whether it reflects the question being asked, as simply and directly as possible.
On answers
Similarly, comments on answers should be used to point out concrete issues with the information given, and only insofar as they can't be fixed by third-party editing (within policy, of course). They are especially not for the OP to ask follow-up questions, except insofar as they are requesting a clarification/additional details, etc. They are not a place for other people with the same problem to come and look for troubleshooting help, either. We don't do "help" anyway; but more importantly, a question is about one question. If there is any suitable non-duplicate question underlying the request, it is to be asked separately.
When someone gets the wrong idea
There is occasionally some value to trying to explain how the site works. Sometimes they listen; and every now and then, they even improve the way they use the site going forward. However, if someone is clearly just looking for someone to write code, engaging can really only make things worse. Please flag "can you write the code plz" type comments as "no longer needed" - as they were never "needed" (there is established precedent for this interpretation of the flag), and don't respond.
There is one important exception here. If you've posted an answer without code, and someone thinks that the answer would be better with code, and there's an at-all-convincing argument that the answer would be better with code, consider editing to include code. Since, again, this is not a discussion forum but a Q&A site, answers are about answering the question as well as possible; and the decision about whether to answer a question (or close it) is about the quality of the question and whether it meets standards. All of this is specifically not about whether the question is easy or how much the OP needs an answer or whether OP comes across as "lazy". It's about collaborating on a Q&A pair that helps everyone, by existing (and doesn't harm by existing, e.g. by cluttering search results, acting as "clickbait", wasting the reader's time etc.).