Is good to call function in other function parameter?

后端 未结 3 1827
囚心锁ツ
囚心锁ツ 2021-01-16 08:13

I suppose this:

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

Is better to call this function in this way:

string call         


        
3条回答
  •  自闭症患者
    2021-01-16 09:11

    Given your exact example (one parameter, value not used elsewhere, no side effects), it's just a matter of style. However, it gets more interesting if there are multiple parameters and the methods have side effects. For example:

    int counter;
    
    int Inc() { counter += 1; return counter }
    
    void Foo(int a, int b) { Console.WriteLine(a + " " + b); }
    
    void Bar()
    {
        Foo(Inc(), Inc());
    }
    

    What would you expect Foo to print here? Depending on the language there might not even be a predictable result. In this situation, assigning the values to a variable first should cause the compiler (depending on language) to evaluate the calls in a predictable order.

提交回复
热议问题