Why AI (CoPilot) is the Absolute Best Practice and the best $10 I ever spent

There comes a time for software developers when they think they know it all. Once you’ve settled into the comfort of your favorite code editor, shortcuts, technologies, libraries, and coding paradigms, it’s easy to feel like you have complete control and mastery of the “art”. Especially comparing yourself to juniors, their hour long tasks you can breeze through in minutes.
However, even if you had read the encyclopedia page for page, could you even argue against a tool as powerful as Google Search?
Enter CoPilot, it’s to Google Search (+ Stack Overflow) what Google Search is to an encyclopedia. Ok, so you still need to go to school, learn some math, learn how to search for the information you need. However, afterwards you absolutely must rely on CoPilot to fill in the blanks.
Here’s a practical example, demonstrating who cares if you can remember how to write a binary search anymore (ironically, I guess Google when they interview you). Just ask and you shall receive.
//@Copilot: "write a binary search function in JS"
//...yields
function binarySearch(arr: number[], target: number): number {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
Stylistically, this code is better than 99% of what you’d get out there, save for an oddly specific matching Stack-Overflow answer. Essentially, this highlights the idea that coding is akin to advanced Lego — assembling building blocks until you create what you desire. CoPilot merely provides you with pre-made pieces.
However, there are caveats to using CoPilot. It circles back to the cardinal rule of coding: you must check your code, and it must work — with or without CoPilot. In the real world, you get paid for working code, not just brilliant ideas.
Yes, the above code is in fact not flawless, and Typescript (another must use tool for JS developers) immediately complains that arr[mid] === undefined. The limit of CoPilot is that it writes better code than you, but often with imperfections. The added benefit is that you can use AI to refine your code:
//@Copilot: "Correct the function, accounting for arr[mid] === undefined"
//...yields
function binarySearch(arr: number[], target: number): number {
let left = 0;
let right = arr.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
// Handle the case where arr[mid] is undefined
if (arr[mid] === undefined) {
// Decide whether to treat undefined as less than or greater than the target
// For example, you can choose to treat undefined as less than the target:
arr[mid] = Number.NEGATIVE_INFINITY;
}
if (arr[mid] === target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
Yes, the code and even the explanation!
And finally, I’d recommend putting some finishing touches, i.e. your own signature. For example…
- While loops can be challenging to read, signaling the possibility of going on forever.
- Null-coalescing operator to get rid of the ugly conditional
function binarySearch(arr: number[], target: number): number {
let left: number = 0;
let right: number = arr.length - 1;
for (let mid: number; left <= right; ) {
mid = Math.floor((left + right) / 2);
if (arr[mid] === target) {
return mid;
} else if ((arr[mid] ?? Number.NegativeInfinity) < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
Now, one can confidently say they could get paid for this code. It’s a relief as a developer to know that a tool now exists where you no longer have to do any of the writing, only the directing. That being said, as with the binary-search example, the refinement process is still critical.
To all aspiring developers out there, a final message is don’t learn how to write a binary-search, learn how to read a binary-search for correctness and then get CoPilot to write you a binary-search. Also, remember, if you can’t get CoPilot to work for you, it’s a you problem. It’s trained on millions of lines of code and queries, and you’re simply not.
Up Next… ChatGPT, please improve on my non-English major prose used in this article