Tuesday, September 12, 2006

I have to disagree with Don on the use of enums instead of booleans. Using an enum instead of a boolean may be more readable but you are bound to run into a true, false, maybe wtf.

Instead of a long parameter list I like to use a parameter object

So instead of:

string SomeStringManipulation(strInput, true, false, true, true, false)

I would use the following:

string SomeStringManipulation(StringManipulationArgs args)

Now if I want to change the number of arguments that are passed into the SomeStringManipulation function I only have to change it in one spot and the code is more maintainable.

Tuesday, September 12, 2006 6:26:38 PM (GMT Standard Time, UTC+00:00)  #    Comments [5]  | 
Wednesday, September 13, 2006 12:03:43 AM (GMT Standard Time, UTC+00:00)
in c#, you can use parameter array.
but it is not recommmended because it is not CLS compliant, and has effect on performance
ghassan
Wednesday, September 13, 2006 2:21:52 PM (GMT Standard Time, UTC+00:00)
This is my favored way of doing it as well - way easier to maintain. I still agree that in a strict case of boolean vs. enum arguments it's better in general to be using an enum simply for readability.

There are actually places where "nullable" booleans exist, too - so in some cases that enumeration is better to cover (yes, I know that raises other questions but before I write my own blog post...)
Wednesday, September 13, 2006 6:58:28 PM (GMT Standard Time, UTC+00:00)
"in c#, you can use parameter array.
but it is not recommmended because it is not CLS compliant, and has effect on performance"

But with an object you can explicitly set which variables you are expecting. I admit that the name of my object is poorly named and misleading!

"This is my favored way of doing it as well - way easier to maintain. I still agree that in a strict case of boolean vs. enum arguments it's better in general to be using an enum simply for readability."

But that readability introduces uncertainty. If I ask you a yes or no question and you say maybe, I didn't really get the answer I was looking for.
Tuesday, November 07, 2006 11:41:07 AM (GMT Standard Time, UTC+00:00)
From the Design Guidelines for Developing Class Libraries ( http://msdn2.microsoft.com/en-us/library/ms229039.aspx )

Do use enumerations if a member would otherwise have two or more Boolean parameters.

Enumerations add significant readability to member signatures. Consider the following method call:
Visual Basic

Type.GetType("Contoso.Controls.Array", True, False)

C#

Type.GetType("Contoso.Controls.Array", true, false);


Calls like this are very difficult to understand without checking the documentation or adding code comments. It is much easier to read a call that uses enumeration values in place of multiple Boolean values as demonstrated in the following code example.
Visual Basic

BetterType.GetType("Contoso.Controls.Array", _
ErrorOptions.ThrowOnError, _
CasingOptions.CaseInsensitive)

C#

BetterType.GetType("Contoso.Controls.Array",
ErrorOptions.ThrowOnError,
CasingOptions.CaseInsensitive);


Joshua McKinney
Tuesday, November 07, 2006 11:42:15 AM (GMT Standard Time, UTC+00:00)
Joshua McKinney
Comments are closed.

Theme design by Jelle Druyts

Pick a theme: