- 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, the easy part is tracking it down to that mixup, that only takes a day or two. ;p The fun part is scanning and coming to grips with the entire codebase to try to ensure that it wasn't done that way on purpose, for fear of fixing it and generating new bugs. (In which case it would be easier to keep X_FALSE as true, and have a new X_FALSEFALSE (X_TRULYFALSE?) for false. Which is just going to make the migraine worse for the next maintainer. =DDD)
Admin
The only additional lines are in the constants declaration, though. By the KB?
Admin
It's not really enterprise until it's nested more than three levels deep.
Should really be:
public static final Boolean ORG_HAS_CONTENT_TRUE = BASE_TRUE;
<FONT size=+0>public static final Boolean</FONT> ORG_IS_NEW_TRUE = ORG_HAS_CONTENT_TRUE ;
<FONT size=+0>public static final Boolean</FONT> ORG_ENABLED_TRUE = ORG_IS_NEW_TRUE ;
Admin
I can explain how this happened.
The object in question works similar to another one in the system (implemented by said consultants) and it has a number of statuses that could be either on or off, just like this one.
They were all stored in a bitmask field in the database, so the previous object had constants for all the bitmasks.
When the new objects were created they were stored with booolean values in the DB, but the pattern of making a constant for every possible value of "ON" was carried through.
Of course, why the data container dosn't have is....() methods to check the status bits is beyond me. The constants are defined in the data container and the business layer is doing the bitmask checks and boolean checks.
I could probably keep the submission queue full all by myself with this project.
Anybody want to see our 12,000 line classes that do nothing but transfer control to another layer? Hundreds of functions that do nothing but return the value of another function with the same signature.
Admin
This approach fails to take into consideration "truthiness". Sure, something can BE true or false, but what about things that FEEL true or false?
Admin
Admin
If the constants were "OrgChartEnabled = TRUE" or "OrgChartDisabled = FALSE" that is one thing. But "OrgChartEnableTrue = TRUE" and "OrgChartEnabledFalse = FALSE" Thats just cruel and unusual punishement for the keyboard and the disk drive write head.
Admin
One would hope the Library of Congress uses better standards than that.
Admin
Another possible reason for this WTF: they are future-proofing the code against the change of the variable's type. So they can, for instance, change it from a boolean to a three-valued type.
captcha: salad
Admin
I am afraid to find myself thinking that this sounds like actually a quite reasonable explanation.
Carrying on the tradition is a bit of a WTF I'd say... but if it was just ported to Java I could see that happening.
Admin
Yeah but the guy who wrote this is obviously a tool and probably has his own ideas about "convention"...I can see him arguing... I don't need no stinking "convention" this makes it more obviuous they are constant. It looks to me like he was a C programmer who has it in his head that these are #defines...
Admin
You do realise the guy who wrote this cannot be trusted? After all, his definition of truth can change at any time...
Wait, maybe he's a former lawyer?
Admin
Ok, I've done that. But then why not use a type beneath the "constants" that could actually have more than two states? Unless they were, for instance, planning on racking up even more billable hours/LsOC by having to go back and make them all, oh say, Ints?
Admin
hi guys I googled the wtf blog andf mine is under yours hehehe:)
Admin
No, yes, yes, and yes.
Admin
Perhaps they did it because
result = SomeFunc( organisation, ORG_HAS_CONTENT_TRUE, EVT_ACTION_REQUIRED_TRUE );
can be understood without context, cf
result = SomeFunc( organisation, true, false );
Admin
boolean isWTFAndEnterprisey = true;
result = enterpriseObject.doWTF(organisation, isWTFAndEnterprisey);
Admin
even if that were the case, it'd be better to explain that by using comments ;)
Admin
This is still not properly enterprisey in one way..
It requires actual effort of some form to redefine TRUE and FALSE..
Surely they'd have better having a truth.properties where they define TRUE and FALSE appropriately.
That way, they could easily fix any Bizarro-world incompatability without having to change any .javas or .classes.
True flexibility.
darj
Admin
Surely we've all seen the (in)famous
#define NUMBER_ONE 1
#define NUMBER_ZERO 0
and their ilk, so that the programmer can say "look ma, no magic numbers"
Admin
No they can't !!
That's why The Truth had to be spin-doctored with few layers of redirection :) makes it easier to swallow
Admin
oooh yeah I want to see that one.
Admin
I hope that these 12,000 lines were made by a generator, not written by hand. But then, considering the circumstances, my hope is very slight.
Admin
Admin
Cracking :)
Admin
That's 3VL, right?
Admin
You mean, it should've been declared "external"? :)
Anyway, this isn't the reinvention of the wheel, this is the reinvention of wood.
Admin
I actually had a coworker who wrote things like
#define SOME_OPTION_FALSE 0
#define SOME_OPTION_TRUE 1
#if SOME_OPTION == SOME_OPTION_TRUE
...
#endif
(Oh, and why is it that unregistered people such as myself apparently cannot post or reply to the sidebar but can reply here?)
Admin
They call this pattern "Defensive Programming". They say it never hurts to be wary and your code has to be fool-proof. Just like this one:
if( foo == true ) {...}
else if ( foo == false ) {...}
else {...}
They are not happy to be informed that this code will only be fool-proof after they've lost write permission to the sourcebase.
Admin
Even though physical punishment is forbidden in todays schools and universities I reckon there are teachers and professors who would beat the s?*t out of their students for doing something stupid like this!
Admin
someFunc should have parameter names that reflect what true and false mean. if it's unclear when the method is invoked add a line of comment. if the function does anything complex then it should always be taking some similar true and false (if not the same true and false). another thing I wonder is... maybe they have ebject with really bad instance variable names. for instance:
foo = ORG_HAS_CONTENT_FALSE;
bar = EVT_ACTION_REQUIRED_TRUE;
instead of the better
orgHasContent = false;
evtActionRequired = true;
Admin
It would be even better if Java supported named parameters:
ob.someFunc( organisation : organisation, hasContent : true, actionRequired : true );
Admin
Defensive programming is often a good practice. For example, some of real code I wrote:
Ignore the first comment. My code has a lot of "throw new Error()" or "throw new AssertionError()" (like errx(2,"error in algorithm!") in C). Some of them even pass a message ("this should be reasonably unreachable").
It's good because it means when there's an error in my judgement or a typo, it's far easier to tell where it is (it's either in the function, or something not checked in the arguments to the function).For a simple (a) and (!a), it's stupid, because it's more copy-and-pasting, which means more code to change when you change variable names/whatever, and so on, which means more likely to have bugs.
I'm also guilty of if (true) throw new Error() (or "the following code should be unreachable unless my reasoning is wrong, but I don't want to comment it out because comments don't nest, and I don't want to delete it because it might be reachable" - the if (true) is so Java doesn't force me to delete/comment out).
I could just "assert false", but then I'd need to turn assertions on, and there's no enable-assertions-globally setting.
Yes, I'm a hacker, but I aim for some sort of readability ;)
captcha = china
Admin
In Java the constants are always inlined by the compiler so at the end we may even throw out the Truth class (we shoud've done it in the beginning however :) ).
Perhaps the developer was paid per kByte or something like that.
Still it looks impressive - adding hundrets of constants to make the code look complex and mystic.
Admin
Perhaps the author had some crazy reason to facilitate distinct Boolean objects. IdentityHashMap, synchronized, ==, System.identityHashCode or whatever. With the specified approach, it's easy to redefine a particular constant as a unique object. Or maybe he's just a jackass. Personally, I don't even think that Java should provide primitive wrapper object constructors.
Admin
But even then, wouldn't all the *_FALSE values be zero? Or did these even exist?
Admin
Dear community server: You suck. Please stop failing when I use Opera. P.S: I banged your mom. Twice.
TC said:
I could just "assert false", but then I'd need to turn assertions on, and there's no enable-assertions-globally setting.
According to http://java.sun.com/j2se/1.5.0/docs/guide/language/assert.html, the -ea flag w/o arguments enables assertions globally. Assertions are disabled by default.
YOU LOSE, TREBEK.
Admin
genuine, I must pick up that translation!!
Admin
Congress.java?
Admin
No, it was a bitmask, not a bitfield. One "status" column that held an integer.
OPERATION_1 = 1;
OPERATION_2 = 2;
OPERATION_3 = 4;
OPERATION_4 = 8;
OPERATION_5 = 16;
....
OPERATION_25 = 1677216;
You add 'em all up, and shove them into the DB in one field. then your checks look like
if (status_bits & OPERATION_25) {
//do stuff
}
In the old there is no constant for it being false.
Oh, and this isn't "legacy" code either -- it's all 2001ish or newer.
Admin
It almost did. Java is based on Objective C far more than it's based on C++. But it had to have a somewhat C++--ish syntax to lure C++ programmers in, or it would have died an immediate death back in the 90s.
Admin
Was this code written by an (ex) Enron employee? No? Maybe TYCO?
Gives me the heebie jeebies that corporate programmers are so confused by truth.
Admin
And after saying this, he went out again to the management and said to them: “I find no fault in this code.”
- John 18:38, Enterprise Version
genuine, I must pick up that translation!!
---
And for multipying a fish and two pieces of bread, they called Him a pirate, and crucified them.
Lucas 15:29, RIAA Version
Admin
Not true. Sun's Java coding conventions recommend using all-upper-case words separated by underscores for contants.
http://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html
Admin
The real WTF is that java does not provide Boolean.UNKNOWN. And that is the Boolean.TRUTH.
Admin
Hmmm...if you wanted to set HasContent (a boolean) to false, wouldn't you just simply go:
[code]HasContent = false;
...and not even bother with the check before that???
Admin
'k...nvm...just reread the whole thing - also didn't realize the initial poster was doing an assignment, not a check...doh!
Admin
Or they could had just used Enums. Perhaps even a custom Java compiler that autoboxes this particular enum type to basic booleans - of course that would require the enum to have a getTrue() method, and the code would not compile on any other compiler, but that is a small price to pay for a true type-safe multi-value boolean replacement.
Admin
GREATER POOP: Is Eris true?
MALACLYPSE THE YOUNGER: Everything is true.
GP: Even false things?
M2: Even false things are true.
GP: How can that be?
M2: I don't know man, I didn't do it.
Admin
I'de suggest the following improvement:
public class Truth {
static Boolean trueValue;
static Boolean falseValue;
{ if (Math.random() > 0.5) trueValue = true;
else trueValue = false; }
{ if (Math.random() > 0.5) falseValue = true;
else falseValue = false; }
public static final Boolean BASE_TRUE = trueValue;
public static final Boolean BASE_FALSE = falseValue;
/* ... /
public static final Boolean EVT_VERIFY_TRANSACTION_TRUE = BASE_TRUE;
public static final Boolean EVT_VERIFY_TRANSACTION_FALSE = BASE_FALSE;
public static final Boolean EVT_ACTION_REQUIRED_TRUE = BASE_TRUE;
public static final Boolean EVT_ACTION_REQUIRED_FALSE = BASE_FALSE;
public static final Boolean EVT_TRANS_SETUP_TRUE = BASE_TRUE;
public static final Boolean EVT_TRANS_SETUP_FALSE = BASE_FALSE;
/ ... */
public static final Boolean ORG_HAS_CONTENT_TRUE = BASE_TRUE;
public static final Boolean ORG_HAS_CONTENT_FALSE = BASE_FALSE;
public static final Boolean ORG_IS_NEW_TRUE = BASE_TRUE;
public static final Boolean ORG_IS_NEW_FALSE = BASE_FALSE;
public static final Boolean ORG_ENABLED_TRUE = BASE_TRUE;
public static final Boolean ORG_ENABLED_FALSE = BASE_FALSE;
}