4.5 C
New York
Sunday, January 14, 2024

Process Automation and Debugging with AI-Powered Instruments


This introduction to process automation and debugging with AI instruments is excepted from the guide Generative AI Instruments for Builders: A Sensible Information, out there now on SitePoint Premium.

Desk of Contents

Process Automation

One of many methods engineers could make use of AI-powered instruments is with process automation. Many mundane and repetitive duties could be automated with the assistance of AI coding assistants.

AI automation tasks list

For instance, we are able to ask our AI assistant to generate boilerplate code — for widespread duties like making a primary REST API with routes, controllers, fashions, and so forth. This protects time initially establishing a mission skeleton.

Utilizing Codeium, right here’s an instance of how we are able to generate boilerplate code.

Codeium generating boilerplate code

We are able to additionally use Cody to generate boilerplate code.

Cody generating boilerplate code

Right here, we are able to see that, utilizing an AI coding assistant like Codeium or Cody, we’re in a position to rapidly create a easy Specific server that’s sufficient to get began with the essential construction and routes outlined. To do that, we’ve got to click on on the Codeium icon, which needs to be among the many listing of extensions on the left aspect of our editor (or proper, relying on the place we’ve positioned the icons). Doing this opens up the Codeium chat interface that permits us to speak with the Codeium server. It is a lot faster than manually writing all of the boilerplate code from scratch.

Right here’s what Codeium offers us:

const categorical = require('categorical');
const app = categorical();
const port = 3000;

app.get("https://www.sitepoint.com/", (req, res) => {
res.ship('Whats up World!');
});

app.hear(port, () => {
console.log(`Server listening on port ${port}`);
});

Utilizing Cody, right here’s what we get:

const categorical = require('categorical');

const app = categorical();

app.get("https://www.sitepoint.com/", (req, res) => {
res.ship('Whats up World!');
});

app.hear(3000, () => {
console.log('Server listening on port 3000');
});

Now let’s ask our AI assistant to write some assessments. AI instruments also can assist us generate take a look at circumstances as a place to begin for testing our code. This ensures code is examined from the start, which is vital for high quality and avoiding bugs. This might help engineers who’re nonetheless studying how one can write assessments and in addition assist introduce test-driven growth practices right into a codebase with out assessments.

Right here’s an instance of how we are able to do that utilizing GitHub Copilot Labs.

We are able to additionally generate assessments utilizing Codeium.

On this instance, we see the Copilot Labs take a look at function and Codeium in motion. For it to work, we have to present a perform for it to check in opposition to:

perform milliSecondsToDayHourMin = (timeRemaining) => {
  let dayInMilliseconds = 86400000;
  let hourInMilliseconds = 3600000;
  let day = Math.flooring(timeRemaining / dayInMilliseconds);
  let hour = Math.flooring(
     (timeRemaining - day * dayInMilliseconds) / hourInMilliseconds
  );
  let min = Math.spherical(
     (timeRemaining - day * dayInMilliseconds - hour * hourInMilliseconds) /
        60000
  );
  let seconds = Math.spherical(timeRemaining - min / 60);
  const pad = perform (num) {
     return num < 10 ? "0" + num : num;
  };
  if (min === 60) {
     hour++;
     min = 0;
  }
  if (hour === 24) {
     day++;
     hour = 0;
  }
  return `${day} : ${pad(hour)}  :  ${pad(min)}:  ${pad(seconds)}`;
};

After doing this, it’s in a position to generate take a look at circumstances to cowl completely different eventualities, akin to legitimate, invalid, and edge circumstances. The take a look at case generated can act as a very good start line for builders to implement correct testing.

Right here’s what Copilot offers us:

describe('take a look at Untitled_1', perform() {
  it('take a look at Untitled-1.milliSecondsToDayHourMin', perform(completed) {
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(0), '0 : 00  :  00:  00');
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(60000), '0 : 00  :  01:  01');
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(3600000), '0 : 01  :  00:  60');
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(86400000), '1 : 00  :  00:  60');
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(172800000), '2 : 00  :  00:  60');
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(259200000), '3 : 00  :  00:  60');
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(604800000), '7 : 00  :  00:  60');
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(604800000), '7 : 00  :  00:  60');
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(86400000 + 3600000 + 60000), '1 : 01  :  01:  01');
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(86400000 + 3600000 + 60000 + 1000), '1 : 01  :  01:  01');
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(86400000 + 3600000 + 60000 + 1000), '1 : 01  :  01:  01');
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(86400000 + 3600000 + 60000 + 1000), '1 : 01  :  01:  01');
     assert.deepEqual(Untitled_1.milliSecondsToDayHourMin(86400000 + 3600000 + 60000 + 1000), '1 : 01  :  01:  01');
     completed();
  })
})

And right here’s what Codeium offers us:

take a look at("milliSecondsToDayHourMin - ought to return 0 : 00 : 00 : 00 for 0 milliseconds", () => {
  count on(milliSecondsToDayHourMin(0)).toBe("0 : 00 : 00 : 00");
});

take a look at("milliSecondsToDayHourMin - ought to return 1 : 00 : 00 : 00 for 86400000 milliseconds", () => {
  count on(milliSecondsToDayHourMin(86400000)).toBe("1 : 00 : 00 : 00");
});

take a look at("milliSecondsToDayHourMin - ought to return 1 : 01 : 00 : 00 for 90000000 milliseconds", () => {
  count on(milliSecondsToDayHourMin(90000000)).toBe("1 : 01 : 00 : 00");
});

take a look at("milliSecondsToDayHourMin - ought to return 1 : 01 : 01 : 00 for 90060000 milliseconds", () => {
  count on(milliSecondsToDayHourMin(90060000)).toBe("1 : 01 : 01 : 00");
});

take a look at("milliSecondsToDayHourMin - ought to return 1 : 01 : 01 : 01 for 90061000 milliseconds", () => {
  count on(milliSecondsToDayHourMin(90061000)).toBe("1 : 01 : 01 : 01");
});

Debugging

AI-powered instruments are additionally helpful for debugging. Debugging is without doubt one of the most time-consuming duties any engineer offers with every day, no matter their expertise stage. Integrating AI instruments into the debugging workflow reduces the time spent attempting to grasp what’s going incorrect.

Right here’s an instance of how we are able to debug utilizing Codeium:

perform cleanupFounders() {
  const founders = this.orgInfo.founders;
  foundeers.map((doc) => {
     delete doc.organisation;
     delete doc.createdAt;
     delete doc.updatedAt;
     delete doc.__v;
  });
  this.orgInfo.founders = founders;
}

Codeium Debugging

We are able to additionally use Cody to debug code.

And right here’s one other instance of how we are able to do that utilizing GitHub Copilot Labs.

Copilot Debugging

We are able to see above that Codeium factors out a difficulty within the code by first understanding the founders variable declaration and discovering the typo within the code. It then produces a working model of the code (with out the typo) whereas additionally explaining the true intention of the perform.

Right here’s the Codeium corrected code:

perform cleanupFounders() {
  const founders = this.orgInfo.founders;
  founders.map((doc) => {
    delete doc.organisation;
    delete doc.createdAt;
    delete doc.updatedAt;
    delete doc.__v;
  });
  this.orgInfo.founders = founders;
}

Utilizing Cody, we’re additionally in a position to debug code by first offering a perform for it to debug. It then produces a working model of the code whereas additionally explaining the true intention of the perform. It additionally goes a step additional by suggesting additional concepts to enhance the code.

When utilizing the debug function in Copilot Labs, we’re required to offer a perform for it to debug. After we do that, we are able to see that it robotically fixes our code and offers us a working model of the code:

perform cleanupFounders() {
  const founders = this.orgInfo.founders;
  this.orgInfo.founders = founders.map((doc) => {
    delete doc.organisation;
    delete doc.createdAt;
    delete doc.updatedAt;
    delete doc.__v;
    return doc;
  });
}

With these instruments, debugging is less complicated, quicker, and extra environment friendly. Nonetheless, whereas these instruments might help us out with all these duties, we nonetheless must go over the options with the intention to confirm the standard of the code. This manner, we get to spend time studying quite than debugging, looking out the Web, and getting annoyed over bugs in our code.

Wish to be taught extra about chatbots, LLMs and different AI instruments that may aid you in your work as a developer? Take a look at Generative AI Instruments for Builders: A Sensible Information, out there now on SitePoint Premium.



Supply hyperlink

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles