DEV Community

Cover image for Code brushes for GitHub Copilot
Christian Heilmann
Christian Heilmann

Posted on • Originally published at christianheilmann.com

Code brushes for GitHub Copilot

GitHub Copilot is incredible, and if you check what's happening in the preview released as the Copilot Labs extension it will only get more amazing.

Check this out:

Code brushes applied to a simple JavaScript

Let's start with a basic JavaScript that checks a string length and reports when it is empty. One with an obvious mistake, as we use a single = instead of the type safe ===. This will always report the string as empty.

if (str.length = 0) {
    console.error('String is empty');
}
Enter fullscreen mode Exit fullscreen mode

Using the Fix bugs brush, this is rectified.

if (str.length === 0) {
    console.error('String is empty');
}
Enter fullscreen mode Exit fullscreen mode

Using the Clean brush also adds reporting when the string is not empty.

if (str.length === 0) {
    console.error('String is empty');
} else {
    console.log('String is not empty');
}
Enter fullscreen mode Exit fullscreen mode

Using the List steps brush adds comments for step (sure, this is redundant, but it also works for much more complex code).

// check the length of the string
if (str.length === 0) {
    // if length is 0, print 'String is empty' 
    console.error('String is empty');
} else {
    // if length is not 0, print 'String is not empty'
    console.log('String is not empty');
}
Enter fullscreen mode Exit fullscreen mode

Using Make robust adds error reporting instead of logging and an extra check to make sure that str is really a String and not an empty Array.

function test(str) {
    if (str.length === 0) {
        throw new Error('String is empty');
    } else if (typeof str !== 'string') {
        throw new Error('It is not a string');
    } else {
        console.log('String is not empty');
    }
}
Enter fullscreen mode Exit fullscreen mode

The Add types brush does exactly that.

/**
 * @param {string} str
 * @returns {string}
 */
function test(str) {
    if (str.length === 0) {
        throw new Error('String is empty');
    } else if (typeof str !== 'string') {
        throw new Error('It is not a string');
    } else {
        console.log('String is not empty');
    }
}
Enter fullscreen mode Exit fullscreen mode

You can also define custom brushes and there are more to come.

Top comments (8)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

GitHub faces lawsuit over Copilot AI coding assistant | InfoWorld

Class-action complaint contends that training the AI system on public GitHub repos violates the legal rights of creators who posted the code under open-source licenses.

favicon infoworld.com
Collapse
 
kasuken profile image
Emanuele Bartolesi

@jonrandy every time you posted the same article under this kind of posts 😀

Collapse
 
codepo8 profile image
Christian Heilmann

Why not post the source?
githubcopilotinvestigation.com/

Collapse
 
jonrandy profile image
Jon Randy 🎖️

I just went with the first Google hit

Thread Thread
 
codepo8 profile image
Christian Heilmann

So, "stirring the sh*t vs. real concern"?

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

Real concern, in a rush, before a meeting. Thanks for posting a better link 👍

Collapse
 
ioome profile image
sutton

but i like it,thank for you sharing.hhhhh.thank

Collapse
 
ioome profile image
sutton

wow.this is crazy. but it just will instead of a part of the code