Adventures in AI programming
Published -So I was talking to my Team Lead in work today, 'sup Carl, and I was telling him about a method I wrote for my XNA ToolKit probably 12 to 13 years a go now. I had just spotted that it was broken because I'm reviewing all the code so I can get back to game development. This bug took me far too long to spot my mistake and write a fix for it because I'm very slow of thinking.
But a thought occurred to me. Would AI be able to fix it better than I did on my own.
My prompt to DeepSeek
When I call the following function with an empty string I get a single "
back, how can I fix it?
private static string AssertStringQuotes(string value)
{
// make sure we have our surrounding quotations
if (!value.StartsWith("\""))
value = "\"" + value;
if (!value.EndsWith("\""))
value = value + "\"";
return value;
}
Response from DeepSeek
The issue is that when you pass an empty string ""
:
value.StartsWith("\"")
returnsfalse
(empty string doesn't start with quote)value.EndsWith("\"")
returnsfalse
(empty string doesn't end with quote)- So you add a quote at the beginning AND end, resulting in
"\"\""
which displays as""
But when you return this, it looks like a single "
because the two quotes together represent an empty quoted string.
The function is actually working correctly - an empty string surrounded by quotes should be ""
. What you're seeing is the proper representation of an empty quoted string.
However, if you want to handle the empty string case differently, you can add a special check:
private static string AssertStringQuotes(string value)
{
// Handle null or empty string
if (string.IsNullOrEmpty(value))
return "\"\"";
// make sure we have our surrounding quotations
if (!value.StartsWith("\""))
value = "\"" + value;
if (!value.EndsWith("\""))
value = value + "\"";
return value;
}
What's happening in your original code:
- Input:
""
(empty string) - Output:
"\""
+""
+"\""
="\"\""
- This is correctly displayed as
""
(an empty string within quotes)
If you're debugging and seeing a single quote, it might be how your debugger or output window is displaying it. The actual value contains two quote characters.
Have you ever see a moron get the right answer by accident? You have now.
In case you missed it, the original code returns a single "
when you give it an empty string because I checked the string didn't start with a "
which it doesn't, because it's an empty string. So I add a "
before the original value, an empty string.
value
is now a single "
so when I then check if this one character string ENDS with a "
it returns true so I doesn't add the second "
🤓
Honestly, I can dress myself.
The code the AI suggests is the correct fix. But all the other words are nonsense. It didn't even get the original logic flow right. It thinks the reason I see an error is only in debug. It failed to see that value
changed between the two checks.
Don't take the AI's word for it, and you won't need to Stay Lucky.
Post Script Update (2025-09-26)
So while I was in work today I used the AI system we use here to see if it would mess up as badly as the others did. See björn's post for OpenAI & Claude 4.
I used the Cody plugin in Rider which was set to use Claude 3.7 Sonnet. I was amazed that it did correctly identify the issue with the original code and give the now accepted correction. It also offered an alternative solution.
Cody (Claude 3.7 Sonnet)
Alternatively, if you want to keep the same structure but fix the logic issue:
private static string AssertStringQuotes(string value)
{
// make sure we have our surrounding quotations
if (!value.StartsWith("\""))
value = "\"" + value;
// check if the string ends with a quote, but makesure we're not just checking
// the quote we added at the beginning if the string was empty
if (value.Length <= 1 || !value.EndsWith("\""))
value = value + "\"";
return value;
}
🤯 Not only does this work but it's also an answer the question from Melissa
@mort8088 I am wondering what the expected output would be if you pass
"\""
to this method 🧐
If I was going to be picky <=
could just be ==
and it would still work because even if you had given an empty string the minimum length would be 1 at this point.
I still had to check the AI's output for correctness, and I got a spiffy new method to boot 🥳