Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define two methods with same parameter type

Today I ran into a scenario where I have to create a method that share the same name, params count and params types with existent one, Something like this:

public static Department GetDepartment(string departmentName)
{
  //LOGIC
}

public static Department GetDepartment(string employeeID)
{
  //LOGIC
}

at first glance I just said why not to name it with a different name and get things done, but I couldn't! I do want to maintain the readability of my code i'm working on, I want it to be overloaded to the first one,
so I said why not to add a fake parameter just to workaround this issue from the compiler Point of view.

 public static Department GetDepartment(string employeeID, object fakePassWtEver)
    {
      //LOGIC
    }

What is the best practice for this case? I see all the ways can let my code run, but none of them satisfied me

like image 902
Rami Alshareef Avatar asked Jul 28 '11 12:07

Rami Alshareef


1 Answers

You could update your method signatures and make your code more readable at the same time by doing something like the following.

public static GetDepartmentByName( string departmentName )

public static GetDepartmentByEmployeeId( string employeeId )

Personally I feel that adding verbosity to code helps others that come later understand what's going on. It also helps make your methods "read" more easily.

like image 131
Brian Dishaw Avatar answered Oct 21 '22 11:10

Brian Dishaw