Skip to main content
View on YouTube to comment and like!

Syntax Video

Stop Using AI For This

Chapter 1 0:00

intro

I've been thinking lately, what if it were possible to tell a computer exactly what to do? Like, what if we could write out instructions and then the computer would perform those exact instructions every time? It's programming. [laughter] It already exists. Um, and recently I've come across quite a few things where people are using AI to do things where a more deterministic approach would be a lot better. So, I'm going to go over several examples that I've come across and give you an alternative so you don't have to depend on AI as much. And I'd love to know in the comments, what are some examples of things you've come across of maybe co-workers or yourself where you're depending a little bit too much on AI when you probably could have just written code instead. Now, you might think that I hate AI based on recent evidence, but that is not the case. I admit that AI has its use cases, but there are certain scenarios where you probably shouldn't use it and you should just stick to the way we've been doing things for a very long time. And really, all of this is ultimately going to save you time, save you money, make

you more productive, and really just introduce some reliability [music] and consistency in your workflows and in the code that you write. Cuz at the end of the day, that's really all we want, right? So, if that sounds good, let's dive in. My name is CJ. Welcome to Syntax. [music] So, part of this was inspired by a Reddit post I came across a few months back, as well as some various comments I've been collecting from people and just things that I've thought about where I was guilty of using AI when I probably should have written code. And so, this post says, "We've been burning money for 6 months." Because I'm such a hard worker, I spent my weekend going through our open eye usage and we're at $1,200 a month. I honestly thought was that was just the cost of doing business. Then, I actually looked at what we're using GPT4 for and it's seriously a waste of money. Extracting phone numbers from emails, checking if text contains profanity, reformatting JSON, and literally just uppercasing text in one function. I ended up just moving all the dumb stuff to GPT40 mini.

Chapter 2 1:15

$1200 OpenAI Bill

Same exact outputs. Bill dropped to $200. Am I an idiot? How much are you guys spending? This post is so funny because you think you think it's going down the right path. You think that, oh, they realized they should just write code for this stuff, but no, they just switched models, right? they were using a more expensive model, then they switched to a less expensive model [laughter] that can do the same things. But no, they they missed they missed the point. You could actually write code for all this stuff.

So, let's start with this first one. Extracting phone numbers from emails. Now, a deterministic approach to extracting phone numbers from emails would be to use something like a regular expression. Now, you shouldn't always reach for regular expressions. Um, and sometimes you can potentially write regular expressions that take a really long time to run or could introduce a denial of service vulnerability where it takes forever to run and freezes up your program. But for simple cases, you could reach for a regular expression. And I have an example here that I have coded up which has several emails. So each one of these emails is reaching out to our team. And then there's some phone numbers in the signature. And then in this last example, there's also a phone number in the body of text itself. And you can see they're using various formats. So like it's separated by dashes, somes are separated by dots. Uh some are dots and dashes. Some have parentheses and spaces. So this is a scenario where people write phone numbers in a lot of different ways. The other thing to consider are international phone numbers. So right now we're just kind of assuming North American phone numbers. But all of that

Chapter 3 2:24

use existing solutions

to say is you can write a regular expression that looks for sequences of numbers in text and it will run almost immediately. So if we run my program, it finishes immediately. It's able to look through the text of each one of those emails and extract out the phone number immediately and it doesn't require an internet connection cuz I'm not talking to an LLM. Now, this regular expression is a monstrosity. I don't think I would put this in my codebase. I was just giving an example of it. Um, and you nowadays don't even have to know how to write regular expressions. So, I actually have a series of prompts here with Gemini where I was asking it was like, "Hey, I need a regular expression in JavaScript that extracts phone numbers." And I was like, "Oh yeah, here you go." And I was like, "Okay, what about international phone numbers?" And it was like, "Oh, yeah, sure. I have the regular expression for you." [laughter] And it gives you an description of why it works and how. And then it was like, "Hey, maybe you should look at and evaluate that regular expression for a redoss attack." And he's like, "Oh, you're absolutely right." And then it had a syntax error. And finally, it came back with this regular expression. So

that's fine. This is a good use case of AI because if you read these responses in detail, you'll learn a little bit about regular expressions and how to construct them yourself. So that's that's one way to go about this. Use AI to help you solve the problem. Come up with a more deterministic piece of code that you don't need a network access for, you don't need an LLM for, and it will just work every single time. Now, like I said, you shouldn't always write a regular expression for everything. You could also search for an existing solution, and this is a very solved problem. extracting phone numbers from bits of text. So, this is a library I came across on npm. It's called lib phone number-js. And this actually comes from Google. It's a port of the code that is running on every single Android device that's able to extract out phone numbers from text. And again, this comes with caveats. You shouldn't just go off and npm install anything. But this is a very tried and tested and used library.

It has 2,000 dependents and is downloaded over 9 million times weekly. So these are just a few indicators you can look at to be like is this code safe? Does it do the right thing? And it's it's battle tested. So if I were going to implement this in my app, I probably wouldn't use a regular expression. I would reach for this library. And it has a built-in function called find note phone numbers in text.

And it does the hard work of figuring out well the country code or just like assuming the country code of one if there is no country code and it parses it out in a nice format. And this is all just code you install. So once you've installed this MPM module, you don't need internet access anymore to actually extract out phone numbers. So this is another way to solve that exact same problem. And I I'm probably going to keep harping on this in the whole video, but the thing to note here is this code is running on my machine and it's instant, right? It doesn't take any amount of time. We don't have to connect over the network. We don't have to wait for an LLM to analyze the the text and then come back with with a list of phone numbers. It's completely local, will run anywhere. It's portable. This is this is a solved problem is really all I'm trying to say. So when you come across a problem like this where you're potentially prompting an AI, right? So instead of a regular expression or a call to a library, you might literally have a call to open AI to GBT5 saying extract the uh phone numbers from these emails uh but instead reach for some

more deterministic code. Now this next example of checking if text contains profanity, similar situation. There are existing libraries and dictionaries that can do this. Now, one of the libraries I've used in my code before is a library called bad words. And one of the things about making sure text doesn't contain profanity is people try to be very clever about like replacing letters with numbers or adding extra characters so it doesn't get detected. So, you can see in this example certain types of attempts to do this are still blocked by this bad word filter. And this is also using a dictionary based approach. So, there are existing dictionaries out there of like every bad word ever. [laughter] Like, don't don't tell 10-year-old me because I'd be super excited to just look through all the bad words. U but there there are dictionaries that people have come up with and you can pair this with regular expressions. Again, it's a solved problem. So, similar idea, install this library or come up with your own dictionary and that code is going to run instantly. You don't have

to send it off to an LLM. This next one, again, reach for a library or even just use JavaScript or any language itself. A lot of languages have formatting of JSON just built in. Um, so you could pass a bunch of JSON to an AI and expect that it's going to spit it all back in the same way formatted, but it could hallucinate. It could change some of those keys and values. Just use a library for this. So you may not know this, but you can actually use prettier programmatically. You can install this, call it directly from JavaScript and pass it some JSON data, or even better, use JavaScript itself. So prettier is nice because you can pass in a lot of options, change the type of formatting, but let me show you an example of this.

So I have this JSON data here. It's actually the uh data from Reddit that represents that post that we're looking at. It's in a JSON file. Uh but what we can do is read it in use JSON.parse and JSON.stringify to actually format it. So here I'm using FS. Technically, this is modern JavaScript. we could import the JSON file directly in, but that defeats the purpose because then we could just immediately stringify it. So in this case, we're dealing with just a string directly. So if I run this code and I log it, you can see there's no formatting. It's just the text itself.

Uh even better, we could use a command line tool. I mean, maybe this is running in a serverside process, but there's this tool called jq that you can use on the command line and it'll it'll format your JSON for you. Regardless, we can use JavaScript as well. Uh so if we literally just do JSON.parse parse for that JSON data. That's going to turn it into an actual JSON object. And then if we want to turn it back into a string, we can use JSON.stringify. We'll pass in null for the replacer function. And then you can specify how many spaces you want in the formatting. So in this case, if I pass in two, it's going to do a twospace format. And now when I run my codes, let's do it without jQ. It's going to format it right there built into JavaScript. So I didn't need to send this data off to an AI to format it.

This is a solved problem. It's built into most programming languages. Um, just just just use what's built in. Okay, this last one has to be a joke. Uppercasing text in a function because you may not realize it, but look at this. There's a two uppercase function built into JavaScript. Can you believe that there are devs out there that are paying Open AI to uppercase their text for them? It It seems like a joke, right? I don't think it's a joke. Based on the responses in this thread, I don't think it's a joke. people are really doing this. Uh but yeah, you could literally just call the two uppercase function. Um I mean if you want to be weird about it, you could create like a serverless function and then you have a micros service that could uppercase your data. That's going to be even faster than trying to send it off to OpenAI.

Yeah. I think the the main advice for all of this stuff is reaching for existing solutions first instead of passing it off to an AI. The other idea here is it's possible that your prompts to the AI for these things could be more specific. Like for phone numbers, you could say only give me the phone numbers that seem important or something like that. But you could do some pre-processing, right? Like you could first determine are there any phone numbers in the email using some sort of regular expression and then if they are, then send that off to an AI to analyze it instead of sending off every single email. Yeah. Same thing for checking for profanity. Maybe there are specific types of profanity that you've been coming across and you can like pre-prompt the AI. I don't know. All of these things are solved deterministic problems and you can do for free. You don't need you don't need to ask OpenAI to do it for you. Okay. Everything I've shown so far is code that we're writing, right? But this is potentially running in some server side code or maybe it's running on the client. Calling OpenAI from the client would be crazy as well.

But regardless, everything I've showed so far is code that we're actually writing. The next thing I want to talk about is our workflow or or how we're actually using AI when we're writing our own code. And the first example I have for you is converting JSON into types. So let's say we have this JSON data that we got back from an API, but we want to write some type- safe code against it.

Chapter 4 11:18

convert JSON to types

We can generate types for it. Now, I've seen a lot of people just plug this JSON code into an AI, maybe it's a chat GBT window, or maybe it's a cursor chat window, and ask it to generate TypeScript types for you. But this is also a deterministic solved problem. So there is a VS code extension I've been using for a very long time called paste JSON as types. There are probably other extensions out there that are similar.

There probably other websites you can go to that do this. But the conversion of JSON data to a type is a deterministic process and it can be done without any hallucinations or without any loss. Right? If you paste all of this JSON code into an AI, it's possible that it might hallucinate some properties or it might forget some data. If you use an extension like this, it's going to look at all the data and deterministically parse the data to generate a type. So here I can have some JSON data on my clipboard. Run this command. We're going to call this the Reddit response. Hit enter. And when you look at that, we now have a bunch of interfaces that represent this data. So the Reddit response has two properties, kind and data. And then each one of those has several properties. and it'll come up with interesting names if it can't figure out what they are exactly. Uh but here we have an actual type that represents this JSON. And so in the case of parsing it, I can actually just throw it as a Reddit response. And now if I do object dot, look at that. I've got access to data in kind. Now again, I'm disregarding the fact that if we just

imported this JSON file, the our editor would automatically infer the type. This is for different kinds of scenarios where you're potentially getting this JSON data back from an API. But look at that. We've got access to data. we can start to drill in. It's great and I didn't need an AI to do that. I can just use this extension. It's free. It runs locally. I don't need an internet connection. Use this extension if you need to convert your data into types.

Chapter 5 13:14

use IDE refactoring

Another thing I have seen people do is use AI to refactor their code like rename a variable or move things into a file. First of renaming variables, if you didn't know this, inside of VS Code, you can press F2 on any identifier. So, a function name or a variable name. I I feel like we're in beginner class at this point, but people should know about this if you don't. And if you're in a scenario like in Typescript where it knows where all of these variables are accessed, you can just give it a new name. So, uh we'll just give it an abbreviation of EPN. And look at that.

Every instance that was referencing that variable automatically has replaced uh the the reference name to it. I feel like I might be blowing some people's minds here and other people will like, yeah, of course. But seriously, people will prompt AI to say like, "Rename this variable for me." You don't need that. We can just do this instantly, rename across our codebase everything that has access to it. Um, and this is what you need. Same thing for extracting things out into a file. Like technically, you could ask AI, hey, let's move that extract phone numbers function into the utils folder. It's not it's not that big a deal to just cut it, paste it, throw the export keyword on it, and then import it in. Right. Um, now you might say, I don't know. I think we're potentially talking to two different camps of people at this point because you might be like, I'm too lazy to do that, but um that was free. I didn't have to pay OpenAI or Anthropic to move that function into a file for me. So little refactors like this, renaming of variables, your editor already supports all this stuff. Actually, like that

that's the other thing is there are refactoring uh things built in. You don't really even have to do it manually. So, if you're using a modern editor like VS Code, there actually is a menu here called refactor. And there's a bunch of different options like move to a new file. You don't have to do it manually. Look, it moved it. It put the expert keyboard. It automatically uh through the import for me. So, use the editor. Just do it manually. Stop asking AI to refactor your code for these simple type of renames and move to a file type of refactors. Okay, this next example, you could take it or leave it, but in my experience working with AI, it feels like wasted cycles if the AI is generating all of the boilerplate code for a project when an existing CLI exists for generating projects. So, for example, create next app or create vit app or create hono app. These are CLI tools that will give you a base project and you can start going from there. But a lot of times when I was, you know, figuring out how to prompt with AI, I'd ask it to generate a project and it would just start generating these files

Chapter 6 15:13

use CLI generators

from scratch. And again, this is a deterministic process. We already have CLI tools that can generate these projects. So for instance, if I do pnpm create vitra I want. Other options are provided. And especially with VIT, they also throw you off to other uh CLI tools like if you want to create something with React Router or Tanstack Router or the Redwood SDK or uh Vit RSC, you can choose those.

And again, those are CLI tools that are going to give you a a base project that's set up and ready to go with the latest versions of all of your dependencies. This is another thing that AI gets wrong a lot. It will create a package.json with dependencies that are 2, 3, 4 months old. But if you use a CLI tool, you're going to get the latest and greatest stuff and it's going to be ready to go. Look at that. I've got myself a React app running on 5173 and uh it was instant. I didn't have to pay open AAI to do it for me. I didn't even need to be connected to the internet except for the npm install. Uh but then uh look at this. We have our base setup.

It's got a decent ts config, a v config. We have our base react components. This thing is ready to go. And I didn't have to sit here and wait for AI to generate each and every one of these files. So that's my ne next piece of adi advice is when you're starting a project use vit use create next app get the base files ready to go from the source and then start prompting with AI to add new stuff and improve it. Okay, this last one I have is for potentially linting code or you could maybe even people have done this for formatting code like we talked about earlier, but I posted about this on blue sky and X and Dan responded with this is in the official Claude code docs lint colon claude and it's a prompt that directly calls Claude to tell it to find any typos in your code. Report them back one line at a time and uh do not return any other text. And it's legit. I found it in their documentation. Um, they give you an example of using cloud as a Unix style utility. So, you're a llinter.

Chapter 7 17:02

use deterministic linters

Please look at the changes versus main and report any issues related to typos. But again, this is a solve problem, a deterministic solve problem that code is really good at, right? There are dictionaries that exist out there. We can analyze every word in your code and look it up in a dictionary and see if it exists there or if it's like a common thing used in programming. Um, so if you're using a llinter tool, there's probably a plugin for it. Also, there's a tool called C spell and there's a plug-in for VS Code that will find uh typos and you can create a dictionary, especially like if in your if you're in a certain domain and there are words that appear a lot in your code, you can add them to your local dictionary file.

This library here, Cpel, has over a million weekly downloads. So, a lot of code [clears throat] is already using it. Um, so this is another example of just use the existing thing. We don't need AI to to typo check our code. There are existing tools for that. Yeah, and like Dan is saying here, um, having AI as a generalized llinter, so like you could prompt it to look for security vulnerabilities or given my current code changes, tell me if there are better ways to refactor the code or if I have any duplication that might be better refactored. Like I I could see myself using that as like a a post code is done linting type of tool, but not for deterministic things like spellchecking.

Chapter 8 18:53

viewer comments

Now, before I leave you, I'm going to take a look at the responses to the post that I made because there's some other real world examples of people using AI when they probably shouldn't have. So, uh, Giani 1986 says, "My former company subscribed to an online AI docx to PDF converter instead of using the free PDF download feature in Google Docs because they were AI native." Again, this is another solved problem. Um the I'm sure you could find something on npm for converting docx to PDF or there's probably some existing CLI tool that you could just call from node or whatever your language you're using and you don't need AI to do that. Yeah. Charles says one of the first things I did was ask AI to do a slightly more complicated transform of variable names from kebab case to camo case for thousands of lines. Again, I think there are llinter rules and plugins you could add that actually automatically do this conversion. I don't think there's anything made by default, but you could write a llinter plugin that converts from kebab to camelc case, etc. Yeah, and Isaac is saying, I just had an LLM generated project that had 890 lint

errors that could not be autofixed. Tried to have the LLM fix it, but alas, I had to fix them myself one by one. Every single was from interfaces that had generated and 50% of those were useless, so they got deleted. Yeah, this isn't an example of like using AI when you should have written code. It's just an example of like depending on AI too much and then you have to come in and clean up the work afterwards. Yeah. And over on X, developer Zeke is saying, "I'm guilty of getting chat GB to write types from JSON. Can't be bothered to find a web app or extension. I showed you the extension that you could be using." Um, plus it breaks deeply nested JSON down into separate types. This extension I showed also does that. Yeah, Drew is saying, "I use the VS Code extension past JSON as types." Exactly.

And, uh, Robert from Code Web Chat said, "Rename this variable from X to Y. I'm quite often renaming CSS classes written in BIM. Asking AI is the only option." That my friend I think is a good use case of AI because it's non-deterministic right naming of variables and CSS class names um developers come up with that. There's not like a deterministic approach for like renaming them or coming up with better ways to name them. So I think that's a pretty good use case. It's not as deterministic. Um so yeah, I I think that's actually a good use case. Okay, we ended on a bit of a high note because like I mentioned, there are some good use cases for AI. But at the end of the day, before you reach for AI, really all I'm saying is check to see is there exist an existing deterministic solution for the thing you're trying to do or could you write some code to do it? Or could you ask AI to write the code to do it? I don't know. That's all I'm saying.

Quote it with credit and a link. Quoting Syntax