News for hackers. Stuff that matters.
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.