Good practice - writing methods that return void?

I am working on a sophisticated enterprise Java-encoded application that is driven by complex object compositions. For example: in a specific scenario, to execute an action, this is a thread:

login() -> Followed by defined sequence of 10 to 20 method calls in different classes -> Followed by a logout() 

Within the framework of almost all actions, including login, logout and many method calls from 10 to 20, do not have return types. Any erroneous behavior is handled by the framework. Let's say in login

 public void login(){ try{ //login actions //chained sequence of calls }catch(){ // framework handling exceptions and other rollback options } } 

Intermediate 10-20 actions are method calls for different objects at different levels of the hierarchy of the structure.

A random class would look like this:

 class someobject{ def variable void action1(){ do something on variable } void action2(){ do something on variable } ... } 

This variable often changes state, and these actions have a sequence defined only by the wireframe, which I find very annoying.

I would say: โ€œPerhaps if there were suitable return types in general, or at least some of these methods, as in the case of the boolean in the case of login() , life would be much simpler. Due to this tight attachment to the series void return functions are hard for me to debug to understand the flow, and unit testing has become a nightmare for me.

So, now I get the impression that it is always better to write functions that return something, especially when there is a chain of actions. So is this a safe presumption? I would like to express my opinion on this matter. Correct me if I am wrong.

+8
java return void return-value
source share
5 answers

The most basic method verifiability is through the return code. In the case of login you need (as you noted) to check if you are logged in, and the return of boolean is obvious. Otherwise, you need to check some property that seems unnecessarily atomic and complex (although it may be required for other reasons).

For me, this argument extends to any meaningful method. Using void return codes is fairly common, but more as a result of old habits than for good design reasons. However, simple means of defining properties is a counterexample, and I'm sure there are others.

+4
source share

When a method works with local data in a class, the void method is quite reasonable if it models some โ€œbehaviorโ€ that makes sense in the context of the class. For example, if you have a SpecialSortedList that sorts its contents, i.e. myList.sort (), the sort () method will be invalid because it models some behavior related to SpecialSortedList.

If the method does not work with local data inside the class, that is, it excludes its data through parameters and returns part of the data (i.e. does not rely on local data), it is good practice to declare it static and even move it to a utility or auxiliary class.

+2
source share

In my opinion, it is not necessary to always return something along the chain of actions. It completely depends on your requirements. Ie in the login method authentication is better to return a boolean result for simplification. If the chain of actions does not depend on one or the other, then, in my opinion, there is no need to return what will not be used by any action. Therefore, if the result of one action affects the next action, it should return something for simplification and testing so that you can directly process the results of the method that causes the problems. Once the method is identified, you can check the specific method that is identified. So basically it all depends on your requirements. If your requirements are better handled without returning something, you should go with it. You should also follow best practices or design patterns for efficiency that are best for all situations, and you can get rid of the mess at the end. This is just my opinion.

+2
source share

More and more, I prefer to return something like a status code instead of void. This does a few things:

  • I let the code called your function know what to do next (for example, if the operation failed, then the calling code may need to do something else if the operation is successful).
  • Easier to test and debug (as you mention). I began to believe that writing code for verification is one of the best ways to ensure the quality of the code (assuming, of course, that you really test it). Void functions are hard to verify.

My preference is that, when possible, most functions accept parameters, return a response, and do not change any class variables. As Jacquot noted, they can be declared static and even transferred to the utility class. You can then have several controller functions using functions that return values.

0
source share

I would divide the answer into two parts:

  • When "void" returns it is useful -

    If the method returns a value only to indicate an invalid state / exception, then it should return "empty". An exception should take care to handle all error scenarios, and not say a "boolean" return value . Now the calling code needs to check both the logical and the exception again. If he chose only Exception, then for the calling code is much simpler and more understandable.

  • When void returns are not so useful -

    If the method performs any operation and returns a value as a result, then "void" should not be returned, the corresponding type of result should be returned (logical / integer / object, etc.).

In general, the return value should really correspond to whether the method returns something or not. It should not correspond to whether the method was executed correctly (well, it should). All input / exception validation scripts should be handled by throwing appropriate exceptions. For example, throw an IllegalArgumentException when an input is invalid, instead of returning a "boolean", the utterance method did not execute.

0
source share

All Articles