websolete
09/04/2022, 5:22 PMAdam Cameron
this
scope.websolete
09/04/2022, 5:55 PMAdam Cameron
Adam Cameron
obj.property
rather than explicitly obj.getProperty
/ obj.setProperty
has a fair bit of industry traction, by way of cutting down on boilerplate.Adam Cameron
invokeImplicitAccessors
, one gets the brevity of accessing the properties directly, and the flexibility of still controlling what access one has when that becomes a consideration.websolete
09/04/2022, 6:23 PMTHIS
and allow you to therefore chain set() methods for example. it may just be how you've written your article examples but they all return void, thereby disallowing chaining, even if the syntax you're using to assign values isn't the only way. anyway, i guess i'm just missing where the boilerplate omissions gain you anything other than modelling some cf behavior to be more aligned with other languageswebsolete
09/04/2022, 6:24 PMAdam Cameron
websolete
09/04/2022, 6:25 PMwebsolete
09/04/2022, 6:25 PMAdam Cameron
arguably the least amount of overall code is what you get using accessors=trueIt's the opposite. Every time you want to access the property you have to go
myObject.getTheProperty
. That's what all this is trying to avoidwebsolete
09/04/2022, 6:27 PMperson = new Person()
person.firstName = "Annie"
person.middleName = "Jean"
becomes this
person.firstName("Annie").middleName("Jean")
?
websolete
09/04/2022, 6:28 PMAdam Cameron
websolete
09/04/2022, 6:28 PMwebsolete
09/04/2022, 6:28 PMAdam Cameron
websolete
09/04/2022, 6:29 PMAdam Cameron
person.setFirstName("Annie").setMiddleName("Jean")
I thinkwebsolete
09/04/2022, 6:29 PMwebsolete
09/04/2022, 6:29 PMwebsolete
09/04/2022, 6:39 PMperson.setFirstname( "Annie" )
as person.firstName = "Annie"
and person.getFistname()
as person.firstname
then fine, i have no objections i could mountwebsolete
09/04/2022, 6:41 PMAdam Cameron
string firstName {
get() {
return firstName
}
set(value){
firstName = value
}
}
(both are optional)
Here the behaviour for writing and reading to the property is self contained in the property definition. THIS is actually the important part of the functionality. CFML does not do a very good job of this, as I mentioned in the article: the getFirstName
and setFirstName
methods are "separate" from the property definition due to poor / absent design.
What this means for the calling code is one doesn't have to faff around explicitly calling methods on the object, one just focuses on saying to the object "here have this" and "can I have this please?"
This is all just nuance though, sure.
---
Also one calls the code far more often than one writes it, so there definitely is also a perceived benefit in the calling code being able to just go myObj.myProperty
to read it, and myObj.myProperty = "new value"
to write it. Instead of myObj.getMyProperty
and myObj.setMyProperty("new value")
. Admittedly there is not much to it.
Also at the end of the day, it's the property value yer interested in, so just... deal with the property value. Having explicit accessor methods is only a thing in the first place because or incomplete language design in CFML (properties came in in CFMX6, only got their implicit accessors in CF2016!).
I guess the whole thing is one simply doesn't need to explicitly call the accessor methods any more. So why would one?
---
I think there's perhaps little gain when already being in CFML before knowing about the functionality, and going "why should I change?", compared to coming from other languages where it's already a thing, and the way things are done.Adam Cameron
Adam Cameron
Adam Cameron
Adam Cameron
websolete
09/04/2022, 6:56 PMAdam Cameron
websolete
09/04/2022, 6:57 PMAdam Cameron
Adam Cameron
websolete
09/04/2022, 6:58 PMwebsolete
09/04/2022, 6:58 PMAdam Cameron
string firstName {
get() {
return firstName
}
set(value){
firstName = value
}
}
And in 2022 someone from PHP / JS land came along and went how do I call myObj.getFirstName()
we'd be going "oh... how quaint..."websolete
09/04/2022, 7:00 PMAdam Cameron
I noticed this yesterday when I was writing my "Kotlin: the next morning learning Kotlin stuff" article. I was looking into how Kotlin handles accessor methods on properties, and remembered CFML had a similar bit of functionality, and quickly revisited it by way of comparisonIt's a feature that has zero documentation. So I wrote some.
Adam Cameron
Adam Cameron
websolete
09/04/2022, 7:01 PMAdam Cameron
websolete
09/04/2022, 7:02 PMAdam Cameron
Adam Cameron
websolete
09/04/2022, 7:03 PMwebsolete
09/04/2022, 7:03 PMwebsolete
09/04/2022, 7:03 PMAdam Cameron
websolete
09/04/2022, 7:05 PMAdam Cameron
gavinbaumanis
09/05/2022, 6:20 AMleftbower
09/05/2022, 10:29 PMlastName
upper-cased (contrived, ya...) then you could just override the implicit accessor with a hard coded one that UCASE
the returned value and therefore not change any calling code.
But if some/all of the calling code were using implicitAccessors then you'd have to make a number of changes? Or is there a way to work around that with implicitAccessors?gavinbaumanis
09/06/2022, 12:25 AMAdam Cameron
myObj.someProperty = "value"; writeOutput(myObj.someProperty);
If later you need to apply some behaviour to either access or change, then you add the relevant method. But the calling code remains myObj.someProperty = "value"; writeOutput(myObj.someProperty);
One doesn't start by creating boilerplate code (myObj.setSomeProperty("value"); writeOutput(myObj.getSomeProperty());
) one doesn't need, just for the sake of it.Tim
09/06/2022, 2:07 PMaccessors=true
, it automatically added properties to this
, so they could be get and set that way. But I couldn't get it to work, and couldn't find any documentation for it. But that's because it was actually this thing!