It's quite common to end up writing methods that take booleans to say whether certain functionality should be switched on or off. It would be nicer to use enumerations, not least because that way you couldn't end up getting them the wrong way around.
Something like:
void DoSomething(Thing thing, bool withLogging, bool withRollBack){}which would
(
Read more... )
Comments 17
https://github.com/dotnet/roslyn/issues/3497
Reply
DoSomething(thing, logging:True, rollback:False)
That's one of the things in C# which I wish C++ would hurry up and have properly.
Of course, anywhere where you do want more than two options, your enum syntax would be useful. Especially if, for the rare cases where you need it, you could use that as a variable type. And if there was a syntax that prevented the enum polluting any namespaces outside the function.
Reply
Reply
Reply
Named parameters are a big plus for me but I guess some people see it as incompatible with the style of their language. In C++ in fact I think there's no requirement that the names in a prototype and an implementation be the same so
class MyObject {
void wibble(int thisInt, int thatInt).
}
can by implemented as
MyObject::wibble(int thatInt, int thisInt);
And I imagine requiring parameter names be consistent in prototype and impl would break code? So I imagine there's scope for a lot of confusion.
Reply
So DoSomething(myThing, [logging => TRUE, rollback => FALSE]);
Reply
Reply
(The comment has been removed)
I suspect that any of the more complex approaches (including passing parameters down multiple layers) would be much better off declaring an enumeration normally.
Possibly what I want is just to enforce named parameters (or a simple mapping).
Reply
Reply
Leave a comment