DEV Community

Cover image for Getting rid of those console.log('here') statements with GitHub Copilot
Christopher Harrison for GitHub

Posted on • Updated on

Getting rid of those console.log('here') statements with GitHub Copilot

There are two types of developers in the world - those who use console.log('here!') for debugging and then check that code in, and... Actually, I take that back. There's one type of developer in the world. Fortunately, GitHub Copilot can help us clean that up really quickly.

I've got some code with debug statements embedded. I'm also grabbing a temp variable for some data which I would normally just return directly.

async getAll(): Promise<Runner[]> {
    try {
        const runners = await this.prisma.runner.findMany();
        console.log("Runners:", runners);
        return runners;
    } catch (error) {
        console.error("Error getting all runners:", error);
        throw error;
    }
}

async getById(id: number): Promise<Runner | null> {
    try {
        console.log(id);
        return await this.prisma.runner.findUnique({
            where: { id },
        });
    } catch (error) {
        console.error(`Error getting runner by id "${id}":`, error);
        throw error;
    }
}

async getByName(name: string): Promise<Runner[]> {
    try {
        console.log(name);
        console.log('here');
        return await this.prisma.runner.findMany({
            where: { name: { contains: name } },
        });
    } catch (error) {
        let sanitized_name = name.replace(/\n|\r/g, "");
        console.error(`Error getting runners by name "%s":`, sanitized_name, error);
        throw error;
    }
}
Enter fullscreen mode Exit fullscreen mode

I want to clean all of that up. I highlight the code (which sets the context for GitHub Copilot Chat), then ask GitHub Copilot Chat "Remove any debug statements and any unnecessary variables".

Screenshot of GitHub Copilot Chat requesting chat to Remove any debug statements and any unnecessary variables

Within just a few moments, Copilot returns back my cleansed code which I can paste into my editor:

async getAll(): Promise<Runner[]> {
    try {
        return await this.prisma.runner.findMany();
    } catch (error) {
        console.error("Error getting all runners:", error);
        throw error;
    }
}

async getById(id: number): Promise<Runner | null> {
    try {
        return await this.prisma.runner.findUnique({
            where: { id },
        });
    } catch (error) {
        console.error(`Error getting runner by id "${id}":`, error);
        throw error;
    }
}

async getByName(name: string): Promise<Runner[]> {
    try {
        return await this.prisma.runner.findMany({
            where: { name: { contains: name } },
        });
    } catch (error) {
        console.error(`Error getting runners by name "${name}":`, error);
        throw error;
    }
}
Enter fullscreen mode Exit fullscreen mode

Now, should I learn to use the debugging capabilities of Visual Studio Code instead of relying on strategies from decades ago? Probably. :D But until that happens, I can at least cleanup everything quickly and efficiently with Copilot.

I think one of the things which is often overlooked about GitHub Copilot is it's not just about creating new code, but also performing other related tasks and cleanup. When you come across a situation similar to this, where you're worried something might take a while, ask Copilot. You might just find it's able to perform the task with just a couple of quick questions. What have you been able to offload to Copilot which surprised you? Comment below!

Top comments (0)