J
j0mbolar
I would like to see what the majority of people prefer
when it comes to elegance, clarity, etc.
I'll present the problem first based on
having a product, and one product only, at a
given time and then for each product,
when one is chosen, we call a generic
product function.
So in the case that we have no product,
we don't want to do anything but when we do
we want to rely upon a single function that
is applied to all products.
#define APPLE (1<<0)
#define ORANGE (1<<1)
#define PEAR (1<<2)
#define PLUMB (1<<3)
int main(void)
{
int product = APPLE; /* start with an apple */
do_something(product);
return 0;
}
variations follow:
[1]
void do_something(int product)
{
int items = (APPLE | ORANGE | PEAR | PLUMB);
if(product & items)
generic_func();
}
[2]
void do_something(int product)
{
if((product == APPLE) || (product == ORANGE)
|| (product == PEAR) || (product == PLUMB))
generic_func();
}
[3]
void do_something(int product)
{
switch(product) {
case APPLE:
case ORANGE:
case PEAR:
case PLUMB: generic_func();
break;
}
}
or perhaps you would go about it in a completely
different way? and if so, which way?
personally, I can't stand [2] and think it is vile.
when it comes to elegance, clarity, etc.
I'll present the problem first based on
having a product, and one product only, at a
given time and then for each product,
when one is chosen, we call a generic
product function.
So in the case that we have no product,
we don't want to do anything but when we do
we want to rely upon a single function that
is applied to all products.
#define APPLE (1<<0)
#define ORANGE (1<<1)
#define PEAR (1<<2)
#define PLUMB (1<<3)
int main(void)
{
int product = APPLE; /* start with an apple */
do_something(product);
return 0;
}
variations follow:
[1]
void do_something(int product)
{
int items = (APPLE | ORANGE | PEAR | PLUMB);
if(product & items)
generic_func();
}
[2]
void do_something(int product)
{
if((product == APPLE) || (product == ORANGE)
|| (product == PEAR) || (product == PLUMB))
generic_func();
}
[3]
void do_something(int product)
{
switch(product) {
case APPLE:
case ORANGE:
case PEAR:
case PLUMB: generic_func();
break;
}
}
or perhaps you would go about it in a completely
different way? and if so, which way?
personally, I can't stand [2] and think it is vile.