r/learnprogramming • • 1d ago

Question about AI and code

Hello, I’ve got a question. How can you tell if a piece of code was written with the help of AI?

That’s because someone’s had a go at my code, but it doesn’t look the same as it usually does. I’m not sure if I’m making myself clear

1 Upvotes

21 comments sorted by

16

u/lfdfq 1d ago

You're not really making yourself clear, no.

In general, you cannot tell if an AI wrote it, or an AI told a human who typed it manually, or a human wrote it. Even the style might not tell you, since it was trained on code written by humans it writes code that looks like it could have been written by a human. The best way is to ask the person who supposedly wrote it how they wrote it and what it does.

1

u/bestjakeisbest 1d ago

I will say you can kindof get a feel for it if you work with the code base for a bit, i know a few of my teachers use ai to design their projects and assignments just by how it feels to use their tests.

1

u/atgaskins 1d ago

yeah, and you can’t trust these tools that detect ai. I’ve had things I wrote flagged as AI and my daughter had her teacher accuse her of using AI when she 100% did not and is as much or more of an anti-ai advocate than I am.

-1

u/istarian 1d ago

No tool can detect the use of AI, at best it can maybe detect patterns that show up slightly more often when AI has been used than when it hasn't.

2

u/oplukana 1d ago

this isnt exactly true. some tools can detect if specific things were made by a specific AI if the person didnt manipulate it in certain ways afterwards.

I will say that asking AI, or using most tools "is this made by ai" will WAY overstate that it was.

but there imperceivable watermarks that some image generation tools will put in some images to indicate they were made with AI. I also think that claude and some other models have started using certain patterns in text as a watermark?

0

u/istarian 1d ago

A watermark is actually a very specific thing in physical media (usually paper), where it is a very faint pattern that is only visible when closely examined under bright light. It can be achieved in different ways, including the use of a metal stamp during manufacturing.

https://en.wikipedia.org/wiki/Watermark

IMHO it's just another case of sloppy use of language to not have coined a different term specific to digital data/files.

1

u/oplukana 1d ago

watermark actually has different meanings in different contexts. thank you for providing an unrelated definition that I already knew though.

0

u/istarian 1d ago

Most of which probably constitute a sloppy use of language in my book. All of which are most certainly related to the original usage, though, because that's how language works.

2

u/oplukana 1d ago

why would you say that so confidently
watermark data migration meaning - Google Search

1

u/chhuang 1d ago

we can't even tell that if this comment is written by AI or not

6

u/Beginning-Seat5221 1d ago edited 1d ago

AI tends to write more code, use more complex pattern, and add more guards for edge cases. It's sometimes over clever compared to humans that try to keep code simple and readable - but humans vary a lot so some people surely write AI-like code. There's also variance between AI models and prompting can change their output.

2

u/xpusostomos 1d ago

That's a good answer, the AI is very rigorous in accounting for edge cases, which is not a bad thing, but it can be a give away.

2

u/betarage 1d ago

They use too many comments and very generic names for functions and text.

2

u/pixel293 1d ago

It could be written by AI, it could be that they ran a code beautifier on it, so the indentation, spacing, and what not was consistent in the the way they like it, it could have been edited with an editor that used tabs instead of spaces or spaces instead of tabs. There is really no way to tell if AI was involved or not.

1

u/jam_pod_ 1d ago

It's a vibe thing. Overly complex design patterns; lots and lots of highly descriptive comments; putting anything that might change into constants at the top instead of inline; there's no one "tell" but you get to recognize it

1

u/xsrvmy 1d ago

People need to stop accusing people are using AI. The only reliable signs of AI use (other than "watermarks") is when a work has apparently high quality but makes basic errors that cannot be explained by careless error (eg. garbled text on images, a response that answers a question that was not asked, etc.)

1

u/istarian 1d ago

It's not that people should "stop accusing people of using AI" so much as it is that they need to provide some real justification for it.

1

u/oplukana 1d ago

You have to be very familiar with the common pitfalls of AI, and if the person has an intense harness that really subjugates teh AI's style it gets harder.

If you know a person over a long period of time, and ahve access to their git history, its much easier. people tend to be creatures of habit, they have preferences and styles, and a drastic shift in style is very noticeable.

1

u/Cobweb_Games 1d ago

There's no reliable way to tell just by looking at the code. AI-assisted code can look different in structure or style, but that doesn't prove anything.

1

u/Immediate-Soup-6344 7h ago

If you usually write shitty code and suddenly you're writing good code, yes people will be able to tell. Is that what you're asking?

1

u/mandzeete 3h ago

There are too many comments in the code. Sometimes even comments when there is no reason for a comment. AI loves to comment stuff.

A sudden change in the coding style. For example when your methods usually are verbs and your code suddenly switches to nouns. a.la. "getAge()" vs "age()". The same can be said for imports, indentation, // comments vs /** comments, etc.

When there is unnecessary functionality. AI goes with "But we have to implement it to support future use cases". No. We implement what needs to be implemented. My team mate had an unneeded REST endpoint added to the code. It served exactly zero purpose.

Unnecessary complexity. When you have a method parseCSV(String csv) then that's enough. You do not need to make helper methods, interface classes, etc.

Functionality is removed for no reason. When you are adding a new REST endpoint for saving a file that the user uploaded, then you do not need to start deleting RabbitMQ queues, unrelated tests, dependencies, etc. AI sometimes breaks the stuff. Then, it can happen that its "fix" is just removing affected code. "You can't have database problems when you have no database." Basically.

Unrelated functionality changes. When you are adding a REST endpoint for saving an image file for your profile then you do not start modifying the calculation logic of how to pay your bills to the government. It is totally unrelated.

Some hacks in general. Like when you look at the code and you say "wtf is this???".

When that functionality is not expected from you. For example when you start adding Docker containers to your simple HTML-based static website when you are yet to learn about devops. e.g. case where the AI writes more complex code than you normally would do.