someone
someone

Reputation: 6572

Change to void or keep the return values

I'm doing refactoring/review of a Java application

When I'm doing that I show that some of method has return values such as Object, String, Boolean, etc., but return values are not used in any places. Only have done the method calling.

So, I'm just wandering keeping them as it is will cause the performance issue for application.

Should I change them to void or keep them as it is?

Upvotes: -1

Views: 743

Answers (4)

Roman C
Roman C

Reputation: 1

Should not return a value if the value in not used. Use void instead. Sometimes I see some getters that is not used but actually they are used by the web framework. It's difficult to determine if the method is unused. Even if it's used but the return value is ignored. There's no restriction to not ignore the return value.

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

  • Moreover than being a performance hit its an inappropriate construction of a method.

  • If the method's returned value has no use in the program then there is no reason to return it, and so the method's return type should be made void.

Upvotes: 4

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78579

In my opinion, either the API is being incorrectly used or it is incorrectly designed.

If the API is correctly designed then why are the API users not using the method return types? In this case the users must be wrong.

On the other hand if the API is incorrectly designed, what is the point of using it in its current state? If the returned information is superfluous then fix the API and make the methods void.

I think that performance considerations would be of little importance when compared to a good API design. Performance can always be improved later, but APIs are very difficult and expensive to change.

Upvotes: 1

David
David

Reputation: 3937

I do not think that there would be a performance penalty to keeping the return types like they are.

That being said, I think that you should still remove them. The reason is that they are essentially dead code. There might be unknown bugs lurking in those methods revolving around the return types - unknown because they are not used. This is a potential danger if someone decides to use them one day.

Furthermore, the maintenance burden is increased if you keep them: Everytime someone touches one of these methods, she has to (unnecessarily) think about the return type.

This essentially boils down to YAGNI.

Upvotes: 2

Related Questions