Jump to content

If you can't use a getter/setter, which format do you prefer?


rich
 Share

Recommended Posts

If a framework was unable to offer you a direct getter/setter implementation, i.e:

 

alien.x = 50;if (alien.x > 320 && alien.health < 10) {   alien.destroy();}

 

Which of the following formats would you prefer?

 

The braces:

 

alien.x() = 50;if (alien.x() > 320 && alien.health() < 10) {   alien.destroy();}

 

Or the 'get' prefix:

alien.getX() = 50;if (alien.getX() > 320 && alien.getHealth() < 10) {   alien.destroy();}

 

Please assume for the sake of this question that x cannot be a simple variable and the method contains important update logic.

 

Do you have another style you prefer more?

 

Ideally we want to support getters/setters but the implementation (object.defineProperty) isn't supported in older browsers (IE7 for example).

Link to comment
Share on other sites

...

The braces:

 

alien.x() = 50;

 

This confused me a bit. Assigning a value to a function.

 

Yeah that was option 1 benny :) interesting its a 50/50 split so far!

 

Yup, I guess a lot of pepole like the explicit usage of getter/setter which were broadly used in Java. I like em too - however I tend to more and more like implicit getter/setter e.g. introduced with Actionscript.

Link to comment
Share on other sites

  • 3 weeks later...

In C I was using x() and setX() as most libraries use this naming convention, because x should be private anyway. In Python we can define properties, so set and get are just a waste of time and this is the best option for me. I would stick to defineProperty in JS.

Link to comment
Share on other sites

  • 2 weeks later...

I would prefer the syntax already used by the famous Backbone framework:


alien.get('x'); 

or 

 

alien.set('x', 20); 

 

With this technique you can avoid appending a bazillion of new methods to the object (namely two for every attribute) and stay flexible for future additions of attributes.

Its also fast! Lets assume you hold an internal "attributes" object in the alien object:

 

function Alien(){	var attributes = {		x: 10,		y: 10	}	this.get(attr){		return attributes[attr];	}	this.set(attr, value){		attibutes[attr] = value;		return this;	}}

 

You can see: all attributes remain private to the object constructor and can only be accessed via the get/set methods. You can also add some validation to the set function (or emit some events when values have been changed), but the get() method is really really fast and easy to use outside the object.

 

I would adopt this pattern, since it worked very well for Backbone, which is a widely used and proven library.

 

Edit:

 

With a little tweak, you would also be able to use the set() method to set multiple values at once, like so:

alien.set({    x: 10,    y: 20});

 

 

Link to comment
Share on other sites

Well, you have to ask yourself a question, why we use setters and getters in first place.

Because If you will need additional validation or operation on value, then you would have to replace every assignement

 

 obj.x = 1

 

 to 

 

set('x', 1)

 

  Now lets assume that we have a node of html and we want to add some text to it. In your example it would be 

 

node.set('text', text)

 

  but we need to truncate text to prevent users from code injection. So your set code would have to look like this:

function Node(){	var attributes = {		text: 10	}	this.get(attr){		return attributes[attr];	}	this.set(attr, value){                switch (attr) {                    case 'text':                         value = truncate(value);                         break;                }		attibutes[attr] = value;		return this;	}}

 

It has few flaws.

It's slow to check attr every time you set value. 

If you get more attr to validate your switch code will grow fast.

 

If you don't want bazillion of methods then use defineProperty, it's the best solution.

Link to comment
Share on other sites

After thinking a bit about it, I think defineProperty is the best solution, too.

 

It supports get/set methods for the property (docs in MDN) and is widely supported in nearly all browsers, even IE.

 

It also brings the usage back to the simple:

 

alien.x = 20;

 

Which is shorter and clearer than:

alien.set('x', 20);

 

So +1 for @Quezacotl's suggestion for using defineProperty.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...