AI Tools Compared

Generate code with Copilot chat by describing requirements in natural language, iterating on implementation details, and asking for explanations. This guide shows the conversation flow that produces working code efficiently.

GitHub Copilot Chat provides a powerful interface for generating code through natural language conversations. Instead of writing every line manually, you can describe what you need in plain English and receive functional code snippets that integrate with your project. This approach accelerates development and helps developers quickly prototype ideas or implement features they might otherwise postpone.

This guide covers practical techniques for using Copilot Chat effectively to generate code from natural language descriptions.

Accessing Copilot Chat

Copilot Chat is available in VS Code, Visual Studio, and JetBrains IDEs through the GitHub Copilot extension. In VS Code, you access it via the chat icon in the sidebar or by pressing Cmd+I on Mac or Ctrl+I on Windows. The chat interface provides a conversation where you can explain what you want to build, ask questions about code, or request specific implementations.

Before generating code, ensure you have the latest version of the Copilot extension installed and that you are signed in with an account that has access to Copilot Chat features.

Basic Code Generation

The simplest way to generate code is by describing what you want in natural language. Your description should include relevant context such as the programming language, the desired functionality, and any specific requirements or constraints.

Example: Generating a Function

When you need a specific function, describe it clearly with input parameters, return type, and what the function should do:

Create a TypeScript function that calculates the factorial of a number using recursion

Copilot Chat responds with something like:

function factorial(n: number): number {
  if (n <= 1) {
    return 1;
  }
  return n * factorial(n - 1);
}

This response gives you a working implementation you can copy directly into your codebase.

Providing Context for Better Results

The quality of generated code improves significantly when you provide adequate context. Instead of a minimal prompt, include information about your existing code structure, coding conventions, and the specific requirements of your implementation.

Example: Contextual Prompt

A simple prompt like “create an user service” produces generic code. A more effective prompt includes specifics:

Create a TypeScript user service class for a Node.js API using dependency injection.
The service should handle user registration with email validation, login with password hashing using bcrypt,
and token generation using JWT. Use async/await patterns and proper error handling.

This generates more usable code that aligns with your project requirements:

import { injectable } from 'tsyringe';
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';

interface User {
  id: string;
  email: string;
  password: string;
}

@injectable()
export class UserService {
  constructor(
    @inject('UserRepository') private userRepository: UserRepository,
    @inject('JWTSecret') private jwtSecret: string
  ) {}

  async register(email: string, password: string): Promise<User> {
    if (!this.isValidEmail(email)) {
      throw new Error('Invalid email format');
    }

    const hashedPassword = await bcrypt.hash(password, 10);

    return this.userRepository.create({
      email,
      password: hashedPassword
    });
  }

  async login(email: string, password: string): Promise<string> {
    const user = await this.userRepository.findByEmail(email);

    if (!user) {
      throw new Error('Invalid credentials');
    }

    const isValid = await bcrypt.compare(password, user.password);

    if (!isValid) {
      throw new Error('Invalid credentials');
    }

    return jwt.sign({ userId: user.id }, this.jwtSecret, { expiresIn: '24h' });
  }

  private isValidEmail(email: string): boolean {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
  }
}

Generating Code for Different Scenarios

Copilot Chat handles various coding scenarios effectively when you frame your requests appropriately.

Database Queries

For database operations, specify the ORM or query builder you are using:

Write a PostgreSQL query using Knex.js to find all orders placed in the last 30 days,
grouped by customer, showing the total amount spent per customer, ordered from highest to lowest.

API Endpoints

When you need API endpoints, describe the route, method, request body, and response:

Create an Express.js route handler for POST /api/products that validates the request body
contains name (string, required), price (number, positive, required), and category (string, optional).
On success, return the created product with status 201. On validation error, return 400 with error details.

Test Files

Generate tests by specifying the testing framework and what you are testing:

Write Jest unit tests for a utility function that validates credit card numbers using Luhn algorithm.
Include tests for valid cards, invalid cards, and edge cases like empty strings.

Refining Generated Code

After receiving initial code, you can refine it through follow-up messages. This iterative approach helps you shape the output to match your exact needs.

Iterative Improvements

Start with a basic request and then add requirements:

  1. “Create a React component that displays a list of items” (initial request)

  2. “Add sorting functionality with ascending/descending toggle” (refinement)

  3. “Add search filtering that works on the client side” (further refinement)

  4. “Add pagination with 10 items per page” (additional feature)

This approach produces more focused code than attempting to specify everything in a single prompt.

Working with File Context

Copilot Chat can read files in your workspace, allowing you to generate code that integrates with existing code. Reference files by name or use the @mention feature:

@userService.ts - Create a method to update user profile that validates the input
and handles concurrent update conflicts

This makes Copilot aware of your existing code structure and generates code that fits naturally.

Best Practices for Effective Prompts

Structure your prompts to include what you need clearly:

Avoid ambiguous prompts. Instead of “make a function that processes data,” specify exactly what processing means in your context.

Common Use Cases

Copilot Chat excels at generating boilerplate code, handling repetitive patterns, and implementing well-defined algorithms. Common effective use cases include:

Limitations and Workarounds

Copilot Chat works best with well-defined tasks. Complex requirements benefit from breaking them into smaller, sequential requests. For intricate logic, provide examples or reference implementations in your prompt. Always review generated code before integrating it into production systems.

Generated code may occasionally use outdated patterns or deprecated APIs. Check documentation when using generated code with newer frameworks or libraries.


Built by theluckystrike — More at zovo.one