opinions on logical OR variation
From: j0mbolar (j0mbolar_at_engineer.com)
Date: 09/28/04
- Next message: Ramesh: "#include optimization"
- Previous message: Ivan A. Kosarev: "Re: Obtain sizeof struct element using offsetof()?"
- Next in thread: Dan Pop: "Re: opinions on logical OR variation"
- Reply: Dan Pop: "Re: opinions on logical OR variation"
- Reply: Dave: "Re: opinions on logical OR variation"
- Reply: Tim Rentsch: "Re: opinions on logical OR variation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 28 Sep 2004 05:31:38 -0700
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.
- Next message: Ramesh: "#include optimization"
- Previous message: Ivan A. Kosarev: "Re: Obtain sizeof struct element using offsetof()?"
- Next in thread: Dan Pop: "Re: opinions on logical OR variation"
- Reply: Dan Pop: "Re: opinions on logical OR variation"
- Reply: Dave: "Re: opinions on logical OR variation"
- Reply: Tim Rentsch: "Re: opinions on logical OR variation"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|