Nested function calls. What is the best practice?

A small thing, but I will be glad to hear what other people think about it.

Which of the two code segments below is the best programming practice?

var results = GetResults(); SendResults(results); 

OR

 SendResults(GetResults()); 

I think the first option is better, but, on the other hand, option 2 is less than code for writing (and reading). What do you think?

I know this is a very simple question, but still ...

+4
source share
12 answers

it

 var results = GetResults(); SendResults(results); 

better because it is being debugged ... Try putting breakpoin on SendResults(results) and see the meaning of the results.

It is so important that in the next version of Visual Studio 2013, they add a way to see the return value of functions (see, for example, here )

This new function allows you to check the return value of a function when a developer transitions or exits a function during a debugging session. This is especially useful when the return value is not stored in a local variable. Consider the following example of a nested function Foo (Bar ()); in this example, now you can check the return values โ€‹โ€‹from Bar and Foo when moving along this line.

From a compiled perspective, they are usually the same. The only difference at the IL level is that the slot in the stack has some meta information with the variable name ( results ) or is nameless.

+3
source

I usually use the first option, because this way I can insert a breakpoint between calls to GetResults and SendResults .

Usually this is not such a big deal if the code is in the middle of the method, but if it is in the form:

  return Process(GetData()); 

the return values โ€‹โ€‹of both GetData and Process calls are not available. If we are not talking about a frame function that has no side effects and has obvious results (for example, int.Parse), I prefer the format:

 var data = GetData(); var result = Process(data); return result; 
+8
source

In my opinion, you should always strive for clarity, so I would prefer:

 // notice the type not var (unless it obvious) IEnumerable<MyClass> results = GetResults(); SendResults(results); 
+3
source

Personally, I prefer the former, at least when you are debugging, you can check the result of GetResults ().

I do not think that this is due to the practice of programming, but more personal.

+1
source
 var results = GetResults(); SendResults(results); 

This is acceptable because it allows you to use a breakpoint to verify the value of results . Some programming languages โ€‹โ€‹will not optimize this code, and as a result, checking the code may cause a warning that the results variable never changes. C# is likely to be optimized, so there is no problem.

Code indices may issue a warning for inefficient code for the following example;

  var results = GetResults(); return results; 

It depends on the language, and I don't think C# has a problem optimizing this.

+1
source

In my opinion, it does not really matter, but I have a slight preference for the first, because it makes it a little easier to read, and a little easier to expand and maintain. In my opinion, sequence is much more important. Do one or the other, but not both.

0
source

Both are fine ... it's just a matter of your programming style and experience ... But if I had to choose, I would go with option 1. More clear ...

0
source

The first principle when considering such a problem should be readability (if you do not deal with some optimization at a low level). In your simple example, readability is comparable, but consider this code:

 SendResults(GetResults(Sorter, context.GetCurrentPageInfo(userContext), ...); 

Of course, it would be much clearer:

 var results = GetResults(); var pageInfo = context.GetCurrentPageInfo(userContext); ... SendResults(results, pageInfo, ...); 

EDIT: as correctly indicated in other answers, a more readable version has another advantage - it is easier to debug, because you can view all the intermediate values.

0
source

Whatever you prefer, I think, but the first is probably easier to read for beginners or people less familiar with C #.

0
source

If you never need results elsewhere, my opponent is that the second option is better. It reads faster and really has less code to write. However, it is harder to read, so the programmer who reads your code should be better and faster at understanding things.

0
source

It really depends on the particular developer. This is a pretty subjective thing.

Personally, I prefer the first version, because then, if I need to, I can more easily execute the code in the debugger.

A more fanatical end to TDD (Test Driven Development) proponents will say that it doesnโ€™t matter because you will never use your debugger if you make TDD โ€œrightโ€.

I also prefer smaller lines, so if you have a method call that goes through the results of other method calls, and the parameter lists start to excessively exceed, it becomes very difficult to read

 SendResults(GetResults(arg0, arg1, arg2), SomeOtherMethod(arg3, arg4), arg5); 

It becomes very difficult to read and track everything.

Ultimately, your system and your system are supported, so any style you find is easier.

0
source

If the results are used this way, I will probably go for another solution:

 class SomeClass { private MyClass _results; } private void SendResults() { ... // Implementation which sets _results 

If SendResults receives the results directly from _results and GetResults is not required (unless it is used by other classes, then this will be:

 public MyClass Results { get; private set; } 
0
source

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


All Articles