HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Is ToEnumerable<T> for multiple items a good practice?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
practicetoenumerableitemsformultiplegood

Problem

In the similar vein as ToIEnumerable and FromSingleItem

Is this a good improvement for making lists on the fly?

SyntaxSugar.ToIEnumerable("test1","test2","test3");


vs.

new []{"test1","test2","test3"};


Code:

public static class SyntaxSugar
{
    public static IEnumerable ToIEnumerable(params  T[] items)
    {
        return items;
    }
}


not technically an extension to prevent T clutter

Cons:

  • longer codefragment



Pros:

  • more finger friendly- no [] or {}



  • Encapsulates the collection away from being treated as an array.

Solution

As developers we're pretty used to where the keys for all kinds of brackets, braces and parenthesis are and while I agree you may save (a negligible amount of) twists and turns of the wrist, you're just substituting keystrokes for keystrokes, IMO.

You've already made me (as a colleague) have to remember this exists, as opposed to what I know naturally within the language itself; I've no aversion to such a thing, obviously it is part of our job to build on things with the language, but only in cases where it is a concern - this really isn't a concern. Also, I now have to potentially type even more to get my list.

Or is it a concern?..

With all the other keystrokes required from day-to-day second-to-second that utilise modifier keys I think we can rule out the 'saving' this seems to want to achieve, when you look at it relatively.

On the other hand, for the most part we use a powerful IDE (I pity you if this isn't the case), any modern IDE will give you many shortcuts for productivity (such as Visual Studio's snippet insertion, Intellisense and auto-completion) which means we don't need to create unique constructs for such trivial concerns. Sure, it also means your construct can be utilised just as quickly, but becomes redundant as it isn't necessarily quicker. The argument could then be put forth that using these tools stands to mitigate human error.

And, what's wrong with an array? If you don't want something to be treated as an array, use a collection of different sorts.

Context

StackExchange Code Review Q#1863, answer score: 5

Revisions (0)

No revisions yet.