Search code examples
programming-languagesdependency-injectionparameter-passing

Passing parammeters


Hi guys I have one question,

If I have a sequence of methods for example:

Main()
{
  Method1();
}

Method1()
{
  Method2();
}

Method2()
{
  Method3();
}

Method3()
{
  ObtainsUserPermission(httpContext.Current.User.Name);
}

How is the best way to do it, using the parammeter "httpContext.Current.User.Name" in the last Method3, or passing by parammeter in each method? Like this:

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

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

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

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

thank you for all.


Solution

  • This smells like magic parameters.

    A good rule of thumb is - if you wish your execution of method3() to depend on the name, pass the name as a parameter. In general, you shouldn't use globals inside functions. It can become complicated to debug and maintain. An exception is members of a class, in which case your members are visible inside the methods and there is no need to pass them as parameters.