c# - Is it a principle to always have the most abstract parameters and if so does it have a name? -
for example, 1 might have method max on integer array.
public static int max(int[] ints) { var max = ints[0]; foreach(var in ints) { if(i > max) { max = i; } } return max; }
but didn't utilize fact were using array, iterate on them, , didn't need ints, needed object knows how compare itself. more powerful method might be:
public static t mymax<t>(ienumerable<t> ts) t : icomparable<t> { var max = ts.first(); foreach(var in ts) { if(i.compareto(max) > 0) { max = i; } } return max; }
and guess above discounts fact ienumerable<t>
infinite cause hang indefinitely, why trying find max fo infinite iterable in first place.
the point i'm trying make second method more powerful former, makes least assumptions it's parameters needed. there name such principle? thing?
thanks in advance.
i call programming interface.
the design principle of programming interface commonly mentioned in context of declaring variables, , that's how famous gang of 4 book introduced principle; applies equally declaring method parameters.
don't declare variables instances of particular concrete classes. instead, commit interface defined abstract class. find common theme of design patterns...
the term variables here can considered in broad sense includes method parameters.