Comment On Big-Data JSON

When Matthew saw this attempt at a JSON serializer, he had one question: why didn’t you use one of the many libraries we already use in this application? [expand full text]
« PrevPage 1 | Page 2Next »

Re: Big-Data JSON

2012-09-10 08:23 • by unknown (unregistered)
item.append(Constants.FRIST)

Re: Big-Data JSON

2012-09-10 08:24 • by @Deprecated
The Java compiler would produce much more efficient bytecode if he wrote:

item.append(Constants.CURLY_BRACKET_OPEN)
.append(Constants.QUOTE_SIGN)
.append(Constants.SCHEDULE_NUMBER)
etc...

Cuz', you know, that's the only WTF in the code???

Re: Big-Data JSON

2012-09-10 08:25 • by Smug Unix User (unregistered)
Big data is a little short on common sense.
minim - the amount of thinking spent before recreating the wheel.

Re: Big-Data JSON

2012-09-10 08:26 • by nwbrown
Well at least he used a StringBuilder and not a StringBuffer, so its ever so slightly faster than the really dumb way to do it.
I still see people who think this kind of thing is faster than just appending Strings in a single line. Maybe they are still stuck in Java 1.2?

Re: Big-Data JSON

2012-09-10 08:32 • by nwbrown
389449 in reply to 389448
And for all of you thinking "Wait, isn't string concatenation slow?", the compiler will replace any uses of + concatenation with an internal StringBuffer (so as long as you do it all in one line it is at least as fast) and will optimize things like Constants.CURLY_BRACKET_OPEN + Constants.QUOTE_SIGN + Constants.SCHEDULE_NUMBER + Constants.QUOTE_SIGN + Constants.COLON into a single String (assuming those are all really constants), so it could be much faster.

Re: Big-Data JSON

2012-09-10 08:53 • by lucidfox
Actually, starting with Java 1.5 it's StringBuilder, not StringBuffer. And for constant-time concatenations, + generates more efficient bytecode than using StringBuilder manually. Premature optimization at its finest.

Generally, the only case where you'd use StringBuilder explicitly are loops, and even then, something like StringWriter may be a logically better fit for the specific task.

That code is a complete mess, though. Sure, you can rewrite it as String.format("{\"%s\": \"%s\"... whatever, but why not just use a JSON library?

Re: Big-Data JSON

2012-09-10 08:58 • by derari
389451 in reply to 389445
@Deprecated:
The Java compiler would produce much more efficient bytecode if he wrote:

item.append(Constants.CURLY_BRACKET_OPEN)
.append(Constants.QUOTE_SIGN)
.append(Constants.SCHEDULE_NUMBER)
etc...

True, this has less pop and aload bytecodes. However, the JIT could optimize the original version better, because there the virtual method look-up needs to be done only once.

This is exactly why you cannot trust amateurs to handle your bigdata.

Re: Big-Data JSON

2012-09-10 09:00 • by arh
It's incredible to see how many idiots perform this StringBuilder stuff thinking that they're super optimizing, instead of realizing that the way they use it makes it exactly the same as just adding strings together. Of course, using a string buffer can save you cycles and reduce memory fragmentation, but most idiots who use it never think about the hows and whys. "You allocate memory only once", they say. Yeah, but how? "By allocating a large chunk and modifying it." And how large is that chunk? "Ummm.. I don't know?"

The default size of a string builder is 16 bytes. So unless you have a very small string, using the default constructor makes the whole thing pointless. Typical cargo cult, and sooo overused by enterprise programmers.

Re: Big-Data JSON

2012-09-10 09:02 • by Gary (unregistered)
The html comments go off on the Constants.CURLY_BRACKET_OPEN but I really like the Constants.ATTRIBUTE_* pattern myself. And Constants.ATTRIBUTE_WORKING_USERNAME_12 in particular. I wonder where Working Username 1 through 11 went. Maybe the values had colons, quotes, or braces in them.



Re: Big-Data JSON

2012-09-10 09:07 • by RichP
"you should to avoid creation of many objects in the Java!"

Oh, so Matthew works with fake Nagesh, got it.

Re: Big-Data JSON

2012-09-10 09:08 • by Larry (unregistered)
TRWTF is needing to use a "StringBuilder" to do something so simple it should be fundamental to any language.

print "{'Number':$schedNumber,'Date':$date...

Re: Big-Data JSON

2012-09-10 09:14 • by JamesH (unregistered)
I concur. The only possible would be the lack of chaining. Maybe he was under restriction from using specific libraries (it happens). I actually had to do something similar to work with a legacy third party format. I mean I would have this code autogenerated personally, but doesn't look that bad.

Re: Big-Data JSON

2012-09-10 09:14 • by Mijzelf (unregistered)
<!-- I honestly don't understand why anyone- _anyone_ thinks it's a good idea to externalize symbols into constants. I mean, really, are you ever going to change what a CURLY_BRACKET_OPEN is? And even if you do, are you going to want to leave it called CURLY_BRACKET_OPEN, after that? You're going to have to find/replace or refactor one way or the other. Can someone explain to me what drives programmers to do that?-->

That's an easy one. When using speech to text software of certain brand to dictate your code, a curly bracket open is written down as CURLY_BRACKET_OPEN. So what is easier than just creating it as a constant?

Re: Big-Data JSON

2012-09-10 09:15 • by JamesH (unregistered)
389458 in reply to 389455
Larry:
TRWTF is needing to use a "StringBuilder" to do something so simple it should be fundamental to any language.

print "{'Number':$schedNumber,'Date':$date...


Blah blah, my language is better, blah blah. Get over it.

Captcha: show some dignissim next time.

Re: Big-Data JSON

2012-09-10 09:17 • by ceiswyn (unregistered)
you should to avoid creation of many objects in the Java!


The grammar in that sentence is not your only problem. You just used the word 'Java' as a noun. Run fast, run far, before the trademark lawyers catch up with you.

I don't know what Oracle do to people who violate their directive about 'Java' being an adjective, but it's probably bad. After all, making you read their usage guidelines hasn't yet been banned under the Geneva Convention...

Re: Big-Data JSON

2012-09-10 09:19 • by TGV
389460 in reply to 389455
Larry:
TRWTF is needing to use a "StringBuilder" to do something so simple it should be fundamental to any language.

print "{'Number':$schedNumber,'Date':$date...

Which works so well that you'd never run into any kind of trouble at all. Like spaces. Or newlines. Or quotes. Or month day reversal.

But this is most certainly not TRWTF. That is not using libraries already used by the application, because you know the Java better, and then failing at doing it your own way miserably.

Re: Big-Data JSON

2012-09-10 09:20 • by Oh no! (unregistered)
Somebody set us up the Java!

Re: Big-Data JSON

2012-09-10 09:27 • by Anonymous (unregistered)
TRWTF is that not all the variables are final. Only the one string.

Re: Big-Data JSON

2012-09-10 09:39 • by Cbuttius
printf rules... and boost::format which keeps the niceties of printf whilst getting rid of some of the pitfalls.

Serialization and printing / parsing are very often mishandled and languages generally get it wrong in my opinion by not totally decoupling them.

Re: Big-Data JSON

2012-09-10 09:45 • by dgvid
I'll be trying to work the phrase "the Java" into conversations at work all this month. Thanks, Daily WTF!

Re: Big-Data JSON

2012-09-10 09:51 • by ZPedro
comment.append(Constants.LATIN_CAPITAL_LETTER_W);
comment.append(Constants.LATIN_CAPITAL_LETTER_T);
comment.append(Constants.LATIN_CAPITAL_LETTER_F);

Re: Big-Data JSON

2012-09-10 10:06 • by Steve The Cynic
389466 in reply to 389459
ceiswyn:
you should to avoid creation of many objects in the Java!


The grammar in that sentence is not your only problem. You just used the word 'Java' as a noun. Run fast, run far, before the trademark lawyers catch up with you.

I don't know what Oracle do to people who violate their directive about 'Java' being an adjective, but it's probably bad. After all, making you read their usage guidelines hasn't yet been banned under the Geneva Convention...

Um, Java *is* a noun. It is the name of an Indonesian island, so it's actually not merely a noun, but a proper noun.

Re: Big-Data JSON

2012-09-10 10:07 • by Zylon
Been a while since mangled grammar has been an intentional aspect of the WTF. Brillant!

Re: Big-Data JSON

2012-09-10 10:26 • by Hatshepsut
I’m surprised he used StringBuilder- after all, in big data procession, you should to avoid the creation of many objects in the Java.


Tick. In this big data procession, only one StringBuilder object is created in the Java.

Re: Big-Data JSON

2012-09-10 10:43 • by just me (unregistered)
389470 in reply to 389453
Gary:
... I really like the Constants.ATTRIBUTE_* pattern myself. And Constants.ATTRIBUTE_WORKING_USERNAME_12 in particular. I wonder where Working Username 1 through 11 went. ...


I suppose they stopped working.

Re: Big-Data JSON

2012-09-10 11:08 • by Sanhadrin
389473 in reply to 389469
Hatshepsut:
I’m surprised he used StringBuilder- after all, in big data procession, you should to avoid the creation of many objects in the Java.


Tick. In this big data procession, only one StringBuilder object is created in the Java.



For every call to createItemString(), a new StringBuilder will be created. Since they're doing "big data procession", I'm certain createItemString() will be called many times, creating many objects in the Java.

Re: Big-Data JSON

2012-09-10 11:13 • by Hatshepsut
389474 in reply to 389473
Sanhadrin:
Hatshepsut:
I’m surprised he used StringBuilder- after all, in big data procession, you should to avoid the creation of many objects in the Java.


Tick. In this big data procession, only one StringBuilder object is created in the Java.



For every call to createItemString(), a new StringBuilder will be created. Since they're doing "big data procession", I'm certain createItemString() will be called many times, creating many objects in the Java.


Oh yeah? Show me where 'new StringBuilder' is written more than once, smart guy.

Re: Big-Data JSON

2012-09-10 11:19 • by JimLahey
Welcome to my daily business.

We need to serialize some .net DataContracts into JSON, but the property names need to start with a lower case letter. Do we:

a) Spend two weeks writing our own JSON Serializer and change our method signatures to return everything as a string because "WCF doesn't support that"?

or:

b) do this in two seconds: [DataMember(Name="sameNameAsThePropertyJustStartingInLowerCaseLikeYouAsked")]

If there's one thing we excel at it's reinventing the wheel. Few round ones but plenty of various polygons instead, and proud they all are of their achievements. In the meanwhile I go home, seriously ramp up my hard drug consumption and drop out of society altogether.

Re: Big-Data JSON

2012-09-10 11:23 • by Sanhadrin
389477 in reply to 389474
Hatshepsut:
Oh yeah? Show me where 'new StringBuilder' is written more than once, smart guy.


This is why I always search for "new (Object Name)" and delete all of the duplicates.

Re: Big-Data JSON

2012-09-10 11:25 • by Reply to TRWTF (unregistered)
The WTF article, is TRWTF.

Re: Big-Data JSON

2012-09-10 11:26 • by just me (unregistered)
389479 in reply to 389460
TGV:
...
But this is most certainly not TRWTF. That is not using libraries already used by the application, because you know the Java better, and then failing at doing it your own way miserably.


IMHO he succeeded quite well in doing it his own way miserably...

Re: Big-Data JSON

2012-09-10 11:36 • by chubertdev

item.append(Constants.CURLY_BRACKET_OPEN);
item.append(Constants.QUOTE_SIGN);
item.append(Constants.COLON);
item.append(Constants.COMMA);


[sarcasm]why aren't there comments for these? how is anyone supposed to know what they mean??[/sarcasm]

Re: Big-Data JSON

2012-09-10 11:37 • by Rodnas (unregistered)
StringBuilder.BuildString.Tostring()

Re: Big-Data JSON

2012-09-10 11:42 • by ceiswyn (unregistered)
389482 in reply to 389466
Steve The Cynic:
Um, Java *is* a noun. It is the name of an Indonesian island, so it's actually not merely a noun, but a proper noun.


You might think that. I might think that. But Oracle's trademark lawyers think otherwise, and they're the ones with the scary briefcases.

Re: Big-Data JSON

2012-09-10 11:54 • by chubertdev
389483 in reply to 389481
One funny thing I love about the .NET Framework is that every class inherits from System.Object, which has its own .ToString() method. Thus, every single string has a .ToString() method, which I see invoked in so much code.

objUser.Username = txtUsername.Text.Trim().ToString()

Re: Big-Data JSON

2012-09-10 11:56 • by Sanhadrin
389484 in reply to 389481
Rodnas:
StringBuilder.BuildString.Tostring()


...which is the (semantically) correct way to use StringBuilder.

Re: Big-Data JSON

2012-09-10 12:21 • by fffff (unregistered)
389486 in reply to 389464
I think 'the Java' is right up there with 'the Bat Man'. There's just something special about that phrasing...

Re: Big-Data JSON

2012-09-10 12:44 • by My name is unimportant (unregistered)
389488 in reply to 389486
fffff:
I think 'the Java' is right up there with 'the Bat Man'. There's just something special about that phrasing...


Like "the facebook" or Betty White's "The Twitter".

If Indians have trouble using "the" in English, how do they handle German which has like 12 works for "the"

Re: Big-Data JSON

2012-09-10 12:46 • by Justn (unregistered)
At least he used constants.

Re: Big-Data JSON

2012-09-10 12:50 • by Evo
389491 in reply to 389464
dgvid:
I'll be trying to work the phrase "the Java" into conversations at work all this month. Thanks, Daily WTF!


In case you want to do it without being fired, I suggest you make it something like:
"I'll be coding the Java applet"

Re: Big-Data JSON

2012-09-10 13:09 • by Coyne
By far the biggest WTF I see here is that one constant, "QUOTE_SIGN".

Just what character is that?

If it's an apostrophe or double quote: Since when is a quote a sign?

Or is it a quote that can be positive or negative? If so, what does it mean when I use a positive quote versus a negative quote?

Or maybe a + or - sign in quotes?

Or is it just that they have to have two words in every constant? And of so: Why not "QUOTE_CHARACTER"? Is "_CHARACTER" considered harmful?

Will I ever run out of questions about it?

Re: Big-Data JSON

2012-09-10 13:16 • by chubertdev
389495 in reply to 389490
Justn:
At least he used constants.


What, ASCII characters 45 and 150 aren't the same? :)

Re: Big-Data JSON

2012-09-10 14:16 • by RFmich (unregistered)
One should also be avoiding the creme and sugar in the java

Captcha - not frist but secundum

Re: Big-Data JSON

2012-09-10 14:18 • by Dragnslcr
389497 in reply to 389488
My name is unimportant:
Like "the facebook"


The original name for the site was "The Facebook". I still have it listed in my bookmarks on my computer at home that way. I think it was about 5 years ago that they changed the name to just "Facebook".

Re: Big-Data JSON

2012-09-10 14:40 • by TheCPUWizard
389498 in reply to 389497
Dragnslcr:
My name is unimportant:
Like "the facebook"


The original name for the site was "The Facebook". I still have it listed in my bookmarks on my computer at home that way. I think it was about 5 years ago that they changed the name to just "Facebook".


Due to the increase in size, they are re-visiting that descision. I am betting they rename it to just "The" since no other dientification should be necessary.

Re: Big-Data JSON

2012-09-10 14:59 • by BR (unregistered)
389499 in reply to 389459
ceiswyn:
you should to avoid creation of many objects in the Java!


The grammar in that sentence is not your only problem. You just used the word 'Java' as a noun. Run fast, run far, before the trademark lawyers catch up with you.

I don't know what Oracle do to people who violate their directive about 'Java' being an adjective, but it's probably bad. After all, making you read their usage guidelines hasn't yet been banned under the Geneva Convention...


It probably depends on what extradition treaties the U.S. has with India...

(Also, there is more than one Geneva Convention.)

Re: Big-Data JSON

2012-09-10 16:25 • by Decade (unregistered)
To answer Remy's question in the boring, informative way:

It's because programmers do cargo cult programming. "Go To Considered Harmful," that sort of thing.

That reminds me of an assignment, back when I took my data structures class. It was in C++, and I couldn't remember how to do loops in C++, but I did remember tail recursion because of Scheme. But in C++, that was causing the stack to explode. So, I simulated tail recursion with a "goto" and a comment. The grader marked points off for use of "goto."

So, this guy was indoctrinated on the "No Magic Numbers" rule, and turned the character codes into named constants.

Re: Big-Data JSON

2012-09-10 17:24 • by Robert Hanson (unregistered)
You're all missing the point. This is a LOT of code; the more code you write the more can go wrong. If I accidentally delete a line that inserts a {, you'd never notice but it would be horribly broken. If instead I construct a java object and serialize it to JSON, the serialization part is ONE line of code and can't break no matter how you slice it.

The best line of code is the one you don't write.

Re: Big-Data JSON

2012-09-10 18:36 • by SP (unregistered)
389503 in reply to 389501
I think your statement/philosophy is so idiotic and ignorant that I actually felt compelled to say so!

Re: Big-Data JSON

2012-09-10 19:51 • by Brian Hsu (unregistered)
I’m surprised he used StringBuilder- after all, in big data procession, you should to avoid the creation of many objects in the Java, and StringBuilder is definitely an object in the Java.


Didn't this a common practice in Java? StringBuffer / StringBuilder is much more efficiency way to append String compared to using + operator?

« PrevPage 1 | Page 2Next »

Add Comment