1

I have an object that wraps an Either<T,T[]>. It can only be either a single instance of T or an array of T[] but I don't know at compile time. What do I name the class, and what do I name an instance for example Either<string,string[]>? I want to describe it as short as possible.

//class name
SingularOrArray //too long
SingleOrMultiple //too long
AnyOfT //not descriptive enough

//instance name (string)
SingleOrMultipleString //too long
StringOrStrings //clumsy
AnyString //not descriptive enough
  • 2
    How do you plan to use that object? That could help find some ideas for naming. E.g. like composite pattern? – Bernhard Hiller Sep 17 '18 at 8:34
  • 7
    What @BernhardHiller said, from this description the only thing we know it does would indeed be "Either". If the Either has some other behaviour depending on the amount of elements, then that behaviour might give you a good name. However, I'm rather thinking this might be an X Y Problem. After all, an array with only one element is still a valid array and I can't think of anything Either<> could do that an array couldn't with if(stringArray.Length == 1). – R. Schmitz Sep 17 '18 at 9:03
  • Indeed - if the difference between one and mutliple contained elements does not affect behavior, it shouldn't be mentioned in the name. In this case, it would be an implementation detail the user should not need to care about. – Hulk Sep 17 '18 at 9:37
  • 2
    its a pity you cant use () in names otherwise object(s) child(ren) maybe you could use underscore to represent the convention child_ren? – Ewan Sep 17 '18 at 10:43
  • 1
    Your naming constraints are pretty severe. The difference between your "not descriptive enough" example and your "too long" examples is nine characters. – Robert Harvey Sep 17 '18 at 14:37
1

It can only be either a single instance of T or an array of T[] but I don't know at compile time

You don't need to have both cases in your class then. Instead abstract their behaviour to the interface and have two implementations of that interface. One for single object and one for an array.

public interface IDataOf<T>
{
    void Match(Action<T> match);
    void Match(Action<T[]> match);
}

public class SingleOf<T>
{
    private readonly T _data;

    public SingleOf(T data) => _data = data;

    void Match(Action<T> match)
    {
        match(_data);
    }

    void Match(Action<T[]> match)
    {
        // do nothing or throw an exception
    }
}

public class MultipleOf<T>
{
    private readonly T[] _data;

    public MultipleOf(T[] data) => _data = data;

    void Match(Action<T> match)
    {
        // do nothing or throw an exception
    }
    void Match(Action<T[]> match)
    {
        match(_data);
    }
}   

Example of usage:

Action<string> singleMatch = item => { };
Action<string[]> multipleMatch = items => { };

var text = new[] { "one", "two", "three" };
IDataOf<string> lines = new MultipleOf<string>(text);
lines.Match(multipleMatch);

var anotherText = "only one";
IDataOf<string> sentence = new SingleOf<string>(anotherText);
sentence.Match(singleMatch);

Names of the classes should be based on their behavior.
Names of the variables should be based on their usage, easy to read and understand in the current context.

If you still want to have class with both options, name SingularOrArray is good enough and is not too long.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.