# Handling AI generated code

If you keep programming after this course, you will inevitably encounter AI generated code; you might even be the one to produce it. Some code is purely _vibe coded_ (written by an LLM and maybe never inspected by a human programmer). Reasons for this can range from time pressure to limited code literacy of the user. Currently, the fraction of _vibe coded_ scripts is ever increasing as models become cheaper, stronger, and more readily available. 
While LLMs have gotten significantly better at coding since 2023, some fundamental issues remain. This page discusses three of these issues and proposes ways of coping with them. While it is easy to say _just don't use AI, duh_, it is also a cheap answer that fails to take the pressures of the modern workplace into account and does not address code written by others. We aim to address this topic maturely from perspectives of both code author and code reviewer without the often-attached _stigma_ or _hype_. 

## Lack of project ownership

What we mean by _project ownership_ in this section, as opposed to the legal sense, is the ability of the author to grasp the full scope of their code and communicate it.[^naur]

[^naur]: For a more in-depth discussion of this topic and its consequences, read the section "Programming and the Programmers' Knowledge" from Peter Naur's 1985 essay "Programming as Theory Building" {cite}`naur_1985`. Many of the hotly debated issues surrounding _vibe coding_ are — in fact — older than most currently active programmers.

Using LLMs, it is very easy to get started on a project quickly. At the early stages, the behavior of the code is simple enough to understand from its output alone. However, as the complexity increases, the programmer may find themselves unable to understand the full scope and architecture of the project anymore. This can cause a vicious cycle, where new edits require the use of an LLM even if the programmer would have been fully capable of understanding the code, had they written it themselves. 
A common symptom of this cycle is the slow decay of code quality and structure as the project is treated only as a _black box_ (a complicated system that cannot be looked inside). This lowers the barrier for adding _more_ code that strongly interacts with other chunks of code — further fueling the cycle. 

### Help, I think this is happening to me

While not foolproof, following some old-school advice for coding can reduce the severity of this issue. 

**Make a plan for the project architecture beforehand** (_you_, not just your LLM). _What do you want your project to do?_ _What features would this require?_ _What consequences do these have on the project?_ The rationale behind this advice is to avoid losing oversight and control of the structure of your project. It also helps a project start more structured, creating a buffer that delays the negative effects of _some_ loss of structure. {doc}`Pseudocode <chapter-08-flow-control/pseudocode>` is a good tool for making such a plan.

**Do not allow your LLM to make changes that would be too large to describe in a commit message.** If **you** can describe a particular set of changes in a single coherent sentence, it is easier to stay in control of the project. Write the commit messages yourself and question the LLM if you do not understand a feature. A particularly useful habit for keeping track is _making_ the commits yourself and looking through each change with `git diff` (see {ref}`good commits <good-commits>`).

**Test code between changes** by running it and checking its output. This is important to keep track of the effect of changes on the _output_ of your code. Especially code that produces a visual output (biomolecular simulations, data visualization, etc.) necessitates an understanding of the relation between code and output such that you retain the ability to make targeted manual changes to visual features. Recent models tend to write automated tests (code that checks other code) by themselves, or at least suggest it as an option. This is, however, not a substitute for checking the output yourself, as the tests encode the _assumptions_ of the LLM. 

### Ugh, this code looks terrible. What was the author thinking... Or were they?

If you have some programming experience, a lack of project ownership quickly becomes apparent when reading code written by another author. Common features include:

1. A lack of coherent program flow, making it difficult to follow what code is executed when.

2. Overly complicated code that arises from only _adding_ logic, _never removing_ it.

3. The use of multiple _different solutions_ for the _same problem_ in the same script. 

Crucially, these issues can arise without the author having used an LLM at all. They can be a sign of long pauses between work on the code (the author simply forgetting details), a bad project plan requiring unnecessary interaction between features, or collaboration between many programmers, where nobody has the whole picture. 
If you encounter this in someone else's code and cannot find your way through the structure, contact the authors first. There _might_ actually be an idea behind the apparent chaos and the authors can help you understand and extend the code {cite}`naur_1985`. If not, consider scavenging the usable parts and starting over — it sometimes really _is_ faster. 

## Mismatched design intent

If a programmer uses an LLM as the primary interface for adding new features to a piece of software, any design intent has to be communicated to the model. The level of detail provided in these instructions will vary depending on the thoroughness of the programmer. This can range from a single sentence to a full _design spec_ (a document essentially containing a blueprint for the project). 
Either way, it is likely that at least _some_ decisions still have to be made during development as new issues are identified, leading to one of the three following scenarios:

*The LLM asks an informed user about the decision.* The programmer then suggests what they believe is the best option or decides that the exact choice does not matter.

*The LLM asks an uninformed user about the decision.* If the programmer does not know the implications of each option, they most likely pick the one recommended by the model. Importantly, they will only know about the options listed.

*The LLM silently makes the decision.* This is the most treacherous scenario as it is the hardest to notice without reviewing the code. This bakes the model's assumptions about what _you_ want into the code without your input and may present a major failure of communication if you are not aware that this could happen in the first place. 

Importantly, these design decisions impact how the program works and can be very difficult to revert because other parts of the program build on them. This becomes an issue when the decision made by (or in collaboration with) the LLM does not match what the programmer _actually_ wants their program to do, or breaks the program because they were unaware of an introduced constraint. Accordingly, the consequences can range from mundane (a color scheme) to catastrophic (leaking sensitive user data).  

### Can I avoid this?

In short: yes. You need to be sufficiently proficient in the programming language used to review the code produced by the LLM with a critical eye. This has two unfortunate side effects. Firstly, it is a lot of work. If the program is complex, reviewing it can take a similar amount of time as authoring it yourself. Secondly, (in the opinion of the author) reviewing is a lot less fun than coding. 

If the model asks you to make a design decision (_does the program need to access the internet?_, _what package do you want to use for data processing?_, ...), take the time to think it through. If you are not sure, ask the model about the implications *and* perform at least five minutes of internet search to give you enough context for the decision. 

Finally, it is important to stay critical and not to outsource all thinking to the LLM. If _anything_ feels off, raise concerns and take over control. This is especially the case with sensitive subject matter or code that directly impacts humans.

### Why is this (not) here?

Programming is often a collaborative process and involves reviewing code written by others before it is _deployed_ ("released into the wild"). Some decisions made by other programmers or their LLMs will give you pause, because you would have done it differently or because you do not see the relevance of a feature (_why does this toaster need wifi???_). It is generally advisable to nurture a critical attitude about these design decisions. This boils down to repeatedly questioning _why each chunk of code is important_ and _why a chunk of code was designed *this* way_. Similarly, it is good practice to think about edge cases that could break a program while reviewing it. You likely have a different perspective than the author and can help _patch_ (fix after the fact) weaknesses. 
In summary, aside from the obvious benefit of gaining a better understanding of the code architecture, this lets you catch wrong or missing assumptions. Just make sure to then communicate perceived issues to the author and discuss them. 

## Liability for code

As addressed above, code can affect the _analogue (real) world_ and, consequently, LLMs can affect the _analogue world_ via the code they write. This can cause *big* issues if the code harms a person, causes property damage, or infringes on intellectual property. _Typically_, the skill of a programmer correlates with their level of access to vulnerable systems, the complexity and ambition of their projects, and, consequently, their opportunity to do harm. The unique risk that LLMs pose in this context stems from the fact that they can act as force multipliers that quickly let a programmer write and deploy programs whose consequences they cannot understand. This holds especially true when _vibe coding_, authority, and time pressure coincide. 
To use terminology from Git: if a programmer commits code, they are responsible for it. This still holds if the programmer did not _write_ the code, but asked an LLM to do it instead. 

### What does this mean for me? 

If you do not plan to let your code touch user data, interact with the internet, or steer physical machinery, and do not publish your code, you are _likely shielded_ from the extreme scenarios painted above. However, you are still accountable to your organization for the code you write. Check (and follow) the rules of your university on AI use if you want to stay on the safe side (see also {doc}`AI in learning programming <chapter-01-introduction/ai>` and {doc}`AI in programming <chapter-06-ai/intro-to-Ai>`). 

However, maybe you _do_ find yourself in the situation where your code can have _real_ consequences, be it because you use Python to compile data from members of your student association, or because you get hired by a company in a developer role. Then be careful not to rely on AI to do something that you could not do yourself. 

### Should this concern me if I do not use AI?

You may at some point need to extend code that somebody else has _vibe coded_. If they underestimated the consequences of a feature, your own additions may be the tipping point that turns hypothetical problems into real harm. If you get the impression that your collaborator does not understand the code they have authored, make sure to first really familiarize yourself with the code before making changes. If you then find something that could pose a risk, raise concerns with them immediately. 

## Perspective

This book has introduced you to programming with Python. In a world where LLMs can reverse engineer commercial software suites, [port half-million-line code bases to Rust](https://bun.com/blog/bun-in-rust), and occupy headlines for half a decade, some people will claim that the skills you have just learned will be obsolete in two months (irrespective of today's date). However, the hard and soft skills you built while doing so have tended to stay relevant through changes of a similar magnitude. Much of the programming advice from forty years ago is just as true today as it was then {cite}`naur_1985`. If anything, it is _easier_ to use AI for coding if you can code in the first place, something that holds especially true if you want to code something that matters. 

## References

```{bibliography} ../references.bib
:filter: docname in docnames
```
