Example 1: Simple Arithmetic Function
----------------------------------------
Task: Create a function that adds two numbers.
Inadequate Prompt: "Write a function to add numbers."
Enhanced Prompt: "Create a Python function named add_numbers which takes two parameters, a and b, and returns their sum. Ensure to include comments explaining the functionality."
By being explicit about the function name, parameters, and demanding comments, this prompt provides the AI with a clear path to generate precise and documented code.
Example 2: Handling Edge Cases
----------------------------------
Task: Develop code to find the square root of a number, handling negatives.
Inadequate Prompt: "Write a function to find the square root."
Enhanced Prompt: "Construct a Python function named calculate_square_root that accepts a number x as input and returns its square root. For negative input, the function should return None and print an error message. Ensure the solution adheres to PEP 8 standards and includes comments explaining the logic and error handling."
Detailing how to manage negative numbers and insisting on style adherence and comments ensures clarity, usability, and maintainability in the generated code.
Example 3: Ensuring Efficient Solution
----------------------------------------
Task: Develop a function to check if a number is prime.
Inadequate Prompt: "Create a function to check for prime numbers."
Enhanced Prompt: "Write a Python function named is_prime that determines if a given integer n is a prime number. Ensure the solution has a time complexity no worse than O(sqrt(n)) and include comments that detail the algorithm’s logic and any loops used. Provide a clear error message for invalid inputs like non-integers or negative numbers."
By enforcing a maximum time complexity and mandating error handling, the prompt guides the AI to generate a solution that is both efficient and robust.
Example 4: Ensuring Modularity in Code
------------------------------------------
Task: Find the factorial of a number.
Inadequate Prompt: "Write a factorial function."
Enhanced Prompt: "Develop a Python function named calculate_factorial that takes an integer n as input and returns its factorial. Incorporate a separate helper function to validate that n is a non-negative integer, returning an appropriate error message if it is not. Ensure the code is PEP 8 compliant and includes explanatory comments."
This prompt champions modularity by requesting a helper function for input validation and ensuring clean, standardized code through style adherence and comments.
Example 5: Facilitating Generalization
----------------------------------------
Task: Create a function to calculate the nth Fibonacci number.
Inadequate Prompt: "Create a Fibonacci function."
Enhanced Prompt: "Generate a Python function named get_fibonacci that determines the nth number in the Fibonacci sequence, ensuring that the solution works for any positive integer n. Include examples and comments that explain the solution’s logic and any loops or recursive calls utilized. The code should handle invalid inputs gracefully with meaningful error messages."
Explicitly requesting a generalizable solution, including logic explanation and error handling, ensures the resultant code is versatile and user-friendly.
Conclusion
Each of these examples underscores the importance of specificity, clarity, and anticipating potential issues in the code generation process. Through structured and insightful prompting, AI can be skillfully navigated to produce code that is logical, efficient, and user-friendly, reducing the need for extensive post-generation refinement and ensuring alignment with the developer’s intentions and standards. This exercise of tactical prompting ultimately culminates in a productive synthesis of human oversight and AI capability in code generation.