string.format vs. string concatenation

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Post Reply
User avatar
Crendgrim
Moderator Emeritus
Posts: 1328
Joined: October 15th, 2010, 10:39 am
Location: Germany

string.format vs. string concatenation

Post by Crendgrim »

8680 wrote:Finally, while you can use string.format(), you don't have to.
I'm very sorry to use this thread for my question, but as this topic arose here: What are the advantages / disadvantages of using string.format()?
Or is it just a matter of taste?


Crend
UMC Story Images — Story images for your campaign!
Anonymissimus
Inactive Developer
Posts: 2461
Joined: August 15th, 2008, 8:46 pm
Location: Germany

Re: vultraz's lua questions

Post by Anonymissimus »

Crendgrim wrote:[What are the advantages / disadvantages of using string.format()?
Or is it just a matter of taste?
Silene said performance.
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 startersPlan Your Advancements: mp mod
The Earth's Gut: sp campaignSettlers of Wesnoth: mp scenarioWesnoth Lua Pack: lua tags and utils
updated to 1.8 and handed over: A Gryphon's Tale: sp campaign
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: vultraz's lua questions

Post by 8680 »

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 ( :whistle: ) the variables being interpolated.
User avatar
melinath
Posts: 1298
Joined: May 20th, 2009, 7:42 am

Re: vultraz's lua questions

Post by melinath »

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.
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: vultraz's lua questions

Post by 8680 »

melinath wrote:I've had some issues with string formatting and userdata (like translated strings) in the past.
Try tostring(...):format(...).
melinath wrote:I do generally prefer string.format, though, because it's easier to read.
Even with syntax highlighting?
User avatar
melinath
Posts: 1298
Joined: May 20th, 2009, 7:42 am

Re: vultraz's lua questions

Post by melinath »

8680 wrote:Try tostring(...):format(...).
Yeah, that's what I ended up doing. This was a while ago. Although I'm pretty sure you mean string.format(<format_string>, tostring(<userdata_string>), <normal_string>, ...)
8680 wrote:Even with syntax highlighting?
Compare:

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.
User avatar
8680
Moderator Emeritus
Posts: 742
Joined: March 20th, 2011, 11:45 pm
Location: The past

Re: vultraz's lua questions

Post by 8680 »

melinath wrote:Although I'm pretty sure you mean string.format(<format_string>, tostring(<userdata_string>), <normal_string>, ...)
Ah. I assumed the t_string was the formatstring. I'm not sure why now.
melinath wrote:Granted, this is based on an experimental pool consisting only of me.
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.
Post Reply