Incremental Prompting using GenAI : CRUD Script example

In previous blog I have emphasized that even with the rise of AI tools in software development, a strong foundational understanding of programming concepts is crucial. I have highlighted key elements like syntax, data types, loops, and data structures as essential to master before leveraging AI for tasks like scaffolding or code snippets. AI should be seen as a “co-pilot” or potent assistant, not a replacement for a developer’s thinking or responsibility.

One-shot AI code generation creates a “black box” with minimal control and significant pitfalls, as AI is not a mind-reader and requires programmatic thinking to break down problems. A truly efficient approach involves breaking down development logic into iterative, focused prompts, allowing developers to review, understand, refine, and maintain full control over the generated code.

Let’s explore an incremental prompting approach for building an application, using a basic Python CRUD application with a CSV file as its database

Outline of Development Approach

Below is the outline of our approach to developing a CRUD application if it was done without GenAI usage, a similar approach would be employed

  1. Initial Setup & Basic Read Functionality: Establishes the foundation and ensures you can interact with your ‘database’ from the start

Prompt: “I’m building a Python CRUD application using a CSV file. For the first step, please provide Python code for project setup (main.py, data.csv), a function to read all records from data.csv into a list of dictionaries, and a way to display them. Assume ‘id’ and ‘name’ columns initially.”

2. Implementing the ‘Create’ Operation: Focuses on adding new data, including ID management and file persistence.

Prompt: “Building on the previous code, provide a Python function to add a new record to the in-memory list, assign a unique ID (e.g., auto-increment), and then write the updated list back to the CSV file. Include an example of how to use it.

3. Implementing the ‘Update’ Operation: Addresses modifying existing data, which often involves rewriting the entire file.

Prompt: “Now, provide a Python function to update an existing record. It should take an ID and a dictionary of updates. Find the record by ID, apply the changes, and then overwrite the entire CSV with the modified data. Show an example of its use.”

4. Implementing the ‘Delete’ Operation: Completes the core data manipulation functions.

Prompt: “Next, create a Python function to delete a record. It should take an ID, remove the corresponding record from the in-memory list, and then overwrite the CSV file with the remaining records. Provide a usage example.”

5. Building the Command-Line Interface (CLI): Connects all the backend functions into an interactive application.

Prompt: “Integrate the Create, Read, Update, and Delete functions into a basic command-line interface. Include a menu loop, user input handling for choices and data, and calls to the appropriate functions. Provide an exit option.”

6. Adding Error Handling and Input Validation: Makes the application robust and prevents crashes from unexpected input or scenarios

Prompt: “Enhance the application with error handling. Include checks for ‘CSV file not found’, ‘record ID not found’ (for update/delete), and basic input type validation (e.g., ensuring IDs are numbers). Provide user-friendly messages for errors.”

7. Code Modularization: Enhances maintainability, readability, and prepares for larger projects.

Prompt: “To improve code organization, help me refactor the application. Suggest splitting the code into separate files like csv_handler.py (for file I/O), crud_operations.py (for business logic), and main_app.py (for the CLI). Show how to import functions between them.”

Try this approach and let me know your thoughts through comments.

~Good Luck.

Leave a Comment