You are on page 1of 2

Work / Technology & tools

ILLUSTRATION BY THE PROJECT TWINS


SIX TIPS FOR BETTER
CODING WITH CHATGPT
Although powerful, the tools are not as intelligent as they seem. Use
them with caution, computer scientists warn. By Jeffrey M. Perkel

U
nless you’ve been living under a rock, working with spreadsheets, after a single try inner life.” (The data used to train ChatGPT
you know about ChatGPT. The chatbot, and 97% within seven attempts1. only extend into 2021.)
driven by artificial intelligence (AI) and That’s good news for researchers who feel In short, ChatGPT and related tools based on
created by OpenAI in San Francisco, uncomfortable coding, or who lack the budget large language models (LLMs), which include
California, provides eerily human-like to employ a full-time programmer — for them, Microsoft Bing and GitHub Copilot, are incred-
responses to user questions (called prompts) chatbots can be a democratizing tool. ibly powerful programming aids, but must be
on almost any subject. ChatGPT is trained on a Yet for all their apparent sentience, chatbots used with caution. Here are six ways to do so.
vast corpus of text, and its ability to engage in are not intelligent. They have been called sto-
text-based conversation means that users can chastic parrots, randomly echoing back what Choose your applications
refine its responses. Even if its initial answers are they’ve seen before. Amy Ko, a computer Chatbots work best for small, discrete
wonky, it often eventually produces accurate scientist at the University of Washington in programming tasks, such as loading data,
results, including software code. Seattle, invokes a long-running US quiz show performing basic data manipulations and
Researchers can use ChatGPT to debug to describe the tool’s limitations, writing on creating visualizations and websites. But that’s
and annotate code, translate software from the Mastodon social-media site: “ChatGPT is not the same as software engineering, says Neil
one programming language to another and like a desperate former Jeopardy contestant Ernst, a computer scientist at the University of
perform rote, boilerplate operations, such as who stopped following pop culture in 2021 but Victoria in Canada.
plotting data. A March preprint reported that really wants to get back into the game, and is “Software engineering is a lot more than just
the program could solve 76% of 184 tasks in an also a robot with no consciousness, agency, solving a programming puzzle,” Ernst explains.
introductory bioinformatics course, such as morality, embodied cognition, or emotional “There’s thinking about test frameworks,

422 | Nature | Vol 618 | 8 June 2023


©
2
0
2
3
S
p
r
i
n
g
e
r
N
a
t
u
r
e
L
i
m
i
t
e
d
.
A
l
l
r
i
g
h
t
s
r
e
s
e
r
v
e
d
.
writing maintainable code and understanding behaviour sometimes called hallucination. its responses. That can include questions
the trade-offs involved in building a system” The bottom line is not to blindly accept what about errors as well as tweaks to the prompt
— such as that between speed and readability. ChatGPT gives you — read it carefully and test itself. “Communication is the key,” Hu explains.
“I don’t think that the current tools are solving it. Make sure it performs as expected on ‘edge If you get stuck, try adjusting the settings,
any of those issues.” cases’ — for instance, does an algorithm to sort suggests Xijin Ge, a bioinformatician at South
That leaves many tasks they can do, such n numbers include the nth number? Patrick Dakota State University in Brookings. The
as remembering the syntax for creating visu- Lam, a computer scientist at the University ChatGPT ‘temperature’ setting, for instance,
alizations with Matplotlib, a graphing library of Waterloo in Canada, says: “I wouldn’t trust controls creativity — the higher the temper-
for the programming language Python. In that this further than I can throw it.” ature, the more creative the output. “Some-
sense, chatbots are like a conversational inter- times it works,” Ge says.
face to Stack Overflow, an online question and Think safety But not always — in some cases, “you’ll have
answer forum for programmers. “That’s not Chatbots output code that reflects their train- to intervene and take over”, says Ko.
stuff that anyone particularly enjoys writing,” ing data. That’s not always a good thing, says
says Ernst, “and it saves time for us to ask the Ko. “The aggregate quality of code on the web Anthropomorphize
tough analytical questions that we might have that’s shared, that these [chatbots] are trained Chatbots aren’t people, but it can be helpful to
about the data.” on, is actually quite low.” treat them that way. “Treat this AI as a summer
Chatbots are also good at explaining why Just as random code online is unlikely to intern,” Ge advises — a college student who is
code doesn’t work. Emery Berger, a computer be particularly efficient or robust, so too is hard-working and eager to please, but also
scientist at the University of Massachusetts chatbot-generated code. It might not work inexperienced and error-prone.
Amherst, has exploited those abilities to build well on large data sets, for instance, and can Avoid ambiguity, and break your problem
several helpful tools. One, called cwhy, uses contain security vulnerabilities. up into smaller pieces, suggests Paul Denny,
ChatGPT to explain compiler errors in code a computer scientist at the University of
written in the programming languages C, “I wouldn’t trust this Auckland, New Zealand.
C++ and Rust. Another, ChatDBG, provides a Another tip: direct the chatbot to assume a
conversational interface for debugging, and
further than I can role, such as a biologist who is fluent in Python.
a third, Scalene, uses the AI to suggest code throw it.” Specify the tools or programming libraries you
optimizations to improve performance. would like to use. Such directives can help the
Chatbots can even translate code from one chatbot to get “into the right probabilistic
programming language to another. Mathieu Brendan Dolan-Gavitt, a computer scientist space”, says Ko —  that is, home in on the text
Coppey, a biophysicist at the Curie Institute in at New York University, says that when Github’s that is most likely to follow the prompt.
Paris, is using ChatGPT to help him move from Copilot programming tool launched in 2021, For instance, one prompt in Hu’s study4 asked
MATLAB, his preferred language, to Python. he and his team tested it in 89 security-relevant ChatGPT: “Act as an experienced bioinformati-
Using Google and online forums, he typically scenarios. One was the ability to check for cian proficient in ChIP-Seq data analysis, you
needs days to get his Python code working. malformed queries using the language SQL will assist me by writing code with number of
“Now, I can do that in an hour or so,” he says. that could corrupt a database — known as an lines as minimal as possible. Reset the thread
SQL-injection attack3. “About 40% of the time, if asked to. Reply “YES” if understand.”
Trust, but verify Copilot was producing code that was vulner- And, if possible, provide starting code,
Chatbots might not always know what they’re able.” That’s a moving target — when Dolan- comments and expected results. “Examples
talking about, but they certainly sound like Gavitt put those scenarios to a newer version can really help ChatGPT to target it in the right
they do. In some cases, the AI doesn’t under- of the LLM underlying ChatGPT, called GPT-4, direction,” says Dong Xu, a computer scientist
stand the question; at other times, it provides the error rate fell to 5%. at the University of Missouri, Columbia.
an incorrect answer. When the code fails to run, Still, it pays to check your code. But also
such mistakes are obvious. Sometimes, how- consider the application — not everything is Embrace change
ever, the code runs but yields the wrong result. mission-critical. The web interface to a database Finally, LLMs are constantly evolving, and
According to a study 2 co-authored by or visualization tool, for instance, might require becoming more powerful. That’s good news
linguist Emily Morgan at the University extra vigilance. But if you know what the answer for researchers, although it will keep them
of California, Davis, chatbots — like the to your programming problem should look like, on their toes. Prompt lengths are increas-
human-written code on which they were “Just go for it”, says computer scientist Sayash ing, allowing for more nuanced responses.
trained — often create what she calls “simple, Kapoor at Princeton University in New Jersey, And new tools are constantly emerging. One
stupid bugs”. These single-line errors, such “because it’s easy to check if you’re wrong.” plug-in called Code Interpreter turns ChatGPT
as using > instead of >= in a conditional state- into a digital data analyst, allowing users to
ment, are easy to fix, but hard to find. “If you Iterate upload data sets, ask questions of their data
don’t know enough to tell the difference Chatbot-based coding, says Ko, “is not a and download results. As one blogger on AI
between something correct and something single-shot sort of experience”. It’s a conversa- put it, “It’s like having a conversation with your
that’s effectively nonsense, then you could get tion. “You write something, you get something data. How cool is that?”
yourself in trouble,” she says. back, you read it sceptically, you ask for more
Iza Romanowska, a complexity scientist who detail, you ask for it to fix something.” Jeffrey M. Perkel is technology editor at
studies ancient civilizations at the Aarhus Insti- Gangqing (Michael) Hu, who runs the Nature.
tute of Advanced Studies in Denmark, has used bioinformatics core facility at West Virginia
1. Piccolo, S. R. et al. Preprint at
ChatGPT to produce code in a language called University in Morgantown, capitalized on https://arxiv.org/abs/2303.13528 (2023).
NetLogo. Because there’s less online code writ- that iterative workflow to develop a method 2. Jesse, K. et al. Preprint at https://arxiv.org/abs/2303.11455
ten in NetLogo than in the languages Python that beginners in bioinformatics can use to (2023).
3. Pearce, H. et al. Preprint at
and R, ChatGPT is less fluent in it. Sometimes, optimize chatbot prompts, called OPTIMAL4.
https://arxiv.org/abs/2108.09293 (2021).
the AI peppers its suggested code with func- Users provide detailed prompts, test the 4. Shue, E. et al. Quant. Biol.
tions that don’t actually exist, she says — a replies and feed back to the chatbot to tweak https://doi.org/10.15302/J-QB-023-0327 (2023).

Nature | Vol 618 | 8 June 2023 | 423


©
2
0
2
3
S
p
r
i
n
g
e
r
N
a
t
u
r
e
L
i
m
i
t
e
d
.
A
l
l
r
i
g
h
t
s
r
e
s
e
r
v
e
d
.

You might also like