Which syntax is better for the return value?

I was doing a massive code review, and one pattern I noticed everywhere is this:

public bool MethodName() { bool returnValue = false; if (expression) { // do something returnValue = MethodCall(); } else { // do something else returnValue = Expression; } return returnValue; } 

This is not how I would do it, I would simply return the value when I realized what it was. which of these two patterns is more correct?

I emphasize that the logic always seems structured so that the return value is assigned only in one cloak, and after its assignment the code is not executed.

+4
source share
13 answers

This section discusses a lengthy discussion.

+4
source

Many people recommend having only one exit point from your methods. The sample that you describe above follows this recommendation.

The main theme of this recommendation is that if you need to clear some memory or state before returning from a method, it is better to have this code in only one place. having multiple exit points leads to duplication of the cleaning code or potential problems due to the lack of a cleaning code at one or more exit points.

Of course, if your method lasts a couple of lines or doesn’t need any kind of cleanup, you can have multiple returns.

+3
source

I would use triple to reduce control structures ...

 return expression ? MethodCall() : Expression;
return expression ? MethodCall() : Expression; 
+2
source

I suspect that I will be in the minority, but I like the style presented in the example. Easily add a log statement and set a breakpoint, IMO. Also, with consistent use, it seems easier to “match the pattern” than to have multiple returns.

I am not sure that there is a “correct” answer to this question.

+2
source

Some educational institutions and books advocate a unified practice of return.

Whether it is better or not subjective.

+1
source

It looks like part of a poor OOP design. Perhaps it should be reorganized at a higher level than within a single method.

Otherwise, I prefer to use the ternary operator, for example:

 return expression ? MethodCall() : Expression; 

It is shorter and more readable.

+1
source

Return from the method immediately to any of these situations:

  • You have found a boundary condition and you need to return a unique or control value: if (node.next = null) return NO_VALUE_FOUND;
  • The required value / state is false, so the rest of the method is not applied (aka security point). For example: if (listeners == null) return null;
  • The purpose of the method is to find and return a specific value, for example: if (nodes[i].value == searchValue) return i;
  • You are in a section that returns a unique value from a method that is not used elsewhere in the method: if (userNameFromDb.equals(SUPER_USER)) return getSuperUserAccount();

Otherwise, it is useful to have only one return statement, so that it is easier to add a debug log, clear resources, and follow logic. First I try to handle all of the above 4 cases, if applicable, and then declare a variable called result(s) as late as possible and assign values ​​as needed.

+1
source

They perform the same task. Some say that a method should have only one entry and one exit point.

0
source

I use this too. The idea is that resources can be freed up in a regular program stream. If you exit the method in 20 different places and you need to call cleanUp () earlier, you will have to add another cleaning method 20 times (or reorganize everything)

0
source

I assume that the encoder accepted the construction of the definition of the toReturn object at the top of the method (for example, List <Foo> toReturn = new ArrayList <Foo> ();) and then filling it during the method call, and for some reason decided to apply it to a boolean return type that is odd.

It can also be a side effect of the coding standard, which states that you cannot return in the middle of the method body only at the end.

0
source

Even if the code is not executed after the return value is assigned, this does not mean that some code will not need to be added later.

This is not the smallest piece of code that could be used, but it is very refactoring friendly.

0
source

Delphi forces this template to automatically create a variable called Result, which will be of the type of the returned function. Regardless of the result, the Result when the function exits is your return value. Thus, there is no keyword "return".

 function MethodName : boolean; begin Result := False; if Expression then begin //do something Result := MethodCall; end else begin //do something else Result := Expression; end; //possibly more code end; 
0
source

The template used is detailed - but it is also easier to debug if you want to know the return value without opening the "Registers" window and without checking EAX.

0
source

Source: https://habr.com/ru/post/1277219/


All Articles