- 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
The intention of the above snippet appears to be checking whether itemA contains all FRIST mandatory!
Admin
I think it's unfair to criticise the addition of the fifth item. When you see code like this, it doesn't take long to e fairly sure it's just bad code, but much longer to be absolutely certain it is, and that you haven't missed anything.
Admin
Sometimes bad is bad.
Admin
TRWTF is those withImportantFooPiece methods are actually mutators instead of returning a new object. That naming style is typically used by immutable classes, not POJOs.
Admin
So the new (nullable boolean) field defaults (via constructor) to false.... Thus a false in an object is actually "nothing", but a "null" is something...
Aside from other WTF aspects, the code breaking because of this seems right.
Admin
Why? Just why? If you have to do this to work with your "equals" overload method, perhaps you either need a new method for your object or just do a field-by-field comparison. Maybe even use class inheritance to do a comparison at some type of base "Item" type and look for the other fields needed for the ItemB type. Does Java support that?
As I think has been mentioned, if you have to null out fields after invoking the constructor, maybe you need an alternate constructor.
Admin
I think the most interesting thing is that SimpleItemB is a subclass of ItemB. Seems quite backwards to me since the Simple alowats contains a subset of the properties. I also think this inversion is what led the developer to need to clear out all of the extra fields and led to this particular coding pattern. I've used the SimpleX class concept to reduce traffic. Return a collection of Simples to build a list in the user interface and fetch the full sized object when a record is selected.
Also, this sounds like it should have been implemented as a factory. Since this is Java and they love Gang of Four naming, I would expect it to be in the ItemBFactory class.
Admin
With an added WTF on top that itemA also has setXXX methods... I guess their style is: withXXX is chainable, setXXX is not.
Admin
If all other comments are removed, then one can determine whether i posted a comment by comparing the comment section to empty.
Admin
Actually, that's just how JAXB works. Your setters return void and you can add jaxb-fluent-api plugin to your pom.xml to generate 'withers' (yes, I've actually heard someone call them that, took me a while to understand what they were talking about) that just call the original setters and then return 'this'.
Admin
But the check for whether the required fields are null only checks for fields 1-4.. Did the guy adding in field 5 forget to add it to the list, or was that just a mistake in anonymizing the code?
Admin
I agree, but for a different reason - sometimes it's a lot less feasible to rewrite bad code than it is to simply add to it. And when a deadline is approaching fast, you don't have time to waste debugging all the little problems that appear when you rewrite the bad code
Admin
This story makes me realize how long I've been gone from being responsible for fixing other peoples code. Its been about 18 years. So I'll just describe this as sometimes the road has ruts that are so deep that its easier to traverse them and take a few bumps than it is to flatten and pave a new surface.
Admin
Honestly, that's the correct approach. You don't just up and re-write battle-hardened code. That always takes significantly more effort, and often introduces bugs. Sure, in this case the dev introduced a bug while trying to maintain the status quo. But that's only in hindsight. The developer took the right approach with the information they had on hand at the time they had to decide how to proceed.