At the company I work for there's a document describing good practices that we should adhere to in Java. One of them is to avoid methods that return this
, like for example in:
class Properties {
public Properties add(String k, String v) {
//store (k,v) somewhere
return this;
}
}
I would have such a class so that I'm able to write:
properties.add("name", "john").add("role","swd"). ...
I've seen such idiom many times, like in StringBuilder
and don't find anything wrong with it.
Their argumentation is :
... can be the source of synchronization problems or failed expectations about the states of target objects.
I can't think of a situation where this could be true, can any of you give me an example?
EDIT The document doesn't specify anything about mutability, so I don't see the diference between chaining the calls and doing:
properties.add("name", "john");
properties.add("role", "swd");
I'll try to get in touch with the originators, but I wanted to do it with my guns loaded, thats' why I posted the question.
SOLVED: I got to talk with one of the authors, his original intention was apparently to avoid releasing objects that are not yet ready, like in a Builder pattern, and explained that if a context switch happens between calls, the object could be in an invalid state. I argued that this had nothing to do with returning this
since you could make the same mistake buy calling the methods one by one and had more to do with synchronizing the building process properly. He admitted the document could be more explicit and will revise it soon. Victory is mine/ours!
The most common causes of purchase returns are unmet expectations, damaged or defective products, and incorrect fit. Any of these issues can be caused by failures on the merchant's part or by events the merchant had no control over.
Tell the clerk you want to return the item. Smile and say, “Hi, I want to return this item which I bought last week.” Show the clerk the item and your receipt. Don't delay returning the item. Some stores allow returns but only for a certain amount of time.
Where Do Your Returns Go? You would think returns would go right back out into the market, but that's rarely the case. According to a 2018 report, returns account for five billion pounds of waste sent to landfills and 15 million tons of carbon emissions every year in the United States.
If you return 5-10 items per month or more than 10% of what you order for no reason other than that you changed your mind, you may receive a warning. If you continue returning items, your account will be suspended or you'll even be banned from Amazon.
My guess is that they are against mutable state (and often are rightly so). If you are not designing fluent interfaces returning this
but rather return a new immutable instance of the object with the changed state, you can avoid synchronization problems or have no "failed expectations about the states of target objects"
. This might explain their requirement.
The only serious basis for the practice is avoiding mutable objects; the criticism that it is "confusing" and leads to "failed expectations" is quite weak. One should never use an object without first getting familiar with its semantics, and enforcing constraints on the API just to cater for those who opt out of reading Javadoc is not a good practice at all— especially because, as you note, returning this
to achieve a fluent API design is one of the standard approaches in Java, and indeed a very welcome one.
I think sometimes this approach can be really useful, for example in 'builder' pattern.
I can say that in my organization this kind of things is controlled by Sonar rules, and we don't have such a rule.
Another guess is that maybe the project was built on top of existing codebase and this is kind of legacy restriction.
So the only thing I can suggest here is to talk to the people who wrote this doc :)
Hope this helps
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With