- 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
No, it's "just me"s bizarre little C-ism that doesn't make sense. TRUE should be 1 and FALSE should be 0, or '1'b and '0'b as the only native versions are written in PL/1.
The others are likewise defined as bit-strings of length one, except for NO_ERROR, which is an int (spelled "fixed bin(31)") to distinguish it from all the possible non-zero return codes, positive and negative, that might otherwise occur.
Admin
It is the spawn of any number of very diabolical entities, if you believe in such things. Well, even if you don't believe in devils, it is the spawn of any and all of them, all at once.
Admin
Of course in the aviation industry you program defensively! Although so far I thought that this would be handled by redundant, independant and developed-by-different-teams systems where truth is something along the line of a majority vote.
Programming defensively in just one application can of course be done, but I still have to see the code for an application where every variable is tripled (isAdmin1, isAdmin2, isAdmin3) and truth is determined by majority vote in case a bit-flip happens (isTrue(isAdmin1,isAdmin2,isAdmin3)).
Admin
switch ( isOk ) { case true: // something break; case false: // something else default: // wtf ! }
But the real WTF is: Who the hell makes a system where switch on boolean is even possible?
Admin
I specifically do not include default cases when I'm switching on an enum. The compiler will give a warning if I'm not covering a possible enum value in the switch, which will vanish if a default case exists. And any uninitialized variable warnings can be worked around by initializing the variable before the switch, or adding an error return after.
This is C, so that error return may indeed be hit.
Admin
that is all
Admin
Admin
Admin
C++ code generally returns 0 on success. And there's a very good reason for that -- you can only return an integer, so you have 0 for success and 1-2^32 (or however big your system makes an int) for failure conditions. Particularly in the days when memory was small as any error message you print needs to be stored in memory somewhere -- instead of a bunch of print statements and strings you just return one integer. But even without that, when doing batch scripting it's very nice to be able to just compare one return value to check for specific errors rather than needing to iterate through the entire program's output.
The alternative is to return 1 on success and 0 on failure and have no idea what that failure is...or to return 1 on success and some other values that ALSO evaluates to true on failure.
I will agree though that this is sometimes carried over to places where it just doesn't make sense. At my job we have things like a database field named 'x_IS_VALID' that is 0 if the thing is valid, 1 otherwise. Always hell to figure out what that's doing, because then you get stuff like if( IS_VALID ) { // code to handle if it ISN'T valid }
Admin
Admin
I was working on a program recently where sale prices have start and end effective dates. If a sale is good indefinitely, the programmers were putting in end dates a hundred or more years in the future ... but each time someone came across this situation, they picked a new date. So some sales end in 2100, others in 3000, others in 2199, etc etc. I found the inconsistency annoying, so I created a static variable I called "DOOMSDAY" and set it to 12/31/2999. Now I'm changing all the far-futures to use DOOMSDAY.
If this program is actually still in use 986 years from now, I guess someone will have to update this. But I think it's a safe bit that I will no longer be working here.
Admin
If I have to forward an exception, I like to call it "up". That way I can write:
Admin
Even if the input is an enum, the variable is just an int so it can have values outside of the list, so you still need the default clause to catch that error condition.
Admin
How about "If no switch case matches, do nothing". That sounds like the sort of warning I would turn off.
Admin
Admin
Admin
See my subsequent comment for my answers to the points you raised (some of which I clipped). I do have to ask, what do you mean by 'C++ code'?
Neither c++ nor c languages mandate or specify returning anything to indicate success or failure. It is convention to return 0 from main upon success, as that is where the program meets the shell. The shell likes to see 0 for success, which was where we started this whole discussion.
There are some functions in the standard c library which return 0 for success, and probably as many (or more) which return 0 for failure. This illustrates (again) my point regarding unix weenies - it was unix weenies who invented c.
Nonetheless, we're not talking about the language, we're talking about code, and my points do not constrain themselves to c or c++ anyway.
Admin
Not enough enterprisey:
bool isTrue(bool aBool) { return aBool ? isTrue(aBool) : isTrue(aBool); }
Admin
If you edit and recompile the JVM you can make the ternary operator occasionally throw a FileNotFoundException. The stack trace is guaranteed to raise some laughs :)
Admin
I don't think you can do that, since those are already defined.
It should be #define FALSE (!TRUE) anyways.