Lurker News

Comments (61) 30 threads

Show all comments

Showing thread 2 of 30

ttty2 replies#
I never like how they chain methods. A better way would be:

    texture({lines: true, heavier: true, stroke: 'red'})
Is easier to configure if you need reusable textures.
findjashua#
imho, if the operations are to be performed in a certain order, use chaining. Otherwise, passing a config object is much cleaner
wayfarer2s#
An initial config object would probably be ideal, and then you could have the chaining to supplement those values. You could have the initial method take in the config and use the values in that in place of the defaults. So

  textures.circles()
    .radius(4)
    .fill("transparent")
    .strokeWidth(2);
could initially be

  var circles = textures.circles({
    radius: 4,
    fill: "transparent",
    strokeWidth: 2
  });
and later updated to be

  circles.strokeWidth( 1 );
Which works rather nicely especially when the config object is generated dynamically.
bkirwi#
Yeah, it makes sense to mimic the D3 style: the only thing worse than a bad convention is two conventions.
mbostock1 reply#
Aren’t these textures already reusable? The constructed texture object (the result of textures.lines(), e.g.) can be called on as many selections as you like to reuse it.

I feel it’s a matter of taste, but one practical reason I prefer chaining to configuration objects is that you get a runtime error when you misspell a property, rather than it being silently ignored.

ttty2 replies#
Image you have an UI which talks to this. For example you have a button on/off. How would you do?

    var t = texture().lines().stroke('red')
    if(this.button.on) t= texture.heavier()
Compare the previous with this one.

    var t = texture({lines: true, heavier: this.button.on, stroke: 'red'})
I also hate the non standard constructor: this should be like this

    var texture = new Texture({lines: true, heavier: this.button.on, stroke: 'red'});
siddboots#
"heavier: this.button.on" has ambiguous semantics. Do libraries really use this pattern?

Also, use of the "new" keyword is contentious. You can hardly pretend that it is best practice.

ricsca#
It should be

    var t = texture().lines().stroke('red')
    if(this.button.on) t.heavier()