I have the following class, this class like many rely one parameters coming in as a pair. Originally for convenience, I set them as params Object[] values
and check if there is an even number of them if (values % 2 == 0)
.
Example Code:
using RVD = System.Web.Routing.RouteValueDictionary;
/// <summary>
/// Allows for quick building of similar RVDs.
/// </summary>
public class RVDBuilder
{
public RVD Template { get; protected set; }
public RVDBuilder(RVD template)
{
this.Template = template;
}
public RVDBuilder(params Object[] routeValues)
{
if (routeValues.Length % 2 != 0)
{
throw new Exception("parameters must come in pairs of two!");
}
}
}
I can see two point of views here:
- This adds great convience for the programmer: i.e.
new RVDBuilder("Key1", 1, "Key2", "2", ...);
- This is not as safe as it could be: i.e.
new RVDBuilder(Tuple.Create("Key1", 1), ...);
I was hoping to get input on this subject/topic.
- Should I force the programmer to use a 'pair' class? (How would this affect coupling?)
- Should I make it safer even though It'll be an easy fix/catch if an error is made?
- Is throwing an exception the best way to handle it or should I just truncate?
- Would adding an event so the programmer can choose how to handle the situation be good practice?
Overall though, how do I identify and decide on a method for handling this situation? I know overall it depends on the setting of the project, but there's not really a mood/convention set for it on this subject. Are there any general rule-of-thumbs?
(Sorry if this isn't quite the right StackExchange for this question. It involves code, but I figured since it's purely for example to help better explain my design question it fit best here).
Aucun commentaire:
Enregistrer un commentaire