Hello community
It’s me back with another tutorial for the developer community.
This Topic is specially made for new or intermediate scripters. And remember that if you use or used to use magic-strings, it doesn’t make you a bad programmer, you just didn’t know how to avoid them and didn’t know that they are a bad idea!
NOTE: By Enum or constant, I mean the same thing, by Enum I mean the same thing with a constant but with a different naming convention than constants (enums are normally used in modulescripts).
What are magic strings?
In programming, a “magic-string” is a term for those text strings that are put directly into code, for example: "i'm a string!"
. These strings have a special use in the code and are used to control the behavior of the program.
But what are the problems with magic-strings?
There are several problems with magic-string, one of them is that if you have written the same string many times, and you need to change it for some reason or another, you will need to change each string by hand and it is too tedious if it is a lot of code.
- “but I can use find & replace”
That’s a good idea, but: what if the string is “local” (for example)?
This task can be very tedious if you are using a string that matches for example a LUAU keyword or the name of a function in your script. It can also make your code unscalable and unreadable, since you don’t know where that string comes from or what it means (hence the term “magic-string”, since you don’t know where it came from or what it is used for).
How can I fix this?
One way to fix this is to create your own constants or enums which contain the text string you want to use in your code, for example:
local DATABASE_PASSWORD = "ilovepizza1234" -- Our constant
local password = "wrong password" --[[ thinking "wrong password" as a dynamic value given by the user ]]
if password == DATABASE_PASSWORD then
print("You can access the database!")
else
warn("Nuh uh")
end
The reason for using constants/enums is to have a single “SOURCE OF TRUTH” for strings that we use throughout the code. This also helps us that in case we want to change the text string, we only need to do it in one place and it will be changed in all the code without having to do a lot of work.
What other use is there for constants/enums?
A very useful use, is to be able to use the constants/enums outside the original code to, for example, pass them as parameters to functions or to use them in different scripts, for example, let’s say we have a modulescript in ReplicatedStorage called “DatabaseEnums” which contains constants (or enums, whatever you want to call them) that are used inside another script.
-- DatabaseEnums.lua
local DatabaseEnums = {}
DatabaseEnums.DatabaseKey = "random key"
DatabaseEnums.DatabasePassword = "bla bla bla"
return DatabaseEnums
-- Database.lua
local Enums = require(DatabaseEnums-path)
local function LogInDatabase(password: string)
if password == Enums.DatabasePassword then
print("Logged in!")
else
warn("nuh uh")
end
end
LogInDatabase("hi") -- nuh uh
This is very useful in case we have many strings that we use in our script to change its flow or behavior, the code becomes more understandable and you avoid having the problems previously mentioned.
Did you use or know about magic-strings?
- Yes
- No
0 voters
If you didn’t understand something, you can ask me for help in the comments and I’ll help you!