Introduction
Over the past year (2022-2023) technology companies have released a staggering variety of AI (Artificial Intelligence) tools that can generate text, code, images, music, and even video clips on-demand. Lately it feels like new AI tools are coming out every week, and it is impossible to keep up with the latest buzzwords and marketing slogans. For this article, I want to zoom in on one kind of AI tool that is especially relevant for scientists and engineers: text-based tools such as ChatGPT and GitHub Copilot that take text (or code) from the user and then generate text (or code) in response to the user’s request. Specifically, I will present six opportunities for scientists and engineers to use these AI tools to learn computer programming skills that are relevant for their jobs.
As AI tools have grown more popular, educators have started writing about ways to use them to teach and learn programming. However, most writing on this topic so far has been about using AI tools (especially within computer science departments) to teach students who aim to become full-time programmers [TODO: cite stuff]. In contrast, in this article I want to provide a complementary perspective by addressing the unique needs of scientists and engineers who do not intend to become professional software developers. For instance, a climate scientist may need to pick up a bit of Python or R to analyze data for their research; or a mechanical engineer may learn command-line scripting on embedded Linux systems because they are rigging together hardware components for a measurement device. There
Background: Frequently-Mentioned Thoughts about Using AI Tools to Learn Programming
Before focusing on scientists and engineers in particular, I will first set the stage by summarizing what others have already mentioned about using AI to learn programming in general. I distilled these sentiments from several overview papers [TODO: cite a few], which each cite more detailed research studies in their respective bibliographies.
As of around November 2023 (only a year after ChatGPT was released), here are some widely-acknowledged capabilities of AI tools for learning programming, especially within the context of formal computer science courses:
- AI tools can accurately solve many kinds of programming exercises that are now used as homework and exam questions in introductory courses. As a result, some instructors are worried about students using them to cheat and are reconsidering what kinds of homework assignments and exams to give in the future.
- On a more constructive note, since AI can generate a variety of different solutions for a programming exercise, those can serve as worked examples (also known as worked-out examples [X]) that students can use to learn different approaches to solving a problem.
- AI can automatically generate a wide variety of programming exercises to meet a given pedagogical goal (e.g., teaching how to join multiple data tables in Python using the pandas library). This capability can both help instructors to prepare assignments more efficiently and provide students with extra practice opportunities on-demand.
- AI can explain what a piece of code does step-by-step in a novice-friendly way. This capability can benefit students who may feel embarrassed to ask someone for help on a seemingly ‘simple’ question; they can now freely ask the AI without fear of being judged.
- AI can help debug students’ code, which can enable them to make progress on their homework again without feeling self-conscious about asking someone else for help.
- AI can automatically write tests for students’ code, which can help them spot more bugs.
- AI can perform code reviews to give students feedback on their coding style and aesthetics.
And here are commonly-discussed drawbacks of these tools:
- First and foremost, AI tools can generate code that is incorrect, buggy, insecure, or violates other known best practices. Moreover, students may have trouble spotting the bugs in AI-generated code since it usually ‘looks’ well-written and correct at first glance.
- Relatedly, since AI-generated code looks so convincing at first glance, students may grow over-reliant and reflexively copy-paste it into their projects without questioning whether it is correct. As a result, instructors have emphasized that it is even more important for students to learn to write rigorous test cases for AI-generated code [X].
- Even if AI-generated code is correct and of high-quality, the fact remains that the student did not write that code themselves. This makes it harder for them to understand what the code does and to make future updates to it.
- AI tools are optimized for “doing” rather than teaching. This means when a student asks the AI a question, it will directly give them the answer as requested. While this can be convenient, it hinders learning. In contrast, a good human tutor would teach the underlying concepts and gradually guide the student to solve the problem on their own (with well-timed hints along the way) instead of giving them the answer right away.
- It is hard for novices to develop a mental model of what these AI tools are and are not capable of, since how they work is mysterious even to experts. Thus, students may get frustrated that AI cannot help them with seemingly-straightforward requests, when in reality those requests are expressed unclearly. In practice one must learn to become good at prompt engineering [X] to be able to write prompts (i.e., textual requests to the AI tool) that elicit high-quality responses.
- The code or explanations that AI tools produce by default may be too complex for beginners to understand, since AI mimics the style of code found on the internet and is not specifically tuned to be beginner-friendly.
- AI tools may generate text that reinforces existing social biases, contains toxic content, or uses copyrighted materials without the original creators' permission.
Note that while I will be presenting new opportunities for AI to help scientists and engineers learn programming, it is important to also be aware of the above limitations and to acknowledge that using AI does carry some level of risk. With that in mind, let’s get started!
Opportunity 1: Using AI to Create Customized Programming Tutorials for Your Own Domain
By far the opportunity I am most excited about is using AI tools to create programming tutorials that are customized for a scientist or engineer’s own domain of work. Why is this significant? Because scientists and engineers often learn programming to analyze their data, but existing tutorials all use generic datasets such as the popular ‘iris’ or ‘mtcars’ data that come pre-installed with the R programming language. The ‘iris’ dataset describes 150 samples of iris flowers with features such as their sepal and petal dimensions; and ‘mtcars’ catalogs information about 32 car models from the early 1970s, with features such as their number of carburetors, horsepower, and rear axle ratio. Unless you happen to be a flower or car enthusiast, chances are that you do not care at all about this data, so learning programming using these datasets may not be the most motivating for you.
Nowadays, instead of reading tutorials that use generic datasets, you can use an AI tool like ChatGPT to generate customized tutorials using example data from your own domain of work. No matter if you are a geologist or astrophysicist or structural engineer, AI tools possess enough knowledge about the basics of your field to be able to generate synthetic data and code examples that you can relate to more than ‘iris’ or ‘mtcars.’ For instance, if you are learning how to do multiple linear regression in MATLAB, it can be far more relatable to see examples using data from your own domain (e.g., geology) rather than, say, predicting car horsepower from rear axle ratios. You can even paste in real datasets from your own research (e.g., a neuroscience study) and have the AI tool generate a programming tutorial based on your data. Learning programming using authentic data in a domain you personally care about can make that knowledge stick better than reading generic programming tutorials [X,X,X]. For more details, I discuss a concrete example of this idea in the “Intermission 1: ChatGPT as a Personalized Tutor” section of my Real-Real-World Programming with ChatGPT article [X].
Opportunity 2: Using AI to Learn Complex Data Visualization Libraries
Another staple of scientific programming is writing code to produce data visualizations ranging from simple bar charts all the way to interactive multi-scale dynamic diagrams. Scientists face an inevitable tradeoff here – they can either a) use a point-and-click interface like Excel or Google Sheets, or b) write custom Python/R/JavaScript/MATLAB/etc. code using libraries such as Matplotlib, Seaborn, ggplot2, Bokeh, Plotly, Altair, or D3.js. The former is easier to learn but offers less expressive power, while the latter is more expressive but harder to learn.
AI tools can help lower the barriers to learning to use the latter category of data visualization libraries. Similar to Opportunity 1, you can use AI to generate tutorials for how to use these libraries within the context of the data you are currently working with. For instance, let’s say you are a marine biologist wanting to make a scatterplot to correlate observations of different types of fish, and you want the data points to vary in color, shape, and size according to certain fishy properties. Even though you may not know how to write the exact code to do so, you have a clear vision of what the output should look like. By expressing this request to an AI tool, it can generate the data visualization code and add inline comments to teach you how that code works step-by-step.
Using AI to write your data visualization code can be effective since it is something that is hard for humans to do but easier for humans to verify. Let’s face it – not even seasoned programmers remember how to write Matplotlib, ggplot2, or other visualization code from scratch. These complex libraries contain hundreds of different functions, each with many different parameters that interact in idiosyncratic ways. It is a waste of our human brainpower to memorize all of these mundane details, but fortunately AI is great at “remembering” these details for us. Since we have an intuitive sense of what the output visualization should look like, we can verify whether the AI-generated code looks correct and make adjustments if needed. This ease of human verification gets around a core limitation of AI tools, which is that they might generate incorrect code. (However, note that looking at the visualization alone may not be enough to fully verify correctness, so it is still important to inspect the AI-generated code to make sure that its logic makes sense.)
Opportunity 3: Using AI to Learn to Refactor Exploratory Code into More Maintainable Software
Another thing many of us do (but may not publicly admit) is writing messy code that does not follow best practices. Scientific programming is often exploratory in nature and done in a mix of creatively-named files (e.g., MY_ANALYSIS_SCRIPT_v2_param53_final_FINAL.py) and computational (e.g., Jupyter or R) notebooks. Our main goal, especially during the early stages of research, is to iterate quickly to explore and refine hypotheses, not to produce clean maintainable code. But if our initial explorations are successful, then inevitably our initial draft code ends up living far longer than we originally intended. So one big challenge for scientists and engineers is learning to refactor this prototype code into something more maintainable longer-term.
Refactoring is a software engineering technique where a programmer revises code to be more clear and maintainable while still maintaining the same existing functionality. AI tools can help here by inspecting your code and suggesting refactoring opportunities such as creating more descriptive variable names, encapsulating common snippets into their own functions, making indentation and spacing more consistent, and adding inline comments to describe what each part of your code does. By seeing how AI refactors your code, you can learn habits that you can apply in the future. In this way, AI plays the role of a senior colleague who demonstrates best practices within the context of your own codebase. These in-situ, context-specific lessons can stick better than if you had read a general guide to code refactoring that did not use your own codebase as a personalized example.
Similar to Opportunity 2, this can be a great use case for AI since it is relatively easy for a human to verify whether the output is correct. Since you already have code that works, the AI-refactored code ought to behave the exact same way when you run it. You can look at the old and new versions side-by-side and run both to give you confidence that the AI worked as intended. Using AI to refactor can be less risky than using it to write brand-new code.
Opportunity 4: Using AI to Learn to Work with Inherited Legacy Code
Scientists often inherit code from former lab members who have graduated or move onto new jobs. As mentioned above, ideally everyone would take the time to refactor their exploratory code into something more maintainable. But in reality, lots of old code is hard to understand since it may have been duct-taped together in a hurry to get experiments working for a paper submission deadline. And even if the original authors intended to clean it up and document it well, they are always under pressure to move onto the next project, aim for the next publication or grant deadline, and so on. Plus, we as scientists are not professional software developers, so we may lack the expertise to follow industry best practices for code quality even if we have the best of intentions. The end result is that inherited code (formally called legacy code [X]) can be really hard to understand and work with, which slows down scientific progress.
AI tools can help here by automatically inspecting a pile of old legacy code and generating step-by-step explanations, clarifying code comments, and supplemental documentation to summarize how that code works. These explanations may not be 100% accurate, but they can serve as a starting point for human investigation. Think of the AI here as an intrepid explorer buddy who can help you out when spelunking into a deep cave of unexplored legacy code. By working alongside the AI when exploring an unknown codebase, you can learn both how to work with it specifically and also more general skills for how to effectively deal with legacy code in the future.
Opportunity 5: Using AI to Learn New Programming Languages On-Demand Within the Context of Your Workflow
Scientists and engineers may need to pick up a new programming language on very short notice if an important library they need is available only in that language. Since their goal is to get their job done as effectively as possible, they cannot easily put their work on hold in order to learn a new language by taking a formal course or working through a textbook. Instead, it would be much more convenient to be able to learn just-in-time and on-demand within the context of their own workflow. AI tools can facilitate this in two ways:
- The scientist can write code for their task in the language they are most comfortable with (e.g., Python) and then use an AI tool to automatically translate it into the new language they currently need to learn (e.g., Julia). While this translation is by no means perfect, it is likely “good enough” to show the correspondences between the two languages (e.g., which Python features map to which Julia features). This way, someone can learn a bit of Julia directly within the context of a piece of Python code that they are familiar with.
- Going the other way, a scientist can find a piece of example code in an unfamiliar language (e.g., Julia) and then use an AI tool to translate it back into a language they already know well (e.g, Python). This can come in handy if, say, that piece of example code implements an important algorithm that they need for their research, but they do not understand how it works since they are not familiar with the language it is written in.
Opportunity 6: Using AI to Question the Assumptions Your Scientific Code is Making
One of the most challenging aspects of writing scientific code is making sure the assumptions that underlie your code are well-justified. Even if you implement the most elegant, efficient, and bug-free algorithm to run on your data, if that is not the appropriate algorithm to use, then your code is still useless (or may even be harmful if it gives misleading or biased results). However, it is impossible for existing code analysis tools to tell whether your code may be making incorrect assumptions since these tools do not know anything about the underlying scientific or engineering questions your code is trying to address. Modern AI tools have the potential to overcome this limitation via a clever rhetorical trick: by asking you whether the assumptions you are making are sound and having you come up with answers on your own.
Realistically, AI tools still cannot do your science for you, but what they can do is serve as a skeptical inquisitor to question the assumptions your scientific code is making. For instance, if you are a geneticist writing scripts using the Biopython library to process a specific type of gene sequencing data, an AI tool may know enough about this domain to ask skeptical questions about why you decided to, say, run a specific parametric statistical test, and whether that test is justified given the properties of the data set you are using. Or the tool can question you about why you decided to use linear instead of logistic regression when your outcome variable seems to be binary. The AI does not necessarily know the correct answer to those questions, but it likely knows “enough” about genetics and statistical tests based on its training data to pose these questions for you to reflect on. This use case is like having the AI serve as a sort of Socratic coach [X] to get you to reflect more deeply on your thought process.
Parting Thoughts and Call to Action
In summary, AI tools like ChatGPT offer unique potential for scientists and engineers to learn programming, while also aiding in various aspects of their work. These tools can generate personalized programming tutorials, facilitate learning of complex data visualization libraries, suggest refactoring of exploratory code, and assist in understanding inherited legacy code. Moreover, they may help in learning new programming languages in the context of work and encourage the questioning of assumptions in scientific code. However, it's essential to maintain a balanced perspective and acknowledge the limitations of these AI tools. They are only as good as their training data, can sometimes produce incorrect output, and, importantly, lack true comprehension of context and domain-specific knowledge. They should be seen as aides that can help streamline certain tasks, but are not replacements for the essential expertise, judgment, and creative problem-solving abilities of human scientists and engineers. Therefore, I encourage the scientific community to explore these tools with a critical eye, understanding their strengths and limitations. Use them where they can genuinely add value and continue relying on human intellect and insight where it matters most. In doing so, we can use technology to enhance our work without over-relying on it, striking a beneficial balance in this era of digital advancement.
I promise this prior paragraph is the only one in this article that was written by an AI tool (ChatGPT with GPT-4). It originally adopted a much more enthusiastic tone, but then I instructed ChatGPT to “make it more balanced and less pro-AI,” so this was its second response. Although it is a reasonable summary of this article’s contents, its style does not match how I normally write. So at least for now I will still be doing my own writing without AI assistance, even though I regularly use AI to help me do programming [X]. It is hard to predict how these tools will evolve in the coming years, but hopefully the ideas I presented here can serve as starting points for you to learn more about this fast-changing topic.
Acknowledgments
Thanks to Lorena Barba for encouraging me to write this article, to Shannon Ellis for helping me to brainstorm ideas for it, and to Ashley Juavinett for feedback. This material is based upon work supported by the National Science Foundation under Grant No. NSF IIS-1845900.