0

For example, one might have a method max on an integer array.

public static int Max(int[] ints)
{
    var max = ints[0];
    foreach(var i in ints)
    {
        if(i > max)
        {
            max = i;
        }
    }
    return max;
}

But actually we didn't utilize the fact that were were using an array, just that we could iterate over them, and we didn't need ints, we just needed an object that knows how to compare itself. So a more powerful method might be:

public static T MyMax<T>(IEnumerable<T> ts) where T : IComparable<T>
{
    var max = ts.First();
    foreach(var i in ts)
    {
        if(i.CompareTo(max) > 0)
        {
            max = i;
        }
    }
    return max;
}

And I guess the above discounts the fact that the IEnumerable<T> could be infinite which would cause this to hang indefinitely, but then why are you trying to find the Max fo an infinite iterable in the first place.

The point I'm trying to make is that the second method is much more powerful than the former, as it makes the least assumptions about it's parameters as needed. Is there a name for such a principle? Is this even a thing?

Thanks in advance.

user420667
  • 6,552
  • 15
  • 51
  • 83
  • general purpose, generic, etc. – sstan Jun 02 '16 at 00:09
  • @sstan: While I agree the result includes generics or is more general purpose, I'm wondering if there isn't a name for the principle where one would want to actively seek the most generic solution possible. I think even this could be made more generic as we could just use the Aggregation operator with Max passed in. – user420667 Jun 02 '16 at 00:18
  • First linq implements the same method, but considering other things too. I think you should have a look to [it](http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,b7b87afe6c3fc715), search for (Max). I don't know about a pattern name for this approach. I think this is very close to functional language or declarative programming, but not the way this function is written, but more in the way it is going to be used, if that make sense. – Paulo Prestes Jun 02 '16 at 00:44
  • @PauloPrestes: Thanks! Hm.. interesting that it uses Conmparer.Default, that's even more general! – user420667 Jun 02 '16 at 00:53
  • [Polymorphism](https://en.wikipedia.org/wiki/Polymorphism_(computer_science))? – Damien_The_Unbeliever Jun 02 '16 at 14:22

1 Answers1

1

I would call this Programming to an Interface.

The design principle of Programming to an Interface is most commonly mentioned in the context of declaring variables, and that's how the famous Gang of Four book introduced the principle; but it applies equally well to declaring method parameters.

Don't declare variables to be instances of particular concrete classes. Instead, commit only to an interface defined by an abstract class. You will find this to be a common theme of design patterns...

The term variables here can be considered in a broad sense that includes method parameters.

jaco0646
  • 15,303
  • 7
  • 59
  • 83