- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Probably copy-pasted from a website known to provide many snippets of code that just 'work.' (for any given value of 'work.')
Admin
I've been trying to think of a pun that hasn't been used, but my brain's a bit num today.
Admin
What I love about that enum is that it assigns True to 0 and False to 1.
Admin
Yeah, but that's supposed to be the one starting with st and ending with low, not the one starting with th and ending with wtf...
Admin
Again.
What is it with those guys?
Admin
Well, that's shell semantics, isn't it? retval 0 means ok, so that's the "good" value, like "true". (And if you think about retvals and && etc. too hard, you'll confuse yourself senseless.)
Admin
I blame this site. It's been advertising the FileNotFound boolean for so long someone was bound to use it. :p
Admin
Assuming NUM is really the "number of items in the enum" idiom, then trying to imagine, before my morning caffeine, why someone would want to iterate over the range of TRUE, FALSE, and FILE_NOT_FOUND is causing my brain to segfault.
Admin
It is, but there's a very strong "so the fucking fuck what" aspect there. We aren't programming in shell here, so we should follow the semantics of the language we are programming in.
Admin
So, uh, we're not even going to talk about the part where the ordinal values TRUE == 0 and FALSE == 1, inversing every standard practice around booleans?
Admin
Uh, TRUE=0 is probably an extension of the C/C++ practice of making SUCCESS=0 and any value other than 0 indicating an error condition. After all, there's only one SUCCESS, but there can be many kinds of FAILURE. I'm not saying that making TRUE=0 is good practice. But there might be an excuse.
Admin
To reinvent a switch statement of course.
iterate over the values, then have an if-else-if stack to do different things depending on which values is passed in.
Admin
For statistics? Allocate NUM integers, fill them as a histogram, output it somewhere nobody will bother to read it...
Admin
I know! I know! They have their own classes, using Hungarian notation. So "ENUM" means "Editable Number" but if things don't work out, downgrade it to class NUM, a non-editable number.
Admin
You can't convince me this isn't the same code as the original.
Admin
Since TRUE means that the setting in the config file was true, and FALSE means that the setting in the config file was set to false, and obviously FILE_NOT_FOUND means that the config file was missing, I would guess that NUM indicates that the value from the config file was a number.
ENABLE_FROBBING=TRUE ENABLE_CHEAT_MODE=FALSE NUMBER_OF_DUCKS=7
Admin
I have become Comfortably NUM
Admin
I will not subscribe to your false dichotomies!!!
Admin
What's so horrible about an extra enum there? While it doesn't make sense on a boolean, if you have more states having a Count at the end of your enum can simplify life at times. I've got one in mind where there are multiple arrays where the enum is the index. In an ideal world that wouldn't happen, but we live in the real world.
Admin
In how far is "settings in a config file" (as provided in your example) a thing one would call a "bool", except maybe it's an acronym for "Build On top Of Logic". It would be understandable if, following the configuration file evaluation approach, config settings can be either "TRUE" (value 0) or "FALSE" (value 1), the config file itself cannot be found - FILE_NOT_FOUND, or some config setting has been assigned a number - NUM. Mapping those... things as arbitrary values to settings (so the value is "TRUE" or "FALSE", but could be "YES" or "NO", "ON" or "OFF", "WANT" or "DONOTWANT", or "1" or "0") and then to program-internal things sounds absolutely possible. Even then, FILE_NOT_FOUND would not be in the same row as TRUE, FALSE, and NUM. But to summarizse, nothing of that is actually boolean and therefore should probably not be expressed as an enum Bool.
Admin
Presumably they couldn't be bothered to change the name. Or it never occurred to anyone that it was even possible to do so.
Admin
That would probably why NUM is used. Somewhere else there's another enum handled the same way, with a final sentinel value of COUNT. Another that uses LAST for the purpose, and another that ends with END....
Admin
I have definitely used MAX_FOO in an enum...
Admin
S_FALSE
would like to talk to you about this "only one SUCCESS" thing.Um.
S_FALSE
is defined to be 1, by the way, whileS_OK
, also a success, is zero.Admin
Except that in C, true is non-zero, and false is zero. That is, using this enum,
if (TRUE)
would not trigger, andif (FALSE)
would.The only place where 0 is success and not-0 is failure is, as mentioned above, in the return value to main() - in other words, to the shell.
(No, strcmp() does not count. 0 does not mean success, it means equal; negative means smaller than the other and positive means larger. Any of those could be a success, depending on what you're comparing.)
Admin
I've actually seen this technique before! And it does make sense, sometimes. I've actually used this in callback-related code where I needed to iterate over all the callback lists for all event types. It can also be useful if you're consuming data from an outside source (i.e. it's not validated by the compiler) and you want to make sure it's a valid enum value.
Whether this makes sense in a bool value...eeeeeeh? Maybe it's just something the style guide said to put on every enum.
Admin
Why stop at file_not_found
{ true, false, file_not_found, file_is_empty, too_many_rows, missing_primary_key, memory_overflow, integer_too_big, impossible_date, .., }