GitHub Copilot is a tool that helps developers write code faster. But how does it compare to traditional coding, where you write everything yourself?
Let’s break it down in simple terms with examples and comparisons.
Let's focus on a scenario where you need to build a class-based structure for handling a Book Inventory System. The system will allow adding, deleting, updating, and searching books. It will also include additional features like tracking borrowed books.
Traditional Coding (Without Copilot)
In traditional coding, the developer writes all the logic, sets up the class structure, handles edge cases, and implements everything from scratch.
interface Book {
id: number;
title: string;
author: string;
isAvailable: boolean;
}
interface User {
id: number;
name: string;
borrowedBooks: number[];
}
class Library {
private books: Book[] = [];
private users: User[] = [];
// Add a book to the library
addBook(book: Book): void {
this.books.push(book);
}
// Register a user
registerUser(user: User): void {
this.users.push(user);
}
// Borrow a book
borrowBook(userId: number, bookId: number): string {
const user = this.users.find(u => u.id === userId);
const book = this.books.find(b => b.id === bookId);
if (!user) return 'User not found';
if (!book) return 'Book not found';
if (!book.isAvailable) return 'Book is already borrowed';
book.isAvailable = false;
user.borrowedBooks.push(bookId);
return `Book "${book.title}" borrowed by "${user.name}"`;
}
// Return a book
returnBook(userId: number, bookId: number): string {
const user = this.users.find(u => u.id === userId);
const book = this.books.find(b => b.id === bookId);
if (!user) return 'User not found';
if (!book) return 'Book not found';
if (user.borrowedBooks.includes(bookId)) {
book.isAvailable = true;
user.borrowedBooks = user.borrowedBooks.filter(id => id !== bookId);
return `Book "${book.title}" returned by "${user.name}"`;
} else {
return 'This user did not borrow this book';
}
}
}
// Example Usage
const library = new Library();
library.addBook({ id: 1, title: '1984', author: 'George Orwell', isAvailable: true });
library.registerUser({ id: 1, name: 'Alice', borrowedBooks: [] });
console.log(library.borrowBook(1, 1)); // Borrow book
console.log(library.returnBook(1, 1)); // Return book
Code with GitHub Copilot
Now, let's see how GitHub Copilot can assist you. In this case, you just need to start writing some comments or function signatures, and Copilot will suggest the entire code for you.
Step 1. Start with Class and Function Signatures.
// A Library System to manage books and users
class Library {
private books: Book[] = [];
private users: User[] = [];
addBook(book: Book): void {
// Copilot fills in the rest of the implementation here...
}
}
Step 2: Copilot Suggests the Full Code.
As you type, GitHub Copilot will start suggesting full blocks of code for all the methods, like addBook
, deleteBook
, borrowBook
, etc.
class Library {
private books: Book[] = [];
private users: User[] = [];
// Add a book
addBook(book: Book): void {
this.books.push(book);
}
// Register a user
registerUser(user: User): void {
this.users.push(user);
}
// Borrow a book
borrowBook(userId: number, bookId: number): string {
const user = this.users.find(u => u.id === userId);
const book = this.books.find(b => b.id === bookId);
if (!user) return 'User not found';
if (!book) return 'Book not found';
if (!book.isAvailable) return 'Book is already borrowed';
book.isAvailable = false;
user.borrowedBooks.push(bookId);
return `Book "${book.title}" borrowed by "${user.name}"`;
}
// Return a book
returnBook(userId: number, bookId: number): string {
const user = this.users.find(u => u.id === userId);
const book = this.books.find(b => b.id === bookId);
if (!user) return 'User not found';
if (!book) return 'Book not found';
if (user.borrowedBooks.includes(bookId)) {
book.isAvailable = true;
user.borrowedBooks = user.borrowedBooks.filter(id => id !== bookId);
return `Book "${book.title}" returned by "${user.name}"`;
} else {
return 'This user did not borrow this book';
}
}
}
Output
Book "1984" borrowed by "Alice"
Book "1984" returned by "Alice"
Conclusion
- Traditional Coding: The developer writes everything from scratch, which gives full control but can take longer.
- Code with GitHub Copilot: Copilot assists by suggesting code, making the process faster and less error-prone. While Copilot’s suggestions might not always be perfect, they save time and effort, especially for repetitive tasks.