Skip to content

Prompting for Generate Code | Lecture 13

Prompting for Generate Code

Learn how to use prompt engineering to generate, explain, and debug code with AI. Discover best practices for writing prompts for programming tasks. Lecture 13

Your AI Coding Buddy

Welcome! Many of you will find that one of the most powerful applications of AI is as a programming assistant. AIs can write code from scratch, explain complex code, translate code between languages, and help you find and fix bugs.

Today, we’ll learn the best practices for prompting an AI for coding tasks. The key is to be extremely precise and provide as much context as possible.

1. Generating Code

When asking an AI to write code, you need to be very specific about your requirements.

Best Practices for Code Generation:
  • Specify the Language: Always state the programming language (e.g., Python, JavaScript, C++).
  • Describe the Goal: What should the code do? Describe the inputs and the expected outputs.
  • Mention Libraries/Frameworks: If the code should use specific libraries (e.g., React, Pandas, TensorFlow), say so.
  • Ask for Comments: You can ask the AI to add comments to the code to explain how it works.
Code Generation Prompt

Act as an expert Python programmer.

Write a Python function called `is_palindrome` that takes a single string as input and returns `True` if the string is a palindrome and `False` otherwise. A palindrome is a word that reads the same forwards and backwards.

Please include comments in the code to explain the logic.

2. Explaining Code

AIs are fantastic for helping you understand code that someone else wrote, or code that you wrote a long time ago!

Code Explanation Prompt
Explain what the following JavaScript code does. Explain it in simple terms for a beginner programmer. Break down the explanation line by line.

```javascript
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((acc, current) => acc + current, 0);
console.log(sum);
```

3. Debugging Code

This is one of the most helpful uses. When you have code that isn’t working, the AI can often spot the error faster than you can.

Best Practices for Debugging:
  • Provide the Code: Paste the exact code that is causing the problem.
  • State the Goal: What is the code supposed to do?
  • Provide the Error Message: Paste the full, exact error message you are getting. This is crucial!
  • Explain the Problem: Describe what happens when you run the code versus what you expected to happen.
Debugging Prompt
I have a bug in my Python code. I am trying to write a function that finds the average of a list of numbers, but it's giving me the wrong answer.

**My Code:**
```python
def calculate_average(nums):
  total = sum(nums)
  return total / len(nums) - 1 # I think the error is here

my_list = [10, 20, 30]
print(calculate_average(my_list)) # Expected output: 20, Actual output: 19
```

**Problem:** The function should return 20 for the input `[10, 20, 30]`, but it's returning 19. Please find the bug and explain how to fix it.
Prompting for Generate Code
Prompting for Generate Code

Key Takeaways from Lecture 13

  • For code generation, specify the language, goal, and any libraries.
  • For debugging, you MUST provide the code, the goal, and the exact error message.
  • Use delimiters (like “`) to clearly separate code blocks from your instructions.
  • The persona “Act as an expert [Language] programmer” is very effective.
  • Never trust AI-generated code blindly. Always test it and make sure you understand it before using it in an important project.

End of Lecture 13. Happy coding! In our next lecture, we’ll discuss some of the common problems and pitfalls with AI, like bias and hallucinations, and how to be aware of them.

Prompting for Summarization | Lecture 12

Najeeb Alam

Najeeb Alam

Technical writer specializes in developer, Blogging and Online Journalism. I have been working in this field for the last 20 years.