Enumeration?

Started by
11 comments, last by TearsKnight 21 years, 9 months ago
quote:Original post by Coward
nahh...
It just increases the chance of bugs, and decreases readabillity...

That's what he was saying, IMO; that the use of enums is more readable than his #define counterexample.

quote:

enum UseOfGrapes {
GrapesUse,
NoGrapesUse
};
enum NoUseOfApples {
NoAppleUse,
AppleUse
};


Yuck. Yuck yuck yuck. There is a time for enums, and a time for variables that are not enums.

If you use enums to explicate every boolean parameter, you will horribly clutter the namespace. Furthermore, booleans are in the language because their meaning is clear; a programmer whose function takes an argument of the form "dont_do_xxx" is following a very very bad form. True == do == yes == positive == successful == etc.

quote:
...now here you can not realy understand the meaning, of a function call, unless you look at the name of the paramenters, or use defs...


If you're not looking at the function parameters to understand a call, you should be. In the case of functions where the use of each argument is not obvious, comments suffice. As shown:


    fh =foo->open_serial(    "COM1:", // first serial port    true,    // allow buffering    false,   // don't convert newlines    128,     // the buffer size    50);     // the timeout value    


Obviously I wouldn't do that for every function; but where the argument lists are long and the functions unfamiliar, it increases readability far better than enums could. After all, how would you force the programmer not to mix up the buffer size and the timeout value?

quote:So, enums is your friend, so use them whenever it's possible!

Use enums when you have a predefined list of options, which is either three or more options, or two options and a boolean doesn't make much logical sense.



Don't listen to me. I've had too much coffee.

[edited by - sneftel on July 17, 2002 2:14:00 AM]
Advertisement
sure, that example was a bit obfuscated, it was merely an example where enumerations would make it more readable...

If you've got code like that, it would proberly be better to split the function up, or atleast make one "actions" paramanter, but I was merely trying to show enums vs. boolens using code that is pretty equal...

I've got loads of examples where a boolean value that's true means that something shouldn't be done, so I must say I don't agree on your "true is equal to do it" statement...
do you never use else?

[edited by - Coward on July 17, 2002 1:14:10 PM]
Don't Temp Fate..
Your right; my proscription of "negative" booleans was a little too absolute. I still think, though, that unless there is some other design principle you''re trying to maintain, it''s better to use positive booleans.

Your point about the actions parameter is a good one, and makes me wonder why people don''t use bit-packed structures more often instead of the long lists of #defines, each setting one bit. I wonder... will C++ allow you to stick a 1-bit bool in a bit-packed struct?

This topic is closed to new replies.

Advertisement