Changes between Initial Version and Version 1 of Ticket #5387, comment 27


Ignore:
Timestamp:
May 30, 2020, 3:02:34 PM (4 years ago)
Author:
elexis

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #5387, comment 27

    initial v1  
    2929* Functions performing work onTick are to cache as much information as possible in their class instance.
    3030* Event handling functions are called externally, hence require special attention and should appear prior to the main substance of the class.
     31* `static` functions don't gain the benefit of being able to use member variables and don't gain the benefit of being able to use prototype inheritance and object instance changes from other files (which is especially useful for modders). Example: `static` functions have to be called using `ClassName.StaticFunction`. So a mod (like nanis autociv mod) would have to replace the function of `ClassName` to modify that function. But if the function was not written to be a static one, then the mod can extend the JS object that is an instance of the class without changing the class; and the mod can introduce JS object properties to that instance too (even if the original class `ClassName` didnt need a `this` instance). Since every `static` function can trivially be implemented as a non-static function, and the non-static ones have tangible benefits, the pattern to never use `static` (unless in some strongly reasoned exception) established in my commits.
     32* `get` may be treated as an anti-pattern, because it hides to the caller which property calls are function calls. But the distinction between a function and a JS property fetch should be clear to the caller. If one only wants to implement a `get` function without a `set` function, one can do the same more commonly with an explicit function. If one wants to implement a `get` and `set` pair, then it modifies most likely a local state variable, and that local state variable can't be hidden / set to private with current JS standard. So `get`+`set` pairs don't have an advantage over `getFoo` + `setFoo` pairs or `.foo` + `setFoo` pairs while having the disadvantage of hiding the fact that calling the thing is a function call. Hiding function calls is bad because they can be expensive for performance, they can have side-effects such as statechanges. So the reader should be informed when they run JS statements rather than being led to believe that there is only a JS property lookup (that does not perform any unrelated state changes).
     33
     34