Skip to content
This repository has been archived by the owner on May 29, 2020. It is now read-only.

Use explicit getters to access all fields #31

Closed
Fingercomp opened this issue Aug 21, 2018 · 0 comments
Closed

Use explicit getters to access all fields #31

Fingercomp opened this issue Aug 21, 2018 · 0 comments
Assignees
Labels
housekeeping Refactoring, making things cleaner, etc wonderfully hard

Comments

@Fingercomp
Copy link
Member

Fingercomp commented Aug 21, 2018

For example, replace uses of LeafNode.parentNode outside that class with LeafNode:getParent().

Though the basic idea is simple, it requires changing a ton of the code, and is very error-prone. But this kind of thing is better to be done earlier than later.

Expected advantages:

  • Increased perfomance when switching from Foo.__getters:bar to Foo:getBar(). __getters are implemented using metamethods, so, when an object is flattened by calling object:optimize(), they aren't added to the object table. Which means that every access to the getters requires traversing the inheritance tree.
  • Errors detected earlier. node.parent (expected node.parentNode) returns nil all the time. This hides the real error, as node.parentNode can also return nil if the node has no parent (is a root node). I've already spent a few days debugging such problems.
  • Makes it explicit that all fields of a class are considered implementation details, and aren't supposed to be accessed directly by the external users, but via the means of getFoo and setFoo methods.
    • Also makes it easier to preserve the invariants.
  • Behavior of getters that have side-effects is made less suprising.

Expected disadvantages:

  • Decreased perfomance when switching from Foo.field (just a table index) to Foo:getField() (usually 2 table indexes, and a call). I don't think the added overhead here are too high to be ignored, but if it does become a bottleneck, the field could just be accessed directly.
@Fingercomp Fingercomp added wonderfully hard housekeeping Refactoring, making things cleaner, etc labels Aug 21, 2018
@Fingercomp Fingercomp added this to the wonderful-core 0.1.0 milestone Aug 21, 2018
@Fingercomp Fingercomp self-assigned this Aug 21, 2018
@Fingercomp Fingercomp mentioned this issue Aug 21, 2018
4 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
housekeeping Refactoring, making things cleaner, etc wonderfully hard
Projects
None yet
Development

No branches or pull requests

1 participant