1

I have a function that takes in a bool, shown below:

public void LoadEndPoints(bool mock)
{

}

I can call this via LoadEndpoints(true) or LoadEndpoints(false), but this can be a bit hard to understand, as you need to know what true/false represents. Is there a way to pass the parameter name and value to a function such as LoadEndPoints(mock = true)?

6
  • You should comment your code to make that clear. You should use XML comments, which will be represented in the intellisense to ensure you and future developers will understand. Commented Apr 26, 2013 at 20:00
  • I don't understand why using "named parameters" would help here.. and there are so many answers relating to such. Commented Apr 26, 2013 at 20:02
  • Perhaps: 1) Use discrete method names: LoadEndPoints/LoadMockedEndPoints or; 2) Take in a enumeration (or other type) that conveys more meaning: LoadEndPoints(EndpointMode.MockOnly) or; 3) Move the configuration to a [class-level] setting: var epl = new EndPointLoader { Mock = true }; epl.LoadEndPoints(); etc. Commented Apr 26, 2013 at 20:02
  • Yes, I agree, your code should be more readable. Commented Apr 26, 2013 at 20:04
  • 2
    I wouldn't call this code "non-readable" although there might be better ways to express the high-level end goal. The method-level documentation should explain the mock parameter. Perhaps the name could be expanded (e.g. mockOnly), but otherwise I think it's generally "ok". There are many methods that take bool types and it's up to the caller to know how to call the method correctly. Commented Apr 26, 2013 at 20:05

5 Answers 5

3

Yes!

You can specify the parameter names like this:

myObject.LoadEndPoints(mock: true);

Further Reading

Another way to improve readability of your code would be to use an enum, like this:

public enum LoadOption
{
    Normal,
    Mock
}

public void LoadEndPoints(LoadOption option)
{
    ...
}

Then the call would look a bit like this:

myObject.LoadEndPoints(LoadOption.Mock);
Sign up to request clarification or add additional context in comments.

4 Comments

@MustafaEkici OP specifically asked know how to make the code more readable by specifying the parameter name as well instead of just the parameter. The way I read the question this is exactly what OP is asking for. That said, aquaraga's answer is good too.
@MustafaEkici What do you mean? I've showed how it can be done and linked to detailed references. aquaraga's answer listed the same solution.
its different not same.. check your refrence example, there is weight and height right? they are already defined. so think that i call that method with parameter 30 (parameter means Age or can be everything) so how you get that name (Age) in the function?
@MustafaEkici and what would a parameter named 'age' do? Why would you expect to be able to pass any parameter with any name to any function that doesn't define what that parameter is or does? I'm sorry, but I fail to see why that would be a criticism of this technique.
2

You could use 'Named arguments', a C# 4.0 feature; and thus call: myObject.LoadEndPoints(mock : true);

If readability is indeed your prime concern, you could even expose two explicit methods, and internally reuse the logic - something similar to:

    public void LoadEndPointsWithoutMock()
    {
        LoadEndPoints(false);
    }
    public void LoadEndPointsByMocking()
    {
        LoadEndPoints(true);
    }
    private void LoadEndPoints(bool mock)
    {

    }

Also, I wouldn't say that LoadEndPointsWithoutMock, etc. are great method names. Ideally, the names should have something to do with the domain.

Comments

1

You can use a KeyValuePair:

   KeyValuePair kvp = new KeyValuePair(BoolType, BoolValue)

2 Comments

What would a call to the method look like, using KeyValuePair?
The other answers are better, but LoadEndpoints would take a KeyValuePair instead of a bool
0

Yes, you can do it with the following syntax in c#:

myObject.LoadEndPoints(mock : true);

And in VB:

myObject.LoadEndPoints(mock := true)

Comments

0

Use named parameters. Have a look at this Named and Optional Arguments

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.