Think about how much time you can save in a situation where you need to change the text in your global variable. You will do it in one move.
Or you will have to change ALL the places where you use the variable value.
In my opinion, from a readability standpoint, using global/static variables also adds clarity. Using them, you are LITERALLY using the same thing.
For example, Epic uses Static Const FName to denote the pin type. These haven’t changed since at least 4.27, but using them lets you know that the pin type is LITERALLY this one, and not another with the same value. It definitely adds to readability.
Yeah I don’t agree. It adds a LOT of code with these constant namespace blocks that tend to become huge and you need to go back and forth between the code you’re reading and these separate declarations to know what value the string variables actually hide.
What is the event namespace value? Is it pascal case, is it camel case?
Every line like this, you need to go back to the top of the file to get your answers. That is not what I call readable. You also add 3 lines instead of 1 for every one of these variables.
Now I agree that if a given variable is used in a lot of places, you should try to centralize it. But I don’t agree in the case where you have several variables that are only used once.
EventName is a local method variable here and GameEventNamespace is a global constant. You need more context to understand these lines of code, it is the opposite of readable.
My consideration is not about readability, it’s more about performance here. It would seem like using an const FString variable would be more effective because it avoids creating FString variables over and over.
But then I checked Epic’s code and I could not find this done anywhere. If it’s ever used, I would bet it’s for centralization purposes, not performance. And in the case of a widely used variable I fully agree that it should be make more globally available.
But it seems very inefficient to allocate a new FString every time, is it not a consideration? If yes, why would Epic ignore this completely? I do not believe their devs are incompetent.
How often is this code executed ? I don’t think performance is such a problem here (as far as usage of FString is concerned) unless you notice frame drops while executing this. I would just go with the readability over trying to squeeze as much performance as you can.
In shipping build the compiler will optimize those strings.
We have different configurations like Debug, Development and shipping, particularly in shipping the compiler can make some adjustment to optimize your code depending on the platform, these optimizations can minimize execution time, memory usage etc.
Sometimes the compiler can decide to bake certain strings in the binary as an optimization.