ga-wdi-boston / ember-object

Explore the Ember Object Model, and leverage it to do some basic binding through computed properties.
Other
1 stars 112 forks source link

Remove all naked access, assignment, and execution #14

Open jrhorn424 opened 8 years ago

jrhorn424 commented 8 years ago

The principle driving this issue is called "The Principle of Uniform Access".

It helps that adhering to this principal will avoid bugs, and that breaking this principle in Ember will break your application code in odd and hard-to-debug ways.

// GIVEN
const Person = Ember.Object.extend({
  givenName: null,
  familyName: null,
});

const Developer = Person.extend({
  jobTitle: 'Developer',
  doJob () {
    return 'Computering';
  },
});

let jeff = Developer.create({
  givenName: 'Jeffrey',
  familyName: 'Horn',
});

// BAD
jeff.givenName; // some Ember junk
// GOOD
jeff.get('givenName'); // "Jeffrey"

// BAD
jeff.givenName = 'J-Rizzle' // let's break Ember
// GOOD
jeff.set('givenName', 'J-Rizzle'); // "J-Rizzle"

// BAD
jeff.doJob(); // 'Computering'
// GOOD
jeff.get('doJob')(); // 'Computering'

As a specific example for execution done wrong in this talk:

const Person = Ember.Object.extend({
  fullName: function(){
    return `${this.get('givenName')} ${this.get('surname')}`;
  }
});

let bob = Person.create({
  givenName: 'Bob',
  surname: 'Belcher'
})

-bob.fullName(); // 'Bob Belcher'
+bob.get('fullName')(); // 'Bob Belcher'
gaand commented 8 years ago

Is this solved more cleanly with a computed property? Is it too soon to talk about that with this talk?

raq929 commented 7 years ago

@jrhorn424 The current example is beetlejuicing, we should definitely remove it.

MicFin commented 7 years ago

This example in particular

const Person = Ember.Object.extend({
  fullName: function(){
    return `${this.get('givenName')} ${this.get('surname')}`;
  }
});

let bob = Person.create({
  givenName: 'Bob',
  surname: 'Belcher'
})

bob.get('fullName')(); // 'Bob Belcher'