Big-Data JSON

  • unknown 2012-09-10 08:23
    item.append(Constants.FRIST)
  • @Deprecated 2012-09-10 08:24
    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???
  • Smug Unix User 2012-09-10 08:25
    Big data is a little short on common sense.
    minim - the amount of thinking spent before recreating the wheel.
  • nwbrown 2012-09-10 08:26
    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?
  • nwbrown 2012-09-10 08:32
    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.
  • lucidfox 2012-09-10 08:53
    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?
  • derari 2012-09-10 08:58
    @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.
  • arh 2012-09-10 09:00
    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.
  • Gary 2012-09-10 09:02
    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.



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

    Oh, so Matthew works with fake Nagesh, got it.
  • Larry 2012-09-10 09:08
    TRWTF is needing to use a "StringBuilder" to do something so simple it should be fundamental to any language.

    print "{'Number':$schedNumber,'Date':$date...
  • JamesH 2012-09-10 09:14
    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.
  • Mijzelf 2012-09-10 09:14
    <!-- 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?
  • JamesH 2012-09-10 09:15
    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.
  • ceiswyn 2012-09-10 09:17
    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...
  • TGV 2012-09-10 09:19
    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.
  • Oh no! 2012-09-10 09:20
    Somebody set us up the Java!
  • Anonymous 2012-09-10 09:27
    TRWTF is that not all the variables are final. Only the one string.
  • Cbuttius 2012-09-10 09:39
    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.
  • dgvid 2012-09-10 09:45
    I'll be trying to work the phrase "the Java" into conversations at work all this month. Thanks, Daily WTF!
  • ZPedro 2012-09-10 09:51
    comment.append(Constants.LATIN_CAPITAL_LETTER_W);
    comment.append(Constants.LATIN_CAPITAL_LETTER_T);
    comment.append(Constants.LATIN_CAPITAL_LETTER_F);
  • Steve The Cynic 2012-09-10 10:06
    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.
  • Zylon 2012-09-10 10:07
    Been a while since mangled grammar has been an intentional aspect of the WTF. Brillant!
  • Hatshepsut 2012-09-10 10:26
    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.

  • just me 2012-09-10 10:43
    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.
  • Sanhadrin 2012-09-10 11:08
    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.
  • Hatshepsut 2012-09-10 11:13
    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.

  • JimLahey 2012-09-10 11:19
    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.
  • Sanhadrin 2012-09-10 11:23
    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.
  • Reply to TRWTF 2012-09-10 11:25
    The WTF article, is TRWTF.
  • just me 2012-09-10 11:26
    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...
  • chubertdev 2012-09-10 11:36

    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]
  • Rodnas 2012-09-10 11:37
    StringBuilder.BuildString.Tostring()
  • ceiswyn 2012-09-10 11:42
    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.
  • chubertdev 2012-09-10 11:54
    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()
  • Sanhadrin 2012-09-10 11:56
    Rodnas:
    StringBuilder.BuildString.Tostring()


    ...which is the (semantically) correct way to use StringBuilder.
  • fffff 2012-09-10 12:21
    I think 'the Java' is right up there with 'the Bat Man'. There's just something special about that phrasing...
  • My name is unimportant 2012-09-10 12:44
    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"
  • Justn 2012-09-10 12:46
    At least he used constants.
  • Evo 2012-09-10 12:50
    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"
  • Coyne 2012-09-10 13:09
    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?
  • chubertdev 2012-09-10 13:16
    Justn:
    At least he used constants.


    What, ASCII characters 45 and 150 aren't the same? :)
  • RFmich 2012-09-10 14:16
    One should also be avoiding the creme and sugar in the java

    Captcha - not frist but secundum
  • Dragnslcr 2012-09-10 14:18
    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".
  • TheCPUWizard 2012-09-10 14:40
    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.
  • BR 2012-09-10 14:59
    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.)
  • Decade 2012-09-10 16:25
    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.
  • Robert Hanson 2012-09-10 17:24
    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.
  • SP 2012-09-10 18:36
    I think your statement/philosophy is so idiotic and ignorant that I actually felt compelled to say so!
  • Brian Hsu 2012-09-10 19:51
    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?

  • Hexadecima 2012-09-10 19:53
    Reply to TRWTF:
    The WTF article, is TRWTF.


    TRWTF is hiring an editor who can't handle basic English grammar. I mean, they have this problem on Slashdot, but at least they're native errors that are easy to skip over.
  • Norman Diamond 2012-09-10 21:47
    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.
    Yeah, besides the grammar problem of grammar rules, you also have the grammar problem of grammar laws.


    ceiswyn:
    I don't know what Oracle do to people who violate their directive about 'Java' being an adjective, but it's probably bad.
    You have asked the Oracle a question about trademarks. The Oracle answers that it will invade Greece and rain lawyers on all of Delphi for giving hospitality to a trademark violator.


    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.
    The Javanese island consists of the Western Javanese province, Central Javanese provice, and Eastern Javanese province. The Oracular company's lawyers will sue all of them for trademark violations.

    And to everyone who continues to live in the light of the Sunny star after the Oracle company bought it, be warned: they're going to sue you too.
  • theJava 2012-09-10 22:08
    "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."

    What alternative do you suggest? If you hadn't used the StringBuilder, more objects would have been created in the Java
  • Mathlete 2012-09-10 23:05
    Remy Porter:

    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?


    The real issue in this case is that constants should have a name that reflects their semantic meaning, not one that describes their current value.

    CURLY_BRACKET_OPEN is a stupid constant name, it should be something that describes the purpose of the constant, like JSON_BLOCK_START. That way when the JSON spec changes and the block delimiters change to @ and ~, you can just change JSON_BLOCK_START to @ and JSON_BLOCK_END to ~ in one place!
  • Ru 2012-09-11 04:42
    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 don't mess with The Java.
  • Martin 2012-09-11 04:55
    Mathlete:
    The real issue in this case is that constants should have a name that reflects their semantic meaning, not one that describes their current value.

    CURLY_BRACKET_OPEN is a stupid constant name, it should be something that describes the purpose of the constant, like JSON_BLOCK_START. That way when the JSON spec changes and the block delimiters change to @ and ~, you can just change JSON_BLOCK_START to @ and JSON_BLOCK_END to ~ in one place!


    Very much this. And as to why they would do this in the first place, probably all programmers have had spec's change on them halfway through a project, or after go-live, so the upshot is basically nobody trusts specs. Even the formally agreed ones, in some cases!
  • Pero Perić 2012-09-11 05:01
    Mathlete:
    Remy Porter:

    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?


    The real issue in this case is that constants should have a name that reflects their semantic meaning, not one that describes their current value.

    CURLY_BRACKET_OPEN is a stupid constant name, it should be something that describes the purpose of the constant, like JSON_BLOCK_START. That way when the JSON spec changes and the block delimiters change to @ and ~, you can just change JSON_BLOCK_START to @ and JSON_BLOCK_END to ~ in one place!


    Exactly. Reasoning that this is good in case JSON specs change is unnecessary. Clarifying constant strings such as ": [{ " is reason enough to use constants that describe semantics of the characters (such as BLOCK_START, BLOCK_END).
  • ubersoldat 2012-09-11 06:08
    you should to avoid creation of many objects in the Java!


    Yeah, being an Object Oriented language don't seem to ring a bell on his little head.

    And just for the record if some future generations read this article, the correct way to do it would be to use a mutable singleton, not a stupid StringBuilder.

    Ah! And kids, do not try this at home, leave it for the cloud enterprise.

    TRWTF is that after six years or so, I just found out that the comments and the forum use different "text editors".
  • Wody 2012-09-11 06:22
    To answer Remy: CURLY_BRACKET_OPEN and many other similar things are used instead of constants, is because printers could not print many computer-specific characters. This means, that they would often be printed as spaces instead, and somebody recreating the program from paper, would get errors because of these missing characters.
    Of course, these days this practice continues, not because much is printed, but because many fonts have the same issue and people tend to vary widely in what font they use to view a program, unaware that their font does not contain all characters, or makes i look the same as l, o the same as 0, and so on.
  • Chris A 2012-09-11 07:08
    Brian Hsu:
    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?


    I think it depends on the context.
    As people have said (plus a bit of my own reading before I answered) - StringBuilder/StringBuffer probably have no performance gain over a one-liner like myStr=str1+str2+str3, because the compiler optimizes away any difference. (But this is not in the language spec, only a compiler's design.)
    SB/SB do become more efficient for multiple lines of concatenation, or concatenation within a loop, though you'd want to consider case-by-case the trade-off against making the code more verbose.
  • Silverhill 2012-09-11 18:25
    fffff:
    I think 'the Java' is right up there with 'the Bat Man'. There's just something special about that phrasing...
    The Bat Man is, I suppose, a grown-up Bat Boy.
    "The Batman" ("Batman", here, is a solid string) is the Official, Teddibly Propah term for the DC Comics superhero....
  • Burst 2012-09-11 20:51
    The Java, it burns!
  • hombre 2012-09-12 05:55
    -Knock, knock.
    -Who's there?
    Very long pause.
    -Java.
  • Chelloveck 2012-09-12 15:34
    fffff:
    I think 'the Java' is right up there with 'the Bat Man'. There's just something special about that phrasing...


    And do *not*, under any circumstances, put objects in the Bat Man. He really, really hates that.
  • qbolec 2012-09-12 16:23
    JimLahey:

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

    How about using a standard JSON serializer, and then using RegExp to correct first letters? Just don't tell me that json is not a regular language.
  • JimLahey 2012-09-13 03:56
    qbolec:
    JimLahey:

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

    How about using a standard JSON serializer, and then using RegExp to correct first letters? Just don't tell me that json is not a regular language.


    We don't need to do any of that. Like I said, with WCF you can do this in a matter of seconds:

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

    You'd do well at our place with anti-productivity like that. They'd probably make you lead architect or something, while anyone who actually knows how to use the .net tools we have gets assigned to a Delphi project.

    If you reinvent the wheel enough times at some point those wheels will form part of the big truck of fuckup which will not only run you down, but reverse back over you a couple of times for good measure.
  • Nolo 2012-09-13 07:57
    > I'll be trying to work the phrase "the Java" into conversations at work all this month. Thanks, Daily WTF!

    I've got one such use for you. Next time someone builds something like this, tell him to quit pissing in the Java.
  • Andrew Au 2012-09-13 21:33
    The optimization totally make sense for me, precondition on they have profiled and noticed garbage collection is an issue with the JSon Serializer library.

    Once I was in QCon and I heard about Twitter data analysis guys are doing exactly the same thing, going primitive, Flyweight pattern to avoid object creation, array instead of list, and much more.

    One thing they could have done better is to allocate memory in block, reserve a single buffer for building JSon strings and keep reusing it to avoid resizing, and if code bloat is an issue write a code generator instead of hand coding them.
  • SoleReasonForVisit 2012-09-14 20:09
    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?

    The best part of a hundred comments, mostly from Java weenies as far as I can see, and only one addresses the actual issue.

    Look, I don't care how (in)efficient the damn code is. It's cretinous.

    You don't even need to use .Net decorations and such. There are perfectly good C# libraries into which you can feed JSON Objects, JSON Arrays, JSON Values, JSON Strings and JSON Numbers. Them things is all JSON is. And what do you get? JSON!

    That's kind of the reason JSON was designed that way.

    It was not designed with the intention that bit-headed lunatics would either (a) argue about the merits and demerits of deprecated or otherwise Java string concatenation constructs or (b) tack an endless number of terminator tokens onto a string inline without benefit of procedures, object-oriented programming, or (here's the bleeding obvious thing when you're talking about parsers) recursion.

    What is wrong with you people?