News for hackers. Stuff that matters.
Re:“A closure is a poor man’s object; an object is a poor ma... by betenoire on Monday May 01, 2017 @01:08AM (#14235689)
Avoid setters?
Re:“A closure is a poor man’s object; an object is a poor ma... by lacampbell on Monday May 01, 2017 @01:12AM (#14235707)
Not parent, but I think setters destroy what's objecty about objects - that you send them high level messages and don't care what's inside of them.

As Alan Kay puts it (paraphrasing): "setters turn an object back into a data structure"

Re:“A closure is a poor man’s object; an object is a poor ma... by scarface74 on Monday May 01, 2017 @10:02AM (#14237485)
What's the difference between a setter in languages like C# and any other method call?

I agree that if you class Foo with a property:

public string Bar {get; set; }

and used as:

foo.Bar = "baz"

It does look like just another data structure. But what's the difference between that and having a method:

public void setBar(string value) { _bar = value; }

It is just as easy to add logic to the property later on:

public string Bar{ get{ return _bar; }; set { ...some logic; _bar = value; }

As it is:

public void setBar(string value) { ..some logic; _bar = value; }

In fact, they both compile to roughly the same thing.

Re:“A closure is a poor man’s object; an object is a poor ma... by lacampbell on Monday May 01, 2017 @07:35PM (#14242744)
For me a setter is something that refers explicitly to data inside the object. It means the world outside the object is now intimately and inappropriately concerned with what's inside the object. The whole point of objects is to make them block boxes and ask them to do higher level things, and hide how they do those things to avoid information overload.

Even if a method does nothing but set an internal field of an object, in my eyes it's not a setter if it performs an actual high level task and doesn't expose the internal structure. IE:

    monster.setDead(true);
Is a setter. While

    monster.Kill();
Is not to me, even if it does the exact same thing inside. The latter doesn't bother me about internal details like some boolean field called "dead".

It's a subtle and almost pedantic distinction, but I hope you see where I'm coming from, and can appreciate the mindshift change from "this is a box full of fields" to "this is an entity".

Re:“A closure is a poor man’s object; an object is a poor ma... by betenoire on Tuesday May 02, 2017 @04:28PM (#14250269)
Thank you :) this is the answer I've been waiting for
Re:“A closure is a poor man’s object; an object is a poor ma... by dllthomas on Monday May 01, 2017 @10:13AM (#14237550)
I'd call that "still a setter". I think the concern is that if bar is more about the representation of the object than the behavior, you're not operating at the right level of abstraction.
Re:“A closure is a poor man’s object; an object is a poor ma... by scarface74 on Monday May 01, 2017 @10:30AM (#14237675)
And the other alternative, because an object still usually needs some type of values would be:

class Foo { Foo(string bar) { _bar = bar; } //Foo is the constructor }

The benefit being that once an object is created, it is guaranteed to be in a valid state.

But by classical OO an object represents state + behavior. So why not do both?

Assign the value in the constructor to guarantee that the object is always in a valid state and use a property (if the object is an entity as opposed to a value object), when it needs to change?

Re:“A closure is a poor man’s object; an object is a poor ma... by lacampbell on Monday May 01, 2017 @07:36PM (#14242750)
Why is it changing? have the method name reflect that domain reason, not reflecting the mechanical details of which internal fields need to change.
Re:“A closure is a poor man’s object; an object is a poor ma... by dragonwriter on Monday May 01, 2017 @07:51PM (#14242837)
The thing is, in simple cases the implementation will trivially parallel the sensible interface. So setters are appropriate.

The problem becomes using setters where that's not the case, often to support tooling that is dependent on constructing objects from serialized state (which often also involves the same kind of abuse of getters, to which exactly the same concerns apply.)

Re:“A closure is a poor man’s object; an object is a poor ma... by dllthomas on Monday May 01, 2017 @08:40PM (#14243094)
To be clear, I wasn't endorsing the concern, just attempting to clarify it. It doesn't surprise me that it can be over-applied.
Re:“A closure is a poor man’s object; an object is a poor ma... by eropple on Monday May 01, 2017 @02:09AM (#14235876)
This is one of the few things about Ruby idioms that drive me a bit nuts--improper use of `attr_accessor` when `attr_reader` creates better and cleaner code is one of the leading code smells in the library ecosystem.
Re:“A closure is a poor man’s object; an object is a poor ma... by cagmz on Monday May 01, 2017 @10:31AM (#14237685)
Is this to say that `attr_accessor` is too "heavy handed" when just using `attr_reader` could have sufficed?
Re:“A closure is a poor man’s object; an object is a poor ma... by eropple on Monday May 01, 2017 @10:46AM (#14237857)
It's not about heavy-handedness, it's that `attr_accessor` breaks encapsulation of private state and code very rarely takes into account what actually updating that member variable does.

It's used mostly because novice Rubyists are told that that's how you make a variable accessible, not that that's how you make it read-writable. In C#, on the other hand, when you create a property that's the moral equivalent, there's a lot more of a focus on creating getter-only properties that reveal state but don't let you munge it.

Ruby is also pretty bad about using #freeze and #taint, but the latter is a lost cause.

Re:“A closure is a poor man’s object; an object is a poor ma... by Ace17 on Monday May 01, 2017 @04:24AM (#14236248)
Obligatory Casey Muratory rant: https://www.youtube.com/watch?v=_xLgr6Ng4qQ