get set vs properties, whether they are a good idea

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
DarkShroom
Prole
Posts: 46
Joined: Mon Jul 17, 2017 2:07 pm

get set vs properties, whether they are a good idea

Post by DarkShroom » Fri May 18, 2018 10:24 am

I wanted to ask before i go ahead and think about adding properties to me game code

is there any explicit reason why love 2d itself does not use properties? is it perhaps because there is no way to reference 'self' without essentially copying curried functions into everything (functions that are embedded with the self reference

ie

Code: Select all

function () function2(reference_to_unique_object) end

for extra info about what i'm doing, i have created a object system where i have a reflected version of all the physics functions, it allows me to set the physics settings of an object before "activating" the physics (which creates the body, triangulates and adds the fixtures etc)... the reflected functions in the case of "fixture" for example, they run the same function on all the fixtures.... i wanted to abstract the physics system to i can turn it off and on and use the same variables to set things

User avatar
zorg
Party member
Posts: 2247
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: get set vs properties, whether they are a good idea

Post by zorg » Fri May 18, 2018 11:37 am

What do you mean by löve not using properities? I'm pretty sure both the Data and Object supertypes, and their subtypes both have tons of members, and getter and setter methods as well; case in point, SoundData has :getSample and :setSample, to name two.

As for whether it makes sense to use getters and setters, that depends on the coder, since by default, lua table access isn't really protected, so code could simply reference a member and either get or set the value that way, which is a more direct kind of thing than using accessor functions. (and as far as micro-optimizations go, probably just a tiny bit faster)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.

User avatar
ivan
Party member
Posts: 1265
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: get set vs properties, whether they are a good idea

Post by ivan » Fri May 18, 2018 12:27 pm

zorg wrote:
Fri May 18, 2018 11:37 am
What do you mean by löve not using properities?
I think he means things like:

Code: Select all

src = love.audio.newSource("bang.wav", "static")
src.volume = 0.5
It's just syntactic sugar and would probably be slightly slower than using functions.
Note that the default Lua modules (string, math, etc) do not use "properties" either, except for "constants" like math.huge or math.pi.
is it perhaps because there is no way to reference 'self' without essentially copying curried functions into everything (functions that are embedded with the self reference
That's not necessarily true - if you are using metatables.

User avatar
raidho36
Party member
Posts: 1663
Joined: Mon Jun 17, 2013 12:00 pm

Re: get set vs properties, whether they are a good idea

Post by raidho36 » Fri May 18, 2018 1:35 pm

Why would that be slower? If anything, it should be faster, however trivial getters/setters are inlined and then there is no difference whatsoever. The practice of using getters and setters is useful when getting and setting a value does more than just changes its private variable, when it has some side-effects. If not, you might as well make those public and access them directly, there will be no difference, neither effective nor performance-wise - as previously mentioned... well, unless the runtime has to use vtables to figure out which function to call, and then it's a performance hit for no reason.

Love objects use getters and setters because they are light userdata, not actual Lua objects, so they don't have their own properties, only a metatable. If you implement a class, there's a good chance that using trivial getters and setters will be detrimental for performance, on account of having to do one extra table lookup and a function call. So if this sort of code goes anywhere near performance-critical section, testing will be very important to establish how it affects things. In general, I suggest treating Lua objects as structs with methods, and you don't need any methods to simply access a struct member.

Post Reply

Who is online

Users browsing this forum: bartbes, zorg and 5 guests