Changing the setter argument is hacking or not?

Is it okay to change setter arguments? Imagine we have a setString method. And we really want to keep the cropped shape of the string. Thus, a line with trailing spaces is invalid, but we do not want to throw an exception.

What is the best solution? To trim a value in a setter, e.g.

public void setString(String string) { this.string = string.trim(); } 

Or trim it in the caller (more than once), for example

 object.setString(string.trim()); 

Or maybe something else?

+4
source share
9 answers

Yes. After all, setters are for that kind of thing! To control and disinfect values โ€‹โ€‹recorded in fields;)

+10
source

Completely. Here is an example: suppose you have engineering programs with different types of units. You save the internal values โ€‹โ€‹in one measuring system, but you convert from all others to the setter and convert back to the receiver, for example:

 public double UserTemperature { get { return Units.Instance.ConvertFromSystem(UnitType.Temperature, temperature); } set { double v = Units.Instance.ConvertToSystem(UnitType.Temperature, value); if (temperature != v) { temperature = v; Changed("SystemTemperature"); Changed("UserTemperature"); } } } 
+1
source

Oh sure. Just be careful to check for NULL before applying any method (e.g. trim ()).

0
source

There are two schools: one says that itโ€™s good to check the parameter in the setter (school style), and the second - beans should not contain any logic and just data (corporate identity).

I believe more in the second. How often do you look at implementing your beans? should getUser throw any exception or just return null?

When you put logic in your setter and recipients, it becomes harder for you to understand what is happening, since many people will never look at its implementation. If you disagree, I highly recommend that you take a look at each setter and getter implementation before using it to check if it is not just a bean.

0
source

At first glance, it seems that he violates the principle of least surprise . If I am a user of your class, I expect the setter to do exactly what I tell him. I would throw an exception in the customizer to get users to trim input.

Another (better?) Alternative is to change the method name to trimAndSetString. Thus, this is not surprising behavior for trimming input.

0
source

Correct me if I am wrong, but it seems logical to me that the installer should adhere to such logic. If the setter just assigns some value to the inner var without checking it, then why not expose var itself?

0
source

That is why you use setters, and not reveal the fields of objects for the whole wide world.

Consider a class that contains an integer angle, expected from 0 to 359 inclusive.

If you expose a field, callers can configure it for what they want, and this will violate the contract specified by your API. It can also upset your functionality somewhere down the track because your code is written to accept a specific range for this variable.

With the installer, you can do several things. One of them is to raise an exception by specifying an invalid value, but that would be wrong in my view (for this case). This will probably be more useful if you change the input value to something between 0 and 359, for example:

 actualVal = passedValue % 360; 

As long as this is indicated in your interface (API), it is completely correct. In fact, even if you do not specify it, you are still free to do whatever you want, because the caller has violated the contract (passing the value out of range). As a rule, I adhere to the rule "sanitize your contribution as soon as possible."

In your particular case, while you indicate that the string is stored in a truncated format, there is no reason for the caller to complain (you have already stated that such a string is unacceptable). It is better in terms of code size (not speed) to do this in the setter, and not on every piece of code that the setter calls. It also ensures that the string is saved as you expect - there is no guarantee that the caller will not accidentally (or purposefully) save the string without trimming.

0
source

Yes. A feature of object-oriented design is that the caller can treat your class as a black box. What you do internally is your own business if the behavior of the interface is documented and logical.

0
source

While different people have different philosophies, I would suggest that property determinants are only suitable when they establish an aspect of the state of an object according to the specified value and, possibly, can notify anyone who is interested in this change, but will not otherwise, it affects the state of the object (it is completely correct for the setter property to change the value of the read-only property if this property is defined in terms of the state associated with the property installer, for example, a control read-only right property can be defined in terms of its Bounds ). Networking professionals should throw an exception if they cannot perform the specified operation.

If someone wants the client to somehow modify the state of the object without satisfying the above description, you should use a method, not a property. If you call Foo.SetAngle(500) , it would be reasonable to expect that the method will use the specified parameter when setting the angle, but the Angle property may not return the angle in the same form that was set (for example, it can return 140), On the other on the other hand, if Angle is a read and write property, one would expect that writing a value of 500 would either be disabled or could cause the value to be read back 500. If you wanted the object store to be located at an angle in the range from 0 to 359 objects may also have the property only o for reading, called BaseAngle , which will always return an angle in this form.

0
source

All Articles