在其他函数参数中调用函数很好吗? [英] Is good to call function in other function parameter?

查看:106
本文介绍了在其他函数参数中调用函数很好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  public static string abc()
{
returnabc ;
}

最好以这种方式调用此函数:

  string call = abc(); 
Console.writeline(call);

比这个吗?

  console.writeline(ABC()); 

是否有任何理由偏好另一个?

解决方案

两者都有效。但是,根据经验,我认为第一种方法更适合易读性和易维护性。作为调试会话的帮助,我无法计算从紧凑风格改为第一次风格的次数。

例如,这种风格使得很容易检查中间结果的正确性中间:

  string call = abc(); 
assert(!call.empty()); //只是一个例子。
Console.writeline(call);

此外,它有助于稍后使代码更健壮,在随后的操作之前添加条件检查检查调用的值,例如,如果设计不能保证前一个断言的条件成立,但你仍然需要检查它。

  string call = abc(); 
if(!call.empty())
{
Console.writeline(call);
}

请注意,使用这种风格,您将能够轻松检查在调试器中调用


I suppose this:

public static string abc()
{
    return "abc";
}

Is better to call this function in this way:

string call = abc();
Console.writeline(call);

Than this?

console.writeline(abc());

is there any reason to prefer one to the other?

解决方案

Both are valid. However, out of experience I have concluded that the first option is more suitable for readability and ease of maintenance. I can't count how many times I have changed from the "compact" style to the first one as a help for a debugging session.

For example, this style makes it easy to check the correctness intermediate of an intermediate result:

string call = abc();
assert(!call.empty()); // Just an example.
Console.writeline(call);

Also, it helps to make the code more robust later, adding a conditional check before the subsequent action that checks call's value, for example if the design does not guarantee that the condition of the previous assert holds but you still need to check it.

string call = abc();
if (!call.empty())
{
    Console.writeline(call);
}

Note also that with this style you will be able to easily inspect the value of call in your debugger.

这篇关于在其他函数参数中调用函数很好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆