Programming languages 通过参数

Programming languages 通过参数,programming-languages,dependency-injection,parameter-passing,Programming Languages,Dependency Injection,Parameter Passing,嗨,伙计们,我有一个问题 如果我有一系列方法,例如: Main() { Method1(); } Method1() { Method2(); } Method2() { Method3(); } Method3() { ObtainsUserPermission(httpContext.Current.User.Name); } 在最后一个方法3中使用parameter“httpContext.Current.User.Name”,或者在每个方法中通过parameter

嗨,伙计们,我有一个问题

如果我有一系列方法,例如:

Main()
{
  Method1();
}

Method1()
{
  Method2();
}

Method2()
{
  Method3();
}

Method3()
{
  ObtainsUserPermission(httpContext.Current.User.Name);
}
在最后一个方法3中使用parameter“httpContext.Current.User.Name”,或者在每个方法中通过parameter传递,最好的方法是什么?像这样:

Main()
{
  Method1(httpContext.Current.User.Name);
}

Method1(string name)
{
  Method2(name);
}

Method2(string name)
{
  Method3(name);
}

Method3(string name)
{
  ObtainsUserPermission(name);
}

谢谢大家。

这闻起来像魔法参数

一个好的经验法则是-如果希望method3()的执行依赖于名称,请将名称作为参数传递。一般来说,不应该在函数中使用全局变量。调试和维护可能会变得复杂。一个例外是类的成员,在这种情况下,您的成员在方法中是可见的,不需要将它们作为参数传递