string.format vs. string concatenation
Moderator: Forum Moderators
string.format vs. string concatenation
I'm very sorry to use this thread for my question, but as this topic arose here: What are the advantages / disadvantages of using8680 wrote:Finally, while you can usestring.format()
, you don't have to.
string.format()
?Or is it just a matter of taste?
Crend
UMC Story Images — Story images for your campaign!
-
- Inactive Developer
- Posts: 2461
- Joined: August 15th, 2008, 8:46 pm
- Location: Germany
Re: vultraz's lua questions
Silene said performance.Crendgrim wrote:[What are the advantages / disadvantages of usingstring.format()
?
Or is it just a matter of taste?
Afaik if you concatenate strings like this
local s3 = "s1" .. "s2"
you first construct (which needs some time and memory) each of them and then concatenate them, this is somehow optimized when using string.format
local s3 = string.format(%s%s, s1, s2)
Also, one can perform a precise check about the type and values of a variable (whether a positive integer actually is for instance).
projects (BfW 1.12):
A Simple Campaign: campaign draft for wml starters • Plan Your Advancements: mp mod
The Earth's Gut: sp campaign • Settlers of Wesnoth: mp scenario • Wesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
A Simple Campaign: campaign draft for wml starters • Plan Your Advancements: mp mod
The Earth's Gut: sp campaign • Settlers of Wesnoth: mp scenario • Wesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
Re: vultraz's lua questions
On my computer at least, concatenations are faster when you're only doing a few of them, but
string.format()
is faster when you have many variables to interpolate. But, as with most such things, you'd have to be doing billions of interpolations like I was to notice it. The main difference is that string.format()
can format ( ) the variables being interpolated.Re: vultraz's lua questions
I've had some issues with string formatting and userdata (like translated strings) in the past. I do generally prefer string.format, though, because it's easier to read.
Re: vultraz's lua questions
Trymelinath wrote:I've had some issues with string formatting and userdata (like translated strings) in the past.
tostring(...):format(...)
.
Even with syntax highlighting?melinath wrote:I do generally prefer string.format, though, because it's easier to read.
Re: vultraz's lua questions
Yeah, that's what I ended up doing. This was a while ago. Although I'm pretty sure you mean8680 wrote:Trytostring(...):format(...)
.
string.format(<format_string>, tostring(<userdata_string>), <normal_string>, ...)
Compare:8680 wrote:Even with syntax highlighting?
string.format("%s.%s", namespace, key)
namespace .. "." .. key
Looking at the first, it's easy to tell immediately that I have two strings separated by a period. It's not that hard to understand the second, either, but it does take just a split second longer (at least for me) - and this is a simple example. Something more complex might be:
string.format("%s pokes %s in the belly %d times, then %s away on a %s.", person1, person2, number_of_times, verb, steed)
person1 .. " pokes " .. person2 .. " in the belly " .. number_of_times .. " times, then " .. verb .. " away on a " .. steed
Here, with the string.format version, the number of variables being replaced is large enough that there is some cognitive load if you need to match variables with their place in the sentence - however, it's extremely easy to parse what the sentence is going to say.
The concatenation version, otoh, clearly shows where each variable goes in the sentence, but it's practically illegible - and it was a pain to type. I kept forgetting spaces at the beginning of the hard-coded string segments. Syntax highlighting helps only marginally.
Granted, this is based on an experimental pool consisting only of me.
Edit:: Just ran across a case where I generally use concatenation: incrementally building strings. For example, building a message based on a lua table. I usually loop through the table and add a new bit to the string each time. Suppose I could use string.format here as well. Maybe I should, if it's faster.
Re: vultraz's lua questions
Ah. I assumed themelinath wrote:Although I'm pretty sure you meanstring.format(<format_string>, tostring(<userdata_string>), <normal_string>, ...)
t_string
was the formatstring
. I'm not sure why now.
It's not like my sample size is any better. Anyway, I didn't intend to discuss a subjective topic like legibility, and furthermore this off-topic conversation about concatenation versus formatting has been too long ongoing.melinath wrote:Granted, this is based on an experimental pool consisting only of me.