简体   繁体   English

嵌套函数调用 - 什么是最佳实践?

[英]Nested function calls - What's the best practice?

A small thing, but I will be happy to hear what other people think about it. 一件小事,但我很乐意听到其他人的想法。

Which of the 2 code segments below is the best programming practice? 以下2个代码段中哪一个是最佳编程实践?

var results = GetResults();

SendResults(results);

OR: 要么:

SendResults(GetResults());

I think that the first option is better, but on the other hand option 2 is less code to write (and read). 我认为第一个选项更好,但另一方面,选项2是更少的代码来写(和读)。 What do you think? 你怎么看?

I know it's a very basic question, but still... 我知道这是一个非常基本的问题,但仍然......

I usually go for the first option, because that way I can insert a breakpoint between the invocations of GetResults and SendResults . 我通常会选择第一个选项,因为这样我可以在GetResultsSendResults的调用之间插入一个断点。

It's usually not that big of a deal, if the code is in the middle of a method, but if it's in the form of: 如果代码位于方法的中间,通常不是那么大的交易,但如果它是以下形式:

 return Process(GetData());

the return values of both the GetData and the Process calls are not readily visible. GetDataProcess调用的返回值不容易看到。 Unless we are talking of a framework function that has no side effects and has obvious results (eg int.Parse) I prefer the format: 除非我们讨论的是没有副作用且结果明显的框架函数(例如int.Parse),我更喜欢这种格式:

var data = GetData();
var result = Process(data);
return result;

This 这个

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

is better because it's debuggable... Try putting a breakpoin on SendResults(results) and watch the value of results. 更好,因为它是可调试的...尝试在SendResults(results)SendResults(results)一个breakpoin并观察结果的值。

This is so much important that in the next version of Visual Studio , the 2013 they are adding a way to see return value of functions (see for example here ) 这非常重要,在Visual Studio的下一个版本中, 2013年他们正在添加一种方法来查看函数的返回值(例如请参见此处

This new feature allows you to examine the return value of a function when the developer steps over or out of a function during your debugging session. 此新功能允许您在开发人员在调试会话期间跨越或退出函数时检查函数的返回值。 This is especially useful when the returned value is not stored in a local variable. 当返回值未存储在局部变量中时,这尤其有用。 Consider the following nested function example Foo(Bar()); 考虑以下嵌套函数示例Foo(Bar()); in this example you can now examine the return value(s) from Bar and Foo, when you step over that line. 在这个例子中,当你跨过那一行时,你现在可以检查Bar和Foo的返回值。

From a compiled perspective they are normally the same. 从编译的角度来看,它们通常是相同的。 The only difference at the IL level is that a slot in the stack has some metainformation with the name of the variable ( results ) or is nameless. IL级别的唯一区别是堆栈中的一个插槽具有一些带有变量名称的元信息( results )或者是无名的。

In my opinion you have to always strive for clarity, so I'd much rather have: 在我看来,你必须始终努力清晰,所以我宁愿:

// notice the type not var (unless it's obvious)
IEnumerable<MyClass> results = GetResults();

SendResults(results);

The first principle when dealing with such issue should be readability (if not dealing with some low-level optimization). 处理此类问题时的第一个原则应该是可读性(如果不处理某些低级优化)。 In your simple example readability is comparable, but consider code like this: 在您的简单示例中,可读性具有可比性,但请考虑以下代码:

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

Surely, much clearer would be: 当然,更清楚的是:

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

As correctly noted in other answers, the more readable version has one more advantage - it is easier to debug, because you can examine all intermediate values. 正如在其他答案中正确指出的那样,更易读的版本还有一个优点 - 它更容易调试,因为您可以检查所有中间值。

Personally I prefer the first one, at least when you're debugging you can examine the result of GetResults(). 我个人更喜欢第一个,至少在你调试时你可以检查GetResults()的结果。

I don't think it's got anything to do with programming practice but more personal style. 我不认为它与编程实践有任何关系,而是更多的个人风格。

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

Is acceptable because it allows for a breakpoint to be used to inspect the value of results . 是可以接受的,因为它允许使用断点来检查results的值。 Some programming languages won't optimized this code, and as a result code inspection might issue a warning that the variable results is never modified. 某些编程语言不会优化此代码,因此代码检查可能会发出警告,表明变量results永远不会被修改。 C# likely will optimize so there is no problem with. C#可能会优化,所以没有问题。

Code inspectors might 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 with optimizing that. 这取决于语言,我不认为C#有优化它的问题。

If you never need the results anywhere else, my opninion is that the second option is the better. 如果你从来没有在其他任何地方需要结果,我的意见是第二种选择更好。 It reads faster and is indeed less code to write. 它读取速度更快,写入的代码确实更少。 However, it is harder to read, so the programmer who is reading your code should be better and understand things faster. 但是,它更难以阅读,因此正在阅读代码的程序员应该更好并更快地理解事物。

This is really up to the individual developer. 这完全取决于个人开发人员。 It is quite a subjective thing. 这是一个非常主观的事情。

Personally, I prefer the first version because then, if I need to, I can more easily step through the code in the debugger. 就个人而言,我更喜欢第一个版本,因为如果需要,我可以更轻松地逐步调试调试器中的代码。

At the more fanatical end of proponents of TDD (Test Driven Development) would say that it doesn't matter because you'll never use your debugger if you're doing TDD "right". 在TDD(测试驱动开发)的支持者更狂热的结尾会说它没关系,因为如果你正在做TDD“正确”,你将永远不会使用你的调试器。

I also prefer smaller lines, so if you have a method call that passes in the results of other method calls and the parameter lists start getting excessive it becomes very difficult to read 我也喜欢较小的行,所以如果你有一个方法调用传入其他方法调用的结果并且参数列表开始变得过多,那么它就变得非常难以阅读

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

That begins to get quite difficult to read and keep track of everything. 这开始变得非常难以阅读和跟踪所有事情。

Ultimately, it is your system and yours to maintain, so what ever style you find easier. 最终,它是你的系统和你的维护,所以你发现的风格更容易。

If the results are used in this way, I probably would go for another solution: 如果以这种方式使用结果,我可能会寻求另一种解决方案:

class SomeClass
{
    private MyClass _results;
}

private void SendResults()
{
   ... // Implementation which sets _results

Where SendResults gets the results directly from _results and GetResults is not needed (unless it is used by other classes, so it would be: SendResults直接从_results获取结果,而不需要GetResults(除非它被其他类使用,所以它将是:

public MyClass Results { get; private set; }

I know this is pretty old, but with Visual Studio 2017, the argument no longer needs to be based on ease of debugging. 我知道这已经很老了,但是使用Visual Studio 2017,这个参数不再需要基于调试的简易性。 There is a new way of handling nested calls. 有一种处理嵌套调用的新方法。 Set your breakpoint on the line with the nested call. 使用嵌套调用在行上设置断点。 When the breakpoint hits, you can step into any level of the nesting using the Step Into Specific context menu (right-click) command. 当断点命中时,您可以使用Step Into Specific上下文菜单(右键单击)命令步入任何级别的嵌套。 This will show the calls top to bottom from innermost to outermost. 这将显示从最里面到最外面的从上到下的调用。 This includes lambda functions (such as LINQ predicates). 这包括lambda函数(例如LINQ谓词)。

在此输入图像描述

In my opinion it doesn't matter much, but I have a small preference for the first, because it makes it slightly easier to read, and slightly easier to extend and maintain. 在我看来它并不重要,但我对第一个有一个小的偏好,因为它使它更容易阅读,并且稍微更容易扩展和维护。 Much more important is consistency in my opinion. 更重要的是我认为的一致性。 Do one or the other, but not both. 做一个或另一个,但不是两个。

两者都可以......这只是你的编程风格和经验的问题......但是如果我不得不选择我会选择选项1.更清楚......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在调用Powershell Cmdlet之间保持状态的最佳实践是什么? - What is the best practice for keeping state between calls to Powershell Cmdlets? 发送消息的最佳做法是什么 - What's the best practice to send a message 使用 Visual Studio 调试器,在一行上评估多个函数调用的最佳方法是什么? - With the Visual Studio debugger, what's the best way to evaluate multiple function calls on one line? 使用 switch 语句的最佳实践是什么? 嵌套? 具体的? - What would be the best practice for using switch statements? nested? specific? 限制并发呼叫的最佳做法 - Best practice to throttle concurrent calls 针对第三方dll进行编程的最佳实践是什么? - What's the best practice for programming against a 3rd party dll? 在MVC中以简单的层次结构构建模型,最佳实践是什么? - Structuring models in a simple hierarchy in MVC, what's the best practice? 触发手动OnClick事件的最佳做法是什么? - What's the Best Practice for Firing Manual OnClick Events? 实体框架中多个“包含”的最佳实践是什么? - What is the best practice for multiple “Include”-s in Entity Framework? 在 C# 中表示 Time 对象的最佳实践是什么? - What's best practice to represent a Time object in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM