How can I call a method of an object by using a va...
# cfml-general
d
How can I call a method of an object by using a variable whose value string is the name of the method?
Copy code
for(m in lineItems.metrickeys){
    lineitems[m.key] = {
        code: m.key,
        name: m.name,
        header: true,
        hidden: false,
        pptd: Metrics[m.key]("pptd",lineItems); // <--------- WHAT I WANT!!!!!
        cptd: Metrics.netrevenue("cptd",lineItems); <----- an example method 'netrevenue' that exist in the Metrics object
        budgetptd: Metrics.netrevenue("budgetptd",lineItems);
        pychangeptd: Metrics.netrevenue("pychangeptd",lineItems);
        pypercentptd: Metrics.netrevenue("pypercentptd",lineItems);
        budgetchangeptd: Metrics.netrevenue("budgetchangeptd",lineItems);
        budgetpercentptd: Metrics.netrevenue("budgetpercentptd",lineItems);
        pytd: Metrics.netrevenue("pytd",lineItems);
        cytd: Metrics.netrevenue("cytd",lineItems);
        budgetytd: Metrics.netrevenue("budgetytd",lineItems);
        pychangeytd: Metrics.netrevenue("pychangeytd",lineItems);
        pypercentytd: Metrics.netrevenue("pypercentytd",lineItems);
        budgetchangeytd: Metrics.netrevenue("budgetchangeytd",lineItems);
        budgetpercentytd: Metrics.netrevenue("budgetpercentytd",lineItems);
    }
}
I have about 20 lineitems each with 14 formulas to calculate. So if I can dynamically call a method by using a variable name I could save 19*14 lines of code.
s
In Lucee:
object[ '#someVariableThatIsAMethod#' ]( arguments )
In ACF,
invoke()
(which also works in Lucee, but I think is slower)
d
i need it ACF. never heard of invoke() thanks I'll try it out.
a
In CF2021 the bracket notation finally works too.
1
🙌🏾 1
z
looking over the Lucee source, it's pretty much the same code path for both approaches
1
a
If there was real world meaningful performance difference between both of those, something would be rather wrong with the implementation.
z
for those interested, i just ran this http://java-decompiler.github.io/ on the generated file in cfclasses
2
a
That has been a bloody handy tool to me in the past.
z
i love it when people ask questions about myths from the 90s and early 00s about cfml performance of different syntaxes
a
Ooh, did I hear someone say "`evaluate`"? 😄
👍🏾 1
Devs do love their cargo culting
(I am not suggesting I am any different, btw 😉
z
things like don't use the request scope coz it's slow
g
Although it seems like we have hijacked @Daniel Mejia,thread.... Any truth (still) that arrays are faster than lists? (Yes I know I could write a test myself.... but someone mmay just definitively know, already)
z
obviously, what's your mental model for how they work?
g
@zackster Not sure if you're replying to me or not.... But just in case it is me.... My understanding is because of the index attached to arrays operations upon their content members is faster, when compared to the same task applied to a list.
a
I'd say "don't use the request scope because in well-written application it's hardly ever the right tool for the job".
As for lists, I think the issue is not performance, it's "a list is not a collection type, it's a string with some commas in it. Don't use it as a collection type. Use an array"
👍🏼 1
👍 1
👍🏾 1
But also the dumbarsey that the CF engine will need to go through under the hood to treat a string as a collection type will be rubbish compared to actually using an appropriate tool for the job.
👍🏼 1
z
lists will always be slower as the engine needs to go thru and split to string into items, where as a list is already split
hence, my try and model what the server is doing
s
i love it when people ask questions about myths from the 90s and early 00s about cfml performance of different syntaxes
The last version of ACF we used was 11, and at that time,
invoke()
was expensive while Lucee's syntax was less so. I ought to have mentioned. As a general disclaimer, anything I say about Adobe Coldfusion pre-dates 2017
❤️ 2
ACF syntax has come a long way since then for sure
2
d
last night a production issue side tracked me so I didn't even finish my task with this and believe me I would have written 266 lines of code to finish the job. Now I'm super stoked about writing 19 lines of code. Thank you fellas! what a great morning.
😇 1
1
m
Little known fact, under the hood in ACF, lists are mapped to a grouping of tiny penguins with little number stickers on their heads. Arrays are mapped to sparrows. Hence the performance difference between lists and arrays. Stay tuned next time for more definitely true facts about ColdFusion!
😂 2
🤣 1
s
no price is too high for tiny penguin list management
2
😀 1
m
I love using penguin of penguin data structures.
👍🏼 1
z
structs are ducks
👍🏼 1
a
😀 1
z
they do love just shitting everywhere and everywhere don't they?
😂 1
a
I was rather taken aback when I saw one actually doing it
m
memory leak
😂 3