• Rodnas (unregistered)

    String result = ... + insertComma(varA) + "," + insertComma(varB) + ...;

  • Rodnas (unregistered) in reply to Rodnas
    Rodnas:
    String result = ... + insertComma(varA) + "," + insertComma(varB) + ...;

    oeps i meant: String result = ... + insertComma(varA) + "Frist" + insertComma(varB) + ...;

  • Gramma (unregistered)

    Seems pretty obvious to me... they're just trying to create a CSV file.

    terribly.

  • Dignissim (unregistered)

    On a related note, the percentage of VB programmers that don't know how to escape quotes is scary, even considering the sample population is VB programmers.

    In all fairness, I do find it kind of counter-intuitive. Why are backslashes too scary for basic?

  • Mike Caron (unregistered)

    Gosh, I hope none of the variables used with this method have quotes in them...

  • (cs)

    Now now, at least they used a StringBuffer... Sure, you might argue that they didn’t use it outside of that method, but hey, it’s a start.

  • (cs)

    Anyone heard of StringBueller? Anyone? Anyone?

  • Bob (unregistered) in reply to frits
    frits:
    Anyone heard of StringBueller? Anyone? Anyone?

    Not personally... no. Heard of StringBuilder though!

  • Anon (unregistered)

    A stretch, but in the UK, quote marks (") are sometimes called inverted commas. So maybe insertComma was supposed to be insertInvertedCommas but, as we all know, shorter function names are more efficient!

  • Anon (unregistered) in reply to Bob
    Bob:
    frits:
    Anyone heard of StringBueller? Anyone? Anyone?

    Not personally... no. Heard of StringBuilder though!

    Whoosh!

  • Daniil (unregistered)

    Ha. I find that code very funny. Funny as in the most useless and pointless and CPU consuming from time sharing hardworking operating system way. Luckily for us, people who write those kinds of code exist in every line of industry or life and generally have ratio of 8 to 2 thus keeping life interesting. Who's up for a drink to talk about good looking famous people?

  • (cs)

    Clearly the work is unfinished. I suggest an additional function...

        private String insertQuote(String src){
    
               StringBuffer result = new StringBuffer();
               try {
                               char s = ',';
                               result.append(src);
                               result.append(s);
                      
                } catch (RuntimeException e) {
                    logger.error(e);
                }
                return result.toString();
             
        }
    Syntax...
        String result = ... + insertQuote(insertComma(varA)) + insertQuote(insertComma(varB)) + ...;
  • Pragmatopian (unregistered)

    The failure to use sensible naming conventions would suggest that the coder is a celebrity parent.

    "We considered James, but in the end we thought that Sage Moonblood would be more appropriate."

  • (cs)

    The function name is clearly off, apart from that I don't see anything but normal not-that-well-written code. It's not like the performance or readability otherwise is worse than a lot of other code.

  • HUH (unregistered) in reply to BlueKnot
    BlueKnot:
    Clearly the work is unfinished. I suggest an additional function...
        private String insertQuote(String src){
    
               StringBuffer result = new StringBuffer();
               try {
                               char s = ',';
                               result.append(src);
                               result.append(s);
                      
                } catch (RuntimeException e) {
                    logger.error(e);
                }
                return result.toString();
             
        }
    Syntax...
        String result = ... + insertQuote(insertComma(varA)) + insertQuote(insertComma(varB)) + ...;

    Error: Variable 'varA' undefined Error: Variable 'varB' undefined Error: Not a function.

    FTFY

  • (cs)

    Hey, at least he used a StringBuilder instead of concatenating one string. Not that that would probably make much difference in this case.

  • Vikrant (unregistered)

    I don't see the WTF.(Except for the function name).

    I have seen code written by a major database company that has exactly the same functionality. They were doing this for creating dynamic SQL Strings and instead of writing '''' many many times, they preferred creating a java function for it.

  • (cs) in reply to Daniil
    Daniil:
    Ha. I find that code very funny. Funny as in the most useless and pointless and CPU consuming from time sharing hardworking operating system way. Luckily for us, people who write those kinds of code exist in every line of industry or life and generally have ratio of 8 to 2 thus keeping life interesting. Who's up for a drink to talk about good looking famous people?

    The ratio is more like 12 to 3 in my experience.

  • (cs)

    comment = insertComment ("");

  • the beholder (unregistered) in reply to Pragmatopian

    [comma user="Pragmatopian"]The failure to use sensible naming conventions would suggest that the coder is a celebrity parent.

    "We considered James, but in the end we thought that Sage Moonblood would be more appropriate."[/comma]

    Hey that's my sorcerer elf's name too!

    [2]

  • OutSource (unregistered)

    This is what you get when you outsource development to countries where English is not well understood.

    comma, quote, period, yaa yaa, whatever, just go insert it. Guys probably tried different values for the char d part till they got it working ...

  • VB Programmer (unregistered) in reply to Dignissim
    Dignissim:
    On a related note, the percentage of VB programmers that don't know how to escape quotes is scary, even considering the sample population is VB programmers.

    In all fairness, I do find it kind of counter-intuitive. Why are backslashes too scary for basic?

    Why does all code, regardless of what it was written in, become another problem with VB programmers. Second, vb.net doesn't support the " escape character as far as I know, you need to use "" to represent a dbl quote in a string. I would have used string.format("""{0}"",""{1}""",vara,varb). So we're not afraid of backslashes, we don't need to use them.

  • Occasional Reader (unregistered) in reply to akatherder
    akatherder:
    Daniil:
    Ha. I find that code very funny. Funny as in the most useless and pointless and CPU consuming from time sharing hardworking operating system way. Luckily for us, people who write those kinds of code exist in every line of industry or life and generally have ratio of 8 to 2 thus keeping life interesting. Who's up for a drink to talk about good looking famous people?

    The ratio is more like 12 to 3 in my experience.

    That's far too optimistic. Try 28 to 7

  • (cs) in reply to akatherder
    akatherder:
    Daniil:
    Ha. I find that code very funny. Funny as in the most useless and pointless and CPU consuming from time sharing hardworking operating system way. Luckily for us, people who write those kinds of code exist in every line of industry or life and generally have ratio of 8 to 2 thus keeping life interesting. Who's up for a drink to talk about good looking famous people?

    The ratio is more like 12 to 3 in my experience.

    to put it in plain terms what akatherder is saying is that the ration should be about 2^32 to 2^30.

  • highphilosopher (unregistered) in reply to eBusiness
    eBusiness:
    The function name is clearly off, apart from that I don't see anything but normal not-that-well-written code. It's not like the performance or readability otherwise is worse than a lot of other code.

    Mr. eBusiness, what you've just said is one of the most insanely idiotic things I have ever heard. At no point in your rambling, incoherent response were you even close to anything that could be considered a rational thought. Everyone in this thread is now dumber for having listened to it. I award you no points, and may God have mercy on your soul.

    Sorry, I've always wanted to use that quote. The function name is off, but nothing else matters. Not naming function names somewhat close to what they're supposed to do is one of the worst heresies in the programming realm. It's comparable to building an airplane, and calling it a car. Every other person of the same language will get really mad at you. You went against a well-known-labeling scheme, and nothing bothers people more than when you mess with their labels.

  • (cs) in reply to Occasional Reader
    Occasional Reader:
    akatherder:
    Daniil:
    Ha. I find that code very funny. Funny as in the most useless and pointless and CPU consuming from time sharing hardworking operating system way. Luckily for us, people who write those kinds of code exist in every line of industry or life and generally have ratio of 8 to 2 thus keeping life interesting. Who's up for a drink to talk about good looking famous people?

    The ratio is more like 12 to 3 in my experience.

    That's far too optimistic. Try 28 to 7

    Ha! I'd go as far as to say 80%.

  • (cs) in reply to Vikrant
    Vikrant:
    I don't see the WTF.(Except for the function name).

    I have seen code written by a major database company that has exactly the same functionality. They were doing this for creating dynamic SQL Strings and instead of writing '''' many many times, they preferred creating a java function for it.

    Please tell us which database company, so I can avoid their products (I suspect it's Oracle - their C++ was shite back when I had to use their API and I doubt their Java's much better).

  • Anon (unregistered)
    1. What is the try/catch for? Sure you wouldn't expect StringBuffer.append to throw an exception unless you are running out of memory or something, but a try/catch that rarely (if ever) actually throws an exception probably has little performance impact.

    2. Why is the delimiter not a class variable or a parameter? If all you ever need to do is enclose a string in quotes, why write a (not much) more complicated general purpose function that takes an extra parameter or worse hide the delimiter as a class variable which I would argue would be less readable?

    3. What the $§%& does the method name to do with the content? If the function had a sensible name (like quoteString) it would be better. So TRWTF is that they gave the method a name which doesn't describe what the method does.

  • no one (unregistered)

    I think it's worth noting that in Java the plus operator actually uses a StringBuffer behind the scenes anyway, so constructing your own in this simple case just adds needless wordiness.

    Also, I hate these terrible catch blocks. Do you really want to return an empty string when there's an error? REALLY??

  • (cs)

    If you really must:

        private static String quoteString(final String str) {
            String ret = "";
            if (str != null && !str.isEmpty()) {
                StringBuilder buf = new StringBuilder();
                buf.append('"');
                buf.append(str);
                buf.append('"');
                ret = buf.toString();
            }
            return ret;
        }
    
  • oops (unregistered) in reply to frits
    frits:
    Occasional Reader:
    akatherder:
    Daniil:
    Ha. I find that code very funny. Funny as in the most useless and pointless and CPU consuming from time sharing hardworking operating system way. Luckily for us, people who write those kinds of code exist in every line of industry or life and generally have ratio of 8 to 2 thus keeping life interesting. Who's up for a drink to talk about good looking famous people?

    The ratio is more like 12 to 3 in my experience.

    That's far too optimistic. Try 28 to 7

    Ha! I'd go as far as to say 80%.

    and by 80 you mean 75?

  • (cs) in reply to oops
    oops:
    frits:
    Occasional Reader:
    akatherder:
    Daniil:
    Ha. I find that code very funny. Funny as in the most useless and pointless and CPU consuming from time sharing hardworking operating system way. Luckily for us, people who write those kinds of code exist in every line of industry or life and generally have ratio of 8 to 2 thus keeping life interesting. Who's up for a drink to talk about good looking famous people?

    The ratio is more like 12 to 3 in my experience.

    That's far too optimistic. Try 28 to 7

    Ha! I'd go as far as to say 80%.

    and by 80 you mean 75?

    No. Learn how to count. 4 out of 5 idiots think 75% is 80%.

  • gus (unregistered)

    now now, it could have been a teensy bit worse. At least they did not make a separate stringbuffer for the quote, and they factored out the quote instead of using the literal twice. So give them two small pluses, among the dozen minuses.

  • Frank (unregistered) in reply to Anon

    For those complaining about the method name and its content... From its usage it probably use to insert a comma.

    Ok the person was a little too lazy at the min a comment should of been added.

    At the most this item gets a quick rolling of the eyes and when you get around to it a copy of the method and renaming it and all the calling locations.

  • Stephen (unregistered) in reply to toth
    toth:
    Hey, at least he used a StringBuilder instead of concatenating one string. Not that that would probably make much difference in this case.

    It does not matter. Had he just concatenated the strings, the compiler would have used a StringBuilder anyway.

    If there is no loop, method call, or variable modification involved in concatenating strings, just concatenate them. The bytecode will be identical and the code will be easier to read.

  • RandomNameOfHiding (unregistered)

    I think the method was originally meant to be used like this:

    String result = '"' + varA + insertComma(",") + varB + insertComma(",") + varC + '"';

    And was then subverted by later developers to be used like mention in the post:

    String result = ... + insertComma(varA) + "," + insertComma(varB) + ...;

  • anonymous internet wanker (unregistered) in reply to oops

    don't talk about midgets like that. they prefer the term "little people".

    unless you prefer the term "stupid" over "math-challenged". ;-)

  • (cs)

    If you must use a function to do your dirty work, then at least use a variadic function and let it handle the +","+ as well. It will look cleaner and make you look less silly.

    String resultA = commaJoin(argA, argB, argC); String resultB = commaJoin(argB, argC);

  • (cs) in reply to Stephen
    Stephen:
    If there is no loop, method call, or variable modification involved in concatenating strings, just concatenate them. The bytecode will be identical and the code will be easier to read.
    The stupid thing is that it's highly inefficient anyway because it sends strings back and forth across a method call rather than sharing the StringBuilder over the whole outer call. The right thing would have been to present each row as an array or List of Strings (or, better yet, Objects). Then they could be assembled much more effectively to make a whole row of CSV output.
  • Edward Royce (unregistered)

    Hmmmm.

    Needs XML.

  • (cs) in reply to Dignissim

    On a related note, I am a VB programmer. I do know how to escape quotes, and backslashes are not too scary, it was just decided that the string parsing subroutines would be quicker if they did not need to check for control codes to be converted into the proper ASCII values.

    On the other hand, the code above is C# and not VB (hint: VB doesn't use semi-colons and curly braces).

    Addendum (2010-01-06 10:46): The above code could be Java. It's simple enough to be either, though from other comments, it would seem that Java has the types used built-in.

  • Bob Granger (unregistered) in reply to toth
    toth:
    Hey, at least he used a StringBuilder instead of concatenating one string. Not that that would probably make much difference in this case.

    If you use a String, the Java compiler uses StringBuilder under the hood.

  • Anon (unregistered) in reply to smbarbour
    smbarbour:
    On the other hand, the code above is C# and not VB (hint: VB doesn't use semi-colons and curly braces).

    Try again, .NET doesn't have a StringBuffer, it has a StringBuilder. You must be a VB 6 programmer or you'd know that. The camel casing suggests Java.

  • (cs) in reply to dkf
    The stupid thing is that it's highly inefficient anyway because it sends strings back and forth across a method call rather than sharing the StringBuilder over the whole outer call. The right thing would have been to present each row as an array or List of Strings (or, better yet, Objects). Then they could be assembled much more effectively to make a whole row of CSV output.

    If you're using a BASIC dialect like that I'm pretty sure performance isn't your biggest concern. Unless it starts showing up in a profiler, I think clarity, ease of use, and ease of implementation should be your biggest concerns in these situations. Who knows, maybe they're using it in a high use area? It just doesn't seem like it would be a very big issue.

  • Procedural (unregistered)

    Not great code, but not as much of a WTF as you might think:

    1. Anybody who has done database exports and imports of large data sets knows that memory (and memory limitation errors) is a persistently nagging problem. Catching it as it occurs as a quick-hack way of knowing how long your string can be (perhaps to show that information to the user, e.g., do not export anything beyond the sales records for Ohio) is not a bad idea if you can't afford to double-parse your data set (once to count and pre-allocate the required amount of memory without going hog-wild on multiples, once to directly set the strings in place without using stack- or heap-costly concatenation.)

    2. It was probably a shortened method name: InsertCommaDelimitedCompatibleValue or some such.

    Verdict: Story: meh. Code: not WTF-worthy.

  • (cs) in reply to Bob Granger
    Bob Granger:
    toth:
    Hey, at least he used a StringBuilder instead of concatenating one string. Not that that would probably make much difference in this case.

    If you use a String, the Java compiler uses StringBuilder under the hood.

    Really? Huh. I guess I've been away from Java too long, it's gotten a bit rusty.

  • bleech (unregistered) in reply to frits
    frits:
    oops:
    frits:
    Occasional Reader:
    akatherder:
    Daniil:
    Ha. I find that code very funny. Funny as in the most useless and pointless and CPU consuming from time sharing hardworking operating system way. Luckily for us, people who write those kinds of code exist in every line of industry or life and generally have ratio of 8 to 2 thus keeping life interesting. Who's up for a drink to talk about good looking famous people?

    The ratio is more like 12 to 3 in my experience.

    That's far too optimistic. Try 28 to 7

    Ha! I'd go as far as to say 80%.

    and by 80 you mean 75?

    No. Learn how to count. 4 out of 5 idiots think 75% is 80%.

    successful troll is successful

  • j1 (unregistered)

    maybe its used to send the developers into a coma

  • alendar (unregistered) in reply to BlueKitties

    That's a good solution. I did not know you could have a variable number of parameters in Java. I would name it "prepForCSV" or something that describes the most likely business use of the function. The function also needs to scan the strings for quotes and escape them, but that depends on the client that would be reading the CSV, which makes it troublesome. Still, it should be escaped so at least its apparent that a miscreant quotation is present. Or replace it with a single tick.

  • jordanwb (unregistered) in reply to Anon
    Anon:
    Bob:
    frits:
    Anyone heard of StringBueller? Anyone? Anyone?

    Not personally... no. Heard of StringBuilder though!

    Whoosh!

    Like a supersonic jet.

Leave a comment on “Insert Comma?”

Log In or post as a guest

Replying to comment #:

« Return to Article