Excessive use of accessors?

camelCase

The Case of the Mysterious Camel.
Reaction score
362
I didn't know how to best title this thread but it's just a silly question that popped up in my head.
I've been working on the framework for my game for a bit and recently re-wrote the entire Unit class (which is huge).

Anyways, within the class method definitions, should I use the accessors in them or just directly access it?
Like this:
Code:
void someClass::someMethod () {
    int i = getSomething();
    //Some stuff
    setSomethingAwful(getSomethingElse()*i+getSomethingElseEntirely());
}

Versus. this:
Code:
void someClass::someMethod () {
    int i = something;
    //Some stuff
    somethingAwful = somethingElse * i + somethingElseEntirely;
}

Is one faster than the other?
(I would think that the one filled with function calls is slower, right?)
 

Vestras

Retired
Reaction score
249
setSomethingAwful is better. The whole more function calls being slow is utter bullshit. It is completely unnoticeable and I doubt it'll be your actual problem if our application performs bad.
 

SerraAvenger

Cuz I can
Reaction score
234
The whole more function calls being slow is utter bullshit. It is completely unnoticeable and I doubt it'll be your actual problem if our application performs bad.
True
setSomethingAwful is better.
Don't overengineer unless you have to. There are cases where using abstractions for everything is a good idea. Sometimes it's just more work, fuglifies your code, but doesn't add anything. If you're using a good editor it is no problem to add an abstraction later.
Don't misread this: As long as you're accessing from the outside, use accessors. If you're working from inside, do if necessary.
In the case of doubt, use abstractions. Highly exaggerated, you have to find the balance between "getting things done" and "getting things right".
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
It seams to me that getters/setters are being replaced in favor of properties which combine the
more readable syntax of a direct accessing of attributes/fields/data member/... and the data validation/update of
setters/getters.
So in my opinion you should use properties, and only in special cases (the only special case I know about is
the representation of a vector with 3 reals/floats =), using setters and getters with vectors will probably slow things a lot
and bloat a lot of things) using direct access (yeah this means no set/get methods oh well...=))

If you are using the Microsoft C++ compiler it seams it has additional features (see the wiki page for property) added to the language like properties.
(but that probably means your code will become less portable if you actually care)
 

Vestras

Retired
Reaction score
249
True

Don't overengineer unless you have to. There are cases where using abstractions for everything is a good idea. Sometimes it's just more work, fuglifies your code, but doesn't add anything. If you're using a good editor it is no problem to add an abstraction later.
Don't misread this: As long as you're accessing from the outside, use accessors. If you're working from inside, do if necessary.
In the case of doubt, use abstractions. Highly exaggerated, you have to find the balance between "getting things done" and "getting things right".

Yes, I agree. In the inside, use private members. From the outside, use accessors.

It seams to me that getters/setters are being replaced in favor of properties which combine the
more readable syntax of a direct accessing of attributes/fields/data member/... and the data validation/update of
setters/getters.
So in my opinion you should use properties, and only in special cases (the only special case I know about is
the representation of a vector with 3 reals/floats =), using setters and getters with vectors will probably slow things a lot
and bloat a lot of things) using direct access (yeah this means no set/get methods oh well...=))

If you are using the Microsoft C++ compiler it seams it has additional features (see the wiki page for property) added to the language like properties.
(but that probably means your code will become less portable if you actually care)

VC++ is Windows only, and honestly I think that property syntax is pretty damn ugly.
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
getters are really only useful to be able to make more of your data private and to put restraints on them (e.g. post-conditions).

The compiler optimizes away all those function calls, however.

So in the end it basically is a question of what you're more comfortable with.
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
Is there, like, a limit to what the compiler will and will not optimize for accessors and mutators?
IIRC, inline functions don't become inline if there's a loop or some other stuff.
Just a little curious.

But I think I might just stick to using the getters and setters so it's easier to modify parts of the code =x
(I'm so lazy, ugh)
 

s3rius

Linux is only free if your time is worthless.
Reaction score
130
Yes, there are limits, but there aren't easy rules you can follow.
Loops will be inlined less often, because they lead to a lot of assembler code.

Basically the compiler checks if it's worth it by comparing how much assembler code it'd have to create when it inlines a function, compared to how much performance gain you'd get from inlining (no register swtiches, no address jumps).

One-lines will very often be inlined unless you do a lot of computation within it.. something like this maybe:

Code:
int blah(int x, data p){ //Does completely stupid things
    return GetRandomNumber(0, GetAverage(GetValueArrayFromData(p, GetArraySize(p)*size(x))));
}

In these cases it might not be worth inlining every instance of blah.

Simple getters/setters will always be inlined.

Nice to note: Virtual functions - even accessors - won't ever be inlined.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top