We are a company of software developers that does not sell a product but provides a wide range of services. Our primary service is developing individual software or maintaining existing software for our customers.
Most of us are polyglot developers each sufficiently fluent in a couple of languages. While I see this as a strength it also means that I do not know the idiomatic solution for a task in all ecosystems in use in our projects by heart.
A few days ago I had a remote-pair-programming session with one of our customers and we wondered how to idiomatically trim certain characters from a string in JavaScript. My customer came up with the idea of asking ChatGPT for a solution while I would have asked Google or stackoverflow.
Being curious we took the ChatGPT route and it delivered a correct solution after I clarified what I mean with “trim”:
function trimCharacterFromStartAndEnd(str, char) {
// Create a regular expression to match the specified character at the beginning and end of the string
const regex = new RegExp(`^${char}+|${char}+$`, 'g');
// Use the replace() function to remove the character from the start and end of the string
const trimmedStr = str.replace(regex, '');
return trimmedStr;
}
// Example usage
const originalString = '###Hello World!###';
const trimmedString = trimCharacterFromStartAndEnd(originalString, '#');
console.log(trimmedString); // Output: Hello World!
But from a professional perspective the code was not simple, concise and idiomatic. I intuitively knew there must be a better way.
So I did what ChatGPT does best: I chatted with it to improve the solution to arrive at a solution a was satisfied with:
function trimCharacterFromStartAndEnd(str, char) {
return str.replace(/^${char}+|${char}+$/g, '');
}
// Example usage
const originalString = '###Hello World!###';
const trimmedString = trimCharacterFromStartAndEnd(originalString, '#');
console.log(trimmedString); // Output: Hello World!
However, you possibly need to handle regex special characters like '.', '*' etc. if they can part of your characters to trim.
Some of the intermediate steps also have their uses depending on the needed flexibility. See the full conversation at trim character from string chat.
Similarily, stackoverflow provides some comprehensive answers you can adapt to your specific situation.
Evaluation
Using ChatGPT can actually provide you useful results. To make the most out of it, you have to be able to judge the solution provided by the AI and try to push it in the wanted direction.
After my experiment our students got the inofficial advice that their solutions should not be worse than what ChatGPT delivers. 😀
Arriving at a good solution was not faster or easier than the traditional developers’ approach using Google and/or stackoverflow. Nevertheless it was more interactive, more fun and most importantly it worked.
It was a bit disappointing to lose context at some points in the conversation, with the g-flag for example. Also the “shortest” solution is longer than the variant with the regex-literal, so strictly speaking ChatGPT’s answer is wrong…
I will not radically change my style of work and jump on the AI-hype-train but I plan to continue experimenting with it every now and then.
ChatGPT and friends certainly have some potential depending on the use case but still require a competent human to judge and check the results.