Christian Heilmann

Code brushes for GitHub Copilot

Tuesday, December 13th, 2022 at 4:45 pm

Github Next now showed a new feature in GitHub Copilot labs that allows you to change code you write to make it cleaner, more robust and document it automatically.

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');
}

Using the `Fix bugs` brush, this is rectified.

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

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');
}

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');
}

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');
    }
}

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');
    }
}

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

Share on Mastodon (needs instance)

Share on BlueSky

Newsletter

Check out the Dev Digest Newsletter I write every week for WeAreDevelopers. Latest issues:

Word is Doomed, Flawed LLM benchmarks, hard sorting and CSS mistakes Spot LLM benchmark flaws, learn why sorting is hard, how to run Doom in Word and how to say "no" like a manager.
30 years of JS, Browser AI, how attackers use GenAI, whistling code Learn how to use AI in your browser and not on the cloud, why AI makes different mistakes than humans and go and whistle up some code!
197: Dunning-Kruger steroids, state of cloud security, puppies>beer
196: AI killed devops, what now? LLM Political bias & AI security Learn how AI killed DevOps, create long tasks in JS, why 1 in 5 security breaches are AI generated code & play "The Scope Creep"
195: End of likes, JS Zoo and Tim Berners-Lee doesn't see AI vs Web Meta kills like buttons, Tim-Berners-Lee thinks AI won't kill the web, GitHub is ending toasts and the worst selling Microsoft product.

My other work: