Prompt Templates for OOP

Prompt templates provide structured guidance for interacting with AI copilots during object-oriented programming activities. They are organized around three major practices—code generation, code review, and code refactoring—and specify the task, relevant context, constraints, expected output, and evaluation criteria to help developers produce clearer, more consistent, and more reusable prompts.

Each prompt template is specified using four meta-elements—Intent, Precondition, Prompt Template, and Expected Output—which clarify the purpose of the prompt, the information required before using it, the actual prompt text, and the form of the expected copilot response. Detailed prompt templates, usage notes, and examples are provided in the book AI-Assisted Object-Oriented Programming.

Example: Class Definition from Object Description (GEN-CLS-02)

This prompt template is designed to generate a class definition from an object description that specifies the object’s intended role and responsibilities.

Using this template, the prompt can be instantiated for a specific object and programming language. For example, the following prompt requests a Java class definition for a Reservation object based on its intended responsibilities.

Generate a class definition in Java for the object named Reservation, which is responsible for recording a customer’s request to rent a specific car type for a given period, tracking the reservation status, and supporting confirmation and cancellation. Include attributes, methods, and a constructor consistent with these responsibilities.

A representative generated output for this prompt is shown below:

public class Reservation {
    private String customerId;
    private String carType;
    private LocalDate startDate;
    private LocalDate endDate;
    private String status;

    public Reservation(String customerId, String carType,
                       LocalDate startDate, LocalDate endDate) {
        this.customerId = customerId;
        this.carType = carType;
        this.startDate = startDate;
        this.endDate = endDate;
        this.status = "Pending";
    }

    public void confirm() {
        status = "Confirmed";
    }

    public void cancel() {
        status = "Cancelled";
    }
}

The generated Reservation class represents the information and behavior implied by the prompt. It defines attributes for the customer, requested car type, rental start and end dates, and reservation status. The constructor initializes these values and sets the initial status to Pending. The confirm() and cancel() methods then update the reservation status to Confirmed or Cancelled, respectively. Thus, the generated class maps the stated responsibilities into a simple object-oriented structure containing both the reservation state and the operations that manage its lifecycle.

As shown in the example above, structured prompt templates improve AI-assisted code generation by making the developer’s intent, relevant context, constraints, and expected results explicit. This reduces ambiguity, promotes more consistent outputs, and makes prompts easier to reuse and adapt across different object-oriented constructs, programming languages, and development tasks.

Prompt Templates for Code Generation

Prompt templates for code generation provide structured coding requests for producing specific object-oriented constructs and complete object-oriented programs. They cover objects and classes, inheritance, polymorphism and dynamic binding, abstract classes, interfaces, generic classes, reflection, and main-program structure, with templates applicable to C++, Java, and Python. Each template specifies the intended construct, relevant design or contextual information, target language, and expected output so that the copilot can produce candidate code for developer review and validation.

The code-generation prompt templates are organized into eight categories corresponding to the major object-oriented constructs and the overall program structure.

  • Prompts for Generating Objects and Classes
  • Prompts for Generating Inheritance Structures
  • Prompts for Generating Polymorphism and Dynamic Binding
  • Prompts for Generating Abstract Classes
  • Prompts for Generating Interfaces
  • Prompts for Generating Generic Classes
  • Prompts for Generating Reflection-Based Code
  • Prompts for Generating Object-Oriented Program Code

Prompt Templates for Code Review

Prompt templates for code review provide structured requests for examining generated or existing object-oriented code against selected quality criteria and construct-specific concerns. They guide the copilot to identify defects, code-quality issues, and violations; assess their impact; and suggest appropriate corrections. The templates support reviews ranging from focused examination of a class or construct to whole-program assessment, defect-pattern checking, and verification after corrections.

The code-review prompt templates are organized into five categories corresponding to different review purposes:

  • Prompts for Focused Object-Oriented Code Review
  • Prompts for Whole-Program (Architectural) Review
  • Prompts for Construct-Focused Review
  • Prompts for Defect-Pattern and Regression Review
  • Prompts for Re-Review After Corrections

Prompt Templates for Code Refactoring

In object-oriented programming, code smells are structural symptoms that may indicate weaknesses in code or design even when the program is functionally correct. They may occur at different levels, including methods, classes, inheritance hierarchies, polymorphic behavior, object collaborations, and overall program structure. Examples include long methods, duplicated logic, large classes, weak encapsulation, inappropriate inheritance, repeated type checking, tight coupling, and misplaced responsibilities. Such findings should be treated as candidate refactoring needs until the developer determines that they represent structural weaknesses worth improving.

Prompt templates for code refactoring provide structured guidance for improving the internal structure of existing object-oriented code while preserving its intended behavior. They support a systematic process in which the copilot helps identify code smells, define an appropriate refactoring strategy, apply the selected transformations, and validate the resulting code. Developers evaluate the copilot’s findings and transformations and remain responsible for deciding which changes should be accepted.

The code-refactoring prompt templates are organized into four categories corresponding to the major steps of the refactoring process:

  • Prompts for Identifying Code Smells
  • Prompts for Defining the Refactoring Strategy
  • Prompts for Applying Refactoring Transformations
  • Prompts for Validating the Refactored Code

Detailed prompt templates, including their structures, guidelines, and examples, are provided in the book AI-Assisted Object-Oriented Programming.