[ 5 posts ]

Satyam

YUI Contributor

  • Username: Satyam
  • Joined: Tue Dec 09, 2008 12:34 am
  • Posts: 2016
  • Location: Sitges, Spain
  • GitHub: Satyam
  • Gists: Satyam
  • IRC: DevaSatyam
  • YUI Developer
  • Offline
  • Profile

Unify attribute setter and validator

Post Posted: Tue Apr 28, 2009 1:42 am
+0-
The order in which the attribute setter and the validator are called in Base has recently changed. I think that there are arguments in favor of either order but I am wondering whether there is any reason to actually have them both.

For example, if validator comes before setter, I have to repeat in validator much of the code of setter, the same number of if statements which, in the first, it would return true or false and in the second, the converted value.

If setter comes before validator, setter would actually do some checks on the values before converting them, which is also redundant and possibly return some invalid value so that validator knows when to reject the setting.

This redundancy makes me believe that there is no point in having them both, being as closely tied as they are. I would suggest that we have only a setter which would do both, agreeing that a return of undefined would be understood as a rejection of the value. Thus, all the checks on the input values would be done just once and if any succeeds, the conversion would be performed and a valid value returned, otherwise, executions would drop of the end of the setter, with no value returned so far, which would result in a return of undefined.

I doubt that an attribute would really need to hold an undefined, null being almost the same thing semantically and much clearer (undefined is often an accident, a missed initialization while null is clearly set on purpose). If undefined would ever be required, I can imagine it would make sense as an initial value which could then be set by calling reset.

Eric Ferraiuolo

YUI Developer

  • Username: ericf
  • Joined: Mon Jan 12, 2009 8:26 pm
  • Posts: 380
  • Location: Boston, MA
  • Twitter: ericf
  • GitHub: ericf
  • Gists: ericf
  • IRC: eric_f
  • YUI Developer
  • Offline
  • Profile
Tags:

Re: Unify attribute setter and validator

Post Posted: Tue Apr 28, 2009 5:58 am
+0-
I like your proposal here. Something to add…

The pattern of having a validator pointing to a this._validateNewAttributeVal could still work, as this function could be called in the setter method. With such a pattern, extending a Class and overriding it's validate method would still be possible.

I agree that it doesn't make much sense to validate a new attribute value, then pass it to a setter which is changing the value.
Eric Ferraiuolo

Satyen Desai

YUI Developer

  • Username: sdesai
  • Joined: Tue Dec 09, 2008 4:17 pm
  • Posts: 302
  • GitHub: sdesai
  • Gists: sdesai
  • YUI Developer
  • Offline
  • Profile

Re: Unify attribute setter and validator

Post Posted: Tue Apr 28, 2009 8:00 am
+0-
Unified validator/setter support is actually already checked in. Appologize for not getting the READMEs updated, will do so today which outlines all the changes which have gone in post PR2 for attribute/base.

You are now able to return Attribute.INVALID_VALUE from a setter, to prevent a set from going through, so if you have logic which is repeated in your validator and your setter, you have the option to combine the two for optimal performance.

Support for an independant validator is also maintained (for simple cases - where you want basic type checking), and it is invoked before the setter.

Satyam

YUI Contributor

  • Username: Satyam
  • Joined: Tue Dec 09, 2008 12:34 am
  • Posts: 2016
  • Location: Sitges, Spain
  • GitHub: Satyam
  • Gists: Satyam
  • IRC: DevaSatyam
  • YUI Developer
  • Offline
  • Profile
Tags:

Re: Unify attribute setter and validator

Post Posted: Wed Apr 29, 2009 1:20 am
+0-
Your choice of value for INVALID_VALUE is great! A wonderful trick I had never thought about. I was wondering what value you could have used and when I saw it was {}, I thought, that is a perfectly valid value for an attribute that you might really want to set. I thought that:

return {};

and

return Attribute.INVALID_VALUE

were the same when, in fact, they are not, they might equate to the same, but they are not the same.

Wonderful, excellent!

Satyen Desai

YUI Developer

  • Username: sdesai
  • Joined: Tue Dec 09, 2008 4:17 pm
  • Posts: 302
  • GitHub: sdesai
  • Gists: sdesai
  • YUI Developer
  • Offline
  • Profile

Re: Unify attribute setter and validator

Post Posted: Wed Apr 29, 2009 9:51 am
+0-
Thanks. Just to close the loop - I've checked in README updates to Attribute, Base, Plugin, Widget which reflect post PR2 changes.
  [ 5 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum