AI Powered Software Development
November 17, 2026 3 min read

Prompting for Code Generation

HS
HEMANT SINGH
Technical Writer & Educator

What is Prompting for Code Generation?

Prompting for Code Generation means giving AI clear instructions to generate source code that solves a specific programming problem.


For example, instead of asking:

Create a product management system.


You provide detailed instructions:

Create a Product Management module using Laravel 12, Livewire 3, Tailwind CSS 4 and MySQL. Include product name, SKU, category, purchase price, selling price and stock quantity. Use Laravel validation, Eloquent relationships and proper error handling. Follow the existing project structure.


The second prompt gives the AI much more useful information.


What information should a code-generation prompt contain?

A good code-generation prompt should cover these eight elements.


Element

What to specify

1. Objective

What should the code do?

2. Technology

Which programming language and framework?

3. Input

What data will the code receive?

4. Output

What should the code return or display?

5. Business logic

What rules must it follow?

6. Architecture

Where should the code fit into the project?

7. Constraints

What should the AI avoid?

8. Testing

How should the solution be verified?

Let's understand these through a practical example.


Practical example: Generate a PHP function

Suppose you want to calculate the final price of a product after applying a discount.

Weak prompt:

Write a PHP function to calculate discount.

This prompt leaves many questions unanswered:

  1. Is the discount a percentage or fixed amount?
  2. Can the discount exceed the product price?
  3. What happens if the price is negative?
  4. What should the function return?

Better prompt:

Act as an experienced PHP developer.

Create a PHP function named calculateDiscount().

Requirements:
1. Accept product price and discount percentage.
2. Both parameters must be integers or floats.
3. Return the final price after discount.
4. Discount must be between 0 and 100.
5. Product price cannot be negative.
6. Use strict types.
7. Add proper return types.
8. Throw InvalidArgumentException for invalid values.

Provide:
- Complete PHP code
- Three usage examples
- Two test cases for invalid inputs

One important technique: Generate code in smaller steps


A common mistake is asking AI to generate an entire application in one prompt.

For a complex feature, divide the work into smaller tasks.


Recommended code-generation workflow

  1. Step 1 — Analyze
  2. Understand the existing project and requirements.
  3. Step 2 — Design
  4. Plan database tables, relationships and architecture.
  5. Step 3 — Generate backend
  6. Create migrations, models, validation and business logic.
  7. Step 4 — Generate frontend
  8. Create Livewire components and UI.
  9. Step 5 — Integrate
  10. Connect frontend and backend.
  11. Step 6 — Test
  12. Verify functionality, validation and edge cases.


Why is this useful?

Because AI can make mistakes when handling too many requirements simultaneously. Smaller tasks make it easier to review the code, identify errors and correct them before moving forward.


Generate tests along with code

Do not only ask AI to generate code. Ask it to generate tests too.

For example:

Create PHPUnit tests for calculateDiscount().

Test these scenarios:

1. Price 1000, discount 10% → 900
2. Price 500, discount 0% → 500
3. Price 1000, discount 100% → 0
4. Negative price → Exception
5. Discount greater than 100 → Exception

Use PHPUnit and explain how to run the tests.