Skip to content

Accessors #4

Closed
Closed

Description

@reissbaker

I disagree that setValue/getValue style accessors are better than jQuery-style value()/value(10) accessors. They add unnecessary name length, and require twice as many function definitions.

Admittedly, it's slightly faster for a VM to deal with functions that always return a single value type, as opposed to sometimes returning a type and sometimes returning nothing. But it's also slightly faster to download scripts using jQuery-style accessors, because the function keyword, prototype keyword, and public property names are unminifiable -- and we've yet to run into issues where our JS was too slow (whereas we do run into issues where our files are too big). If we have to compromise in one direction, I'd lean towards filesize over VM execution speed.

Additionally: the setters can still return a value, despite being "only" setters. Which means the VM executes just as quickly as it normally does.

It's also just less typing.

Activity

  1. hshoff commented on Nov 1, 2012

    @hshoff
    Member

    Up for whatever.

    I picked using getValue / setValue because intent is clearer and you don't have to type out if (!arguments.length) { return this.val } everytime you want to make a new get/set for a property. I prefer the get(prop) / set(prop, val) way.

  2. ssorallen commented on Nov 2, 2012

    @ssorallen
    Contributor

    I can go either way on this one. Methods in Monorail, and often in our JS, are longer than they should be. Methods should be short and do only what their names say they do. By splitting into get and set, you know exactly what the methods do.

    The jQuery way can be tricky if you don't check the value you pass to it. This is a contrived example, but it could happen:

    function Obj() {}
    Obj.prototype.val = function(value) {
      if (typeof value === 'undefined') {
        return this.value;
      } else {
        this.value = value;
      }
    };
    
    var a;
    var b = 10;
    
    var obj = new Obj();
    obj.val(50);
    
    if (b > 20) {
      a = 15;
    }
    
    // Expect to clear / set to undefined?
    obj.val(a);

    That being said, we all use jQuery without any problems.

  3. spikebrehm commented on Nov 2, 2012

    @spikebrehm
    Collaborator

    I don't like the ambiguity that comes with using value() / value(10).

  4. hshoff commented on Nov 2, 2012

    @hshoff
    Member

    We just need to be consistent.

  5. reissbaker commented on Nov 2, 2012

    @reissbaker
    ContributorAuthor

    Eh, guess I'm outvoted on this one. Oh well.

  6. added a commit that references this issue on Dec 8, 2016
  7. added a commit that references this issue on Aug 22, 2017
  8. added a commit that references this issue on Apr 10, 2018
  9. added a commit that references this issue on Mar 26, 2023
    2c29ce8
  10. added a commit that references this issue on Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions