Does it subtract from readability / is it confusing to use non-optional method parameters in C# as if they were optional?

Actually the syntax with a named argument is recommended by MSDN exactly for readability: Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods and Named arguments also improve the readability of your code by identifying what each argument represents Also named arguments are not the same thing as optional arguments. Although there are cases where you need to use a named argument to call a method that has optional arguments, that is not always required and using a named argument does not by itself suggest that the argument is optional.

Actually the syntax with a named argument is recommended by MSDN exactly for readability: Named arguments free you from the need to remember or to look up the order of parameters in the parameter lists of called methods. And Named arguments also improve the readability of your code by identifying what each argument represents. Also named arguments are not the same thing as optional arguments.

Although there are cases where you need to use a named argument to call a method that has optional arguments, that is not always required and using a named argument does not by itself suggest that the argument is optional.

Thank you, I can survive a code review now! – Hamish Grubijan Mar 18 at 0:01 1 @Hamish I'm sure you will survive. It's quite rare to have casualties during a code review nowadays.

Not like in the old times! :) – Paolo Falabella Mar 18 at 0:09.

Make argument values like that self-documenting by using a enum type instead.

That could result in many one-time-use classes. What about integers? Could be an overkill.

– Hamish Grubijan Mar 17 at 23:52 Enums are pretty cheap if the domain of valid integer input values are small. If the concern is an explosion of files, I would suggest defining the enum in the same file as the class that will be using it, I don't consider that too heinous myself! Alas, enums are not as useful in C# as they are in Java, one of few deficiencies of C# in comparison with Java IMHO.

– James Webster Mar 17 at 23:58 1 An enum type isn't a class. Your compiler doesn't get tired of them and they take minimal resources. Integers are far worse, there are many more than 2 to choose from.

Use integers to count, not to label. – Hans Passant Mar 18 at 0:00.

Personally, I don't like injecting comments within a line of code this way. I think that does affect readability. I like to put higher level comments for blocks of code, explaining what is basically happening.

Stuff like the names of parameters can be found by holding the mouse over the method name. I really don't see the advantage of these type of comments.

I think your approach is a good way of documenting the meaning of parameters particularly if: You are calling an API either in the BCL or some other 3rd party API that you cannot redesign, and... There are a large number of parameters of the same or confusing types (e.g. A method that takes a dozen booleans that you are providing constant values for). Ideally confusing APIs wouldn't exist, but alas the world isn't perfect. You could also do: const bool mustEndInText = true; HelperClass.

OpenFileInNotepad("D:\\Temp\\" + fname, mustEndInTxt); but this would take an additional line so your approach is definitely more expressive with fewer characters.

Say, we have ClassA with method Foo containing an optional parameter. So, we can use it as shown in method DoFoo. Once I needed to pass it to another class ClassB.

First I tried to pass it as Action, but the signature surely didn't match. Then I passed it as Action, the signature matched, but the parameter in ClassB was no longer optional. But I did wanted to have it optional and came to an idea to declare a delegate.

So, it worked. So, the question is: can I somehow pass a method with an optional parameter as an argument without explicitly declaring a delegate and keep the optional quality of its parameters?

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions