Replies: 3 comments 4 replies
-
While I understand there's appeal to languages with no hidden control flow, this can be in direct opposition with concise, expressive code. I think all we can do here is use a different icon for properties that have a getter/setter assigned to them in autocompletion. Built-in memoization methods could be interesting to have, but I haven't seen any language that offers them in their standard library. Some languages have parsers that can perform it automatically, but this only has a limited scope of applications.
This rule is internally used in Godot's source code for function parameters. Either way, the current GDScript style guide doesn't advocate for Hungarian notation, so I don't think this would be a good fit for the style guide. Requiring
Not all getters are slow though. This linter would be complex to create in an accurate manner, as the time it takes for the getter to run depends on the current property value, the CPU speed, the build options used for the engine binary and so on. We can't base such a warning based on a time taken in milliseconds, as it would mean the warning would only be emitted on certain machines and not others. |
Beta Was this translation helpful? Give feedback.
-
I may be in the minority on this opinion, but personally I like the idea of (at least the naming conventions) not necessarily being different for properties versus variables. In my mind, properties are like "magic variables", that I can treat from outside the black box just like I would a variable, except that when I interact with them, the black box is smart enough to do extra things. Making it more obvious via naming convention differences that the variables and properties are different things would break this "illusion". It'd probably start to feel (to me at least) that the properties are really just getter/setter functions, when I thought their whole purpose was to look like a variable from the outside. |
Beta Was this translation helpful? Give feedback.
-
Static analysis of whether a getter has side effects is extremely difficult to implement for GDScript right now. Yes, a getter should generally not have side effects, but we can't rely on that. It's ok to have side effects that don't affect the main logic, like printing debug information. Also, a getter can have complex logic where you compute a value through other field(s), so that requires more complex control flow analysis. I think that function inlining is a more feasible optimization than control flow constancy analysis. It would be useful not only for setters/getters but also for other short functions, since calling a GDScript function is quite an expensive operation. And of course I agree that we should add a note about performance in the documentation, plus we could display fields/properties differently in autocompletion. |
Beta Was this translation helpful? Give feedback.
-
The issue
Since there is no stylistic distinction between a
property
and avariable
, it is hard to tell whether extra work is being done. Properties can perform extra work to get their value, and are more expensive than just referencing a variable. So it would be best to cache the result of a property, instead of directly accessing it three times in the same function.Can you tell which one of these is a property and which one is just a variable?
An example
Say I have the following class:
Now, in other scripts, I can see no difference between whether I am accessing a variable
tiles
or a propertysecond_to_last_tile
.e.g. this variable access:
Looks pretty much the same as this property access:
But it would be much more expensive than
As the latter example doesn't have to perform the size calculations and if checks each iteration.
Proposed solution
You could add a rule to start properties with ap_
prefix.Or maybe access properties through..
instead of.
Or maybe usePascalCase
(this would still conflict with types, but it'd be less confusing given that a Type is not used as a value)Honestly, I'm not too happy with any of my solutions, they feel too intrusive, but here's three things I came up with that might more realistically help address this:
It would be a good start to add highlight groups to properties and color them differently from regular methods. But that is not very accessibility friendly.
It would be good if there were different icons for variables and properties:
An 'expensive invocation' check could be added to the linter. I know that when I was developing in Unity they had such a linting option - it would warn me if I accessed a result from a function or property in a function twice, and recommended me to cache these into a local var. This still wouldn't solve the issue of not being able to discern between props and vars in a non-editor setting, like reviewing pull requests on the Github website.
Beta Was this translation helpful? Give feedback.
All reactions