enzi
enzi

Reputation: 4165

Is it a bad idea to use an array with a single element instead of just a variable?

Are there performance disadvantages when I use a single-element array over a simple variable?

I have a base class from which many other classes are derived. This base class provides a Value property of type object.

Now I have to also support multi-value versions of most classes. Thus I was thinking about changing protected object value to protected object[] value and using the array even if only a single value is required.

I know the length of the array in the constructor, so in case of single-value instances, the length of the array is 1.

Since there ususally exist a lot of instances during runtime (up to several thousand, depending on number of users) I'm very concerned with performance.

So my question is: Will this change from simple variable to array cause a noticeable change in performance, or is it generally bad design?

I thought about creating a wrapper class that enables multiple values, but the classes are heavily connected via references and events, so this would be clumsy to use and maintain.

Edit To clarify:

The purpose of the base class is mainly to provide an interface. Other classes use the Value property, which is why it is defined in the base class. The variable itself was only defined in the base class so I don't have to define it in every deriving class (type is always object).

The multi-value extension is not required because there are deriving classes that need more than one value, but because there will be a context-sensitive environment (don't know how to phrase it better) and classes need to remember a value for every context.

For example, think of it as an iternationalization technique (not in my case, just as example).

A class represents a word and depending on the language, the Value is the word in the respective language.

By putting all the multi-value logic in the base class, deriving classes can continue to work like they always did (using the Value property) and the base class figures out which value to use.

Hope this makes things a bit clearer.

Upvotes: 0

Views: 368

Answers (6)

Anders Fjeldstad
Anders Fjeldstad

Reputation: 10834

Without having seen your code, I get a feeling that one could question the design. You have a base class that can hold one or more "values", of arbitrary type? Generally, I would use a base class to contain properties and/or functionality that are common to the deriving types, but if I notice that the base class tries to "fit all possible needs" of the deriving classes it's probably time to refactor. In this case, your base class to me seems a bit too general.

That being said, it's possible that your specific situation justifies the design. (And I don't think you would notice any performance degradation.)

Upvotes: 1

sehe
sehe

Reputation: 393674

Why is there a need for (a) a global base class (b) one that also provides variable storage?

This seems like ... unnecessary restriveness (or ... bad design if your prefer). I don't know any context, of course, but I suggest more along these lines:

public interface IValueProvider<out T> /* : IComparable<T>, IEquatable... etc */
{
    T Value { get; /*protected?*/ set; }
}

If you insist, you can have implementation helpers, like so:

public class ValueProviderClass<T> : IValueProvider<T> 
     where T : class, new()
{
     ValueProviderClass() { Value = new T(); }
}

Of course also structs can implement the interface, yielding even more room for performance optimization if the need arises in the future (however, value types will not be able to transparently derive from a 'base' valuetype because the CLR doesn't allow that)

Note the use of Covariance to get compatibility with IValueProvider, e.g.

Hope this idea helps you spark some more lean-and-mean thinking

Upvotes: 1

Greg B
Greg B

Reputation: 14898

Why not make the class generic and specify the type of Value when the class is instantiated?

Upvotes: 0

Casey Patton
Casey Patton

Reputation: 4101

No: the amount of overhead would certainly not be noticeable. If it reduces the code's complexity, then it is a good idea.

Upvotes: 4

Lucero
Lucero

Reputation: 60276

It is adding overhead, but I doubt that it will be noticeable under "normal" circumstances (e.g. not a in a tight loop).

Upvotes: 1

Oliver M Grech
Oliver M Grech

Reputation: 3171

I guess it would be more performance hungry to change an array into a single variable and vice versa. I wouldn't mind having an array with the possibility to expand with one value!

Upvotes: 1

Related Questions