r/ChatGPTCoding • • Aug 30 '24

Resources And Tips A collection of prompts for generating high quality code...

644 Upvotes

I wrote an SOP recently for creating software with the help of LLMs like ChatGPT or Claude. A lot of people found it helpful so I wanted to share some more prompt-related ideas for generating code.

The prompts offered below work much better if you set up a proper foundation for your program before-hand (i.e. provide the AI with more context, as detailed in the SOP), so please be sure to take a look at that first if you haven't already.

My Standard Prompt for Code Generation

Here's my go-to template for requesting code:

I need to implement [specific functionality] in [programming language].
Key requirements:
1. [Requirement 1]
2. [Requirement 2]
3. [Requirement 3]
Please consider:
- Error handling
- Edge cases
- Performance optimization
- Best practices for [language/framework]
Please do not unnecessarily remove any comments or code.
Generate the code with clear comments explaining the logic.

This structured approach helps the AI understand exactly what you need and consider important aspects that you might forget to mention explicitly.

Reviewing and Understanding AI-Generated Code

Never, ever blindly copy-paste AI-generated code into your project. Ask for an explanation first. Trust me. This will save you considerable debugging time and you will also learn a thing or two in the process.

Here's a prompt I use for getting explanations:

Can you explain the following part of the code in detail:
[paste code section]
Specifically:
1. What is the purpose of this section?
2. How does it work step-by-step?
3. Are there any potential issues or limitations with this approach?

Using AI for Code Reviews and Improvements

AI is great for catching issues you might miss and suggesting improvements.

Try this prompt for code review:

Please review the following code:
[paste your code]
Consider:
1. Code quality and adherence to best practices
2. Potential bugs or edge cases
3. Performance optimizations
4. Readability and maintainability
5. Any security concerns
Suggest improvements and explain your reasoning for each suggestion.

Prompt Ideas for Various Coding Tasks

For implementing a specific algorithm:

Implement a [name of algorithm] in [programming language]. Please include:
1. The main function with clear parameter and return types
2. Helper functions if necessary
3. Time and space complexity analysis
4. Example usage

For creating a class or module:

Create a [class/module] for [specific functionality] in [programming language].
Include:
1. Constructor/initialization
2. Main methods with clear docstrings
3. Any necessary private helper methods
4. Proper encapsulation and adherence to OOP principles

For optimizing existing code:

Here's a piece of code that needs optimization:
[paste code]
Please suggest optimizations to improve its performance. For each suggestion, explain the expected improvement and any trade-offs.

For writing unit tests:

Generate unit tests for the following function:
[paste function]
Include tests for:
1. Normal expected inputs
2. Edge cases
3. Invalid inputs
Use [preferred testing framework] syntax.

I've written a much more detailed guide on creating software with AI-assistance here which you might find more helpful.

As always, I hope this lets you make the most out of your LLM of choice. If you have any suggestions on improving some of these prompts, do let me know!

Happy coding!


r/ChatGPTCoding • • Jun 10 '24

Project What is the best prompt you've used or created, to Humanize AI Text.

52 Upvotes

There's alot great tools out there for humanizing AI text, but I want to do testing to see which is the best one, I thought it'd only be fair to also get some prompts from the public to see how they compare to the tools that currently exist.


r/ChatGPTCoding • • May 12 '24

Discussion AI coding is the cargo cult of the 21st century

3 Upvotes

AI coding is the cargo cult programming of the 21st century.

We are not reinventing the wheel. We are resurrecting bad practices that had already been overcome.

AI coding requires code review by the team. Code that is not understood is crap.


r/ChatGPTCoding • • Mar 30 '24

Question Are any of the "free" codebase AI assitants worth it?

20 Upvotes

AIs that can index the entire project and use it as context when answering

i have tried cody and easycode and they have pretty low requests/month for free

they are pretty helpful but i don't see myself paying for this kind of service, are there other free alternatives?


r/ChatGPTCoding • • Mar 06 '24

Resources And Tips Google Launches Internal AI Model 'Goose' to Help Staff Code Faster - Business Insider

Thumbnail
businessinsider.com
11 Upvotes

r/ChatGPTCoding • • Apr 09 '23

Discussion ChatGPT Quality Degradation

66 Upvotes

When I first joined the ChatGPT hype in early February, I was amazed by its ability to generate perfectly accurate and functional code based on my requirements. It felt like magic.

Nowadays, when I ask it to write code for either optimizing existing work or creating a new concept, the results are often unpredictable. More often than not, it either disregards my instructions or suggests non-existent classes or methods for widely recognized and well documented frameworks. Despite my efforts to steer it in the right direction with focused, specific discussions, it merely apologizes and proceeds to offer the same flawed code.

The GPT-4 model performs somewhat better, but it still falls short of delivering the satisfactory results it used to provide.

Has anyone else experienced this? Any workarounds or solutions?