So you don't like either of the following:
myObject.name = "Ben";
myObject.setName("Ben");
What alternative would you suggest then?So you don't like either of the following:
myObject.name = "Ben";
myObject.setName("Ben");
What alternative would you suggest then?The whole point of an object is that client code shouldn't be concerned with such details as maintaining the state of each internal field. Try and think of an object as a single thing you send high level messages to you, instead of a struct, or a bag, or a hashtable.
Says who?
For example, there is a business requirement that we know the on-hand quantity of some item in the warehouse.
It's a busy warehouse, and we keep picking that item for delivery, and replenishing it from our suppliers.
So, the quantity of the item keeps mutating.
How do you propose to deal with the above? Tell the warehouse people that they should only do their thing "rarely"? Seriously.
What is the point here?
Point - say you have a database with 300 tables and ~3,000 columns.
You do not want to play the game of "hide the method to change the value". That is a big waste of time for everyone involved.
Yeah I want to say that for us developers, "rare" is not something that we can code, without some serious sophistication.
More likely we code "if" and it is either "yes" or no".
To capture "rare", we need some serious tools - statistics, analytics etc etc. That is way beyond the regular developer toolset.
For me a better question is - why is the name being changed? What high level goal are you trying to accomplish that truly needs you to to directly mutate internal fields, after the object has already been created? Could the object - or system of objects - have been designed with a higher level interface, so that the outside world didn't have to concern itself with such things?
As an aside, I'd probably make a method called "rename" that does this, as opposed to something like "setName".
Consider a trivial to-do list. You can click on the 'completed' checkbox. So the 'completed' field has chaged.
Or, you can say ooops I mistyped the label. So you change the label.
Is there anything wrong with that?
I don't know if it's worth the overhead of having unthinking developers writing { get; set } for every field in every class because it makes things "easier".
I don't know if it's worth the overhead of having unthinking developers writing { get; set } for every field in every class because it makes things "easier".
I do not understand your point. First you claim that we should not mutate the field, then you claim that we should mutate the field.
Which one is it? Mutate or not?
We should generally not use goto or local variables either. Avoid reaching for these constructs first. But if it's by far the cleanest, and simplest way of solving the problem - which it usually isn't - go ahead and do it.