c# – 嵌套函数调用 – 什么是最佳实践?

一件小事,但我很乐意听到其他人的想法.

以下2个代码段中哪一个是最佳编程实践?

var results = GetResults();

SendResults(results);

要么:

SendResults(GetResults());

我认为第一个选项更好,但另一方面,选项2是更少的代码来写(和读).你怎么看?

我知道这是一个非常基本的问题,但仍然……

解决方法

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

更好,因为它是可调试的…尝试在SendResults(结果)上添加一个breakpoin并观察结果的值.

这非常重要,在Visual Studio的下一个版本中,2013年他们正在添加一种查看函数返回值的方法(参见例如here)

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()); in this example you can Now examine the return value(s) from Bar and Foo,when you step over that line.

从编译的角度来看,它们通常是相同的. IL级别的唯一区别是堆栈中的一个插槽具有一些带有变量名称的元信息(结果)或者是无名的.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...