Me confundo con las funciones mín y máx, en ciertos contextos.
En un contexto, cuando estás usando las funciones para tomar el mayor o el menor de dos valores, no hay problema. Por ejemplo,
//how many autographed CD's can I give out?
int howManyAutographs(int CDs, int Cases, int Pens)
{
//if no pens, then I cannot sign any autographs
if (Pens == 0)
return 0;
//I cannot give away a CD without a case or a case without a CD
return min(CDs, Cases);
}
Fácil. Pero en otro contexto, me confundo. Si estoy tratando de establecer un máximo o mínimo, lo obtengo al revés.
//return the sum, with a maximum of 255
int cappedSumWRONG(int x, int y)
{
return max(x + y, 255); //nope, this is wrong
}
//return the sum, with a maximum of 255
int cappedSumCORRECT(int x, int y)
{
return min(x + y, 255); //much better, but counter-intuitive to my mind
}
¿No es aconsejable realizar mis propias funciones de la siguiente manera?
//return x, with a maximum of max
int maximize(int x, int max)
{
return min(x, max);
}
//return x, with a minimum of min
int minimize(int x, int min)
{
return max(x, min)
}
Obviamente, el uso de los componentes integrados será más rápido, pero para mí esto parece una microoptimización innecesaria. ¿Hay alguna otra razón por la que esto sería desaconsejable? ¿Qué hay en un proyecto grupal?