开发者

How to name a class that wraps several primitive types?

I have a naming problem for some of my classes. I need to wrap some primitive .net types into a class like the following. There will be about 20 of such classes.

(The naming is crap, of course. Just for a demonstrative purpose)

public class Int32Single
{
    public int Value { get; set; }
}

public class Int32Double
{
    public int Value1 { get; set; }

    public int Value2 { get; set; }

}

public class DoubleSingle
{
    public double Value { get; set; }
}

I can't use a generic approach for this. How should I name such wrapper classes, where each class name should provide the necessary information which primite types are wrapped and in which quantity?

It migh开发者_JS百科t also be possible that I have class that contains mixed primite types.


This doesn't seem like a very good idea at all. You have both the Tuple class and a standard array available, that both make more sense in any conceivable use case. However, that doesn't answer your question, so...

The most intuitive name for a wrapper class would follow the convention of {type-name}Wrapper, or for example, Int32Wrapper. In your case, the wrapped object is a primitive type, so makes sense to call the class a "Tuple". Since you want to specify the size of the Tuple in your class name, {primitive-type-name}{size}Tuple seems like the most intuitive naming convention but this causes several problems.

  • The natural language used to describe Tuples create ambiguity (such as Single and Double because they conflict with the Type names). (e.g. DoubleDouble is bad)
  • Integers are used in the naming of some primitive types so this could cause ambiguity. (e.g. Int322Tuple is bad).

We can't move the size to the beginning such as 2Int32Tuple because integers are not valid characters to begin a class name. So, There are two approaches that I think could work.

I think your best bet to get around these constraints, is to use a {primitive-type-name}{text-represented-size}Tuple convention. (e.g. Int32TwoTuple or DoubleTwoTuple). This convention expresses the contents of the wrapper class without ambiguity, so it seems like a good approach. In addition the name begins with the primitive type name, so, if you have a lot of these classes, it will be easier for your IntelliSense to fill in the correct class name, and it will alphabetically be listed next to the primitive type that is being wrapped.


Generics can help you out here:

public class WrapTwo<T>
{
    public T Value1 { get; set; }
    public T Value2 { get; set; }
}

public class WrapOne<T>
{
    public T Value1 { get; set; }
}

And have you considered the Tuple class?


OneInt32, TwoInt32s, TwoDoubles? Doesn't sound great.

Tuples? http://www.dotnetperls.com/tuple


I don't very fond of Tuples or arrays, because both don't tell much about their purpose. Well, I use them. But mostly as internal members of classes, local variables, or with 3rd party/legacy code.

Back to naming. Compare:

Tuple<int,int> a = Tuple.Create(10,10);
Int32Double b = new Int32Double(15, 15);
WrapTwo<int> c = new WrapTwo<int>(20, 20);

With

Point a = new Point(10, 10);
Vertex b = new Vertex(15, 15);

One can argue, that 'a' is not good name for variable (and suggest to use 'pointA' instead). But I think it's pretty good in context of geometry application.

Not just type name and creation code looks obscure, but consider type fields names:

a.X = 20;
b.Value1 = 20;

So, I think you need some self-descriptive type in context of your application domain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜