This introduction to process automation and debugging with AI instruments is excepted from the e book Generative AI Tools for Developers: A Practical Guide, obtainable now on Pylogix Premium.
Activity Automation
One of many methods engineers could make use of AI-powered instruments is with process automation. Many mundane and repetitive duties may be automated with the assistance of AI coding assistants.
For instance, we are able to ask our AI assistant to generate boilerplate code β for frequent duties like making a fundamental REST API with routes, controllers, fashions, and so forth. This protects time initially establishing a venture skeleton.
Utilizing Codeium, right hereβs an instance of how we are able to generate boilerplate code.
We will additionally use Cody to generate boilerplate code.
Right here, we are able to see that, utilizing an AI coding assistant like Codeium or Cody, weβre capable of 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 must be among the many checklist 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. This can be a lot faster than manually writing all of the boilerplate code from scratch.
Right hereβs what Codeium provides us:
const specific = require('specific');
const app = specific();
const port = 3000;
app.get('/', (req, res) => {
res.ship('Hey World!');
});
app.hear(port, () => {
console.log(`Server listening on port ${port}`);
});
Utilizing Cody, right hereβs what we get:
const specific = require('specific');
const app = specific();
app.get('/', (req, res) => {
res.ship('Hey World!');
});
app.hear(3000, () => {
console.log('Server listening on port 3000');
});
Now letβs ask our AI assistant to write some checks. AI instruments can even assist us generate take a look at instances 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 will help engineers who’re nonetheless studying the right way to write checks and likewise assist introduce test-driven growth practices right into a codebase with out checks.
Right hereβs an instance of how we are able to do that utilizing GitHub Copilot Labs.
We will additionally generate checks utilizing Codeium.
On this instance, we see the Copilot Labs take a look at characteristic and Codeium in motion. For it to work, we have to present a operate for it to check in opposition to:
operate 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 = operate (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 capable of generate take a look at instances to cowl completely different situations, equivalent to legitimate, invalid, and edge instances. The take a look at case generated can act as an excellent place to begin for builders to implement correct testing.
Right hereβs what Copilot provides us:
describe('take a look at Untitled_1', operate() {
it('take a look at Untitled-1.milliSecondsToDayHourMin', operate(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 provides 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 among 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 know whatβs going unsuitable.
Right hereβs an instance of how we are able to debug utilizing Codeium:
operate 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;
}
We will 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.
We will see above that Codeium factors out a problem 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 operate.
Right hereβs the Codeium corrected code:
operate 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 capable of debug code by first offering a operate for it to debug. It then produces a working model of the code whereas additionally explaining the true intention of the operate. It additionally goes a step additional by suggesting further concepts to enhance the code.
When utilizing the debug characteristic in Copilot Labs, weβre required to supply a operate for it to debug. After we do that, we are able to see that it routinely fixes our code and provides us a working model of the code:
operate 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 simpler, sooner, and extra environment friendly. Nonetheless, whereas these instruments will 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 slightly 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 show you how to in your work as a developer? Take a look at Generative AI Tools for Developers: A Practical Guide, obtainable now on Pylogix Premium.