Comment On Insert Comma?

"While browsing the code base of a recently inherited project," Joey L writes, "I found this curious method." [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: Insert Comma?

2010-01-06 09:03 • by Rodnas (unregistered)
String result = ... + insertComma(varA) + "," + insertComma(varB) + ...;

Re: Insert Comma?

2010-01-06 09:04 • by Rodnas (unregistered)
294932 in reply to 294929
Rodnas:
String result = ... + insertComma(varA) + "," + insertComma(varB) + ...;


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

Re: Insert Comma?

2010-01-06 09:06 • by Gramma (unregistered)
Seems pretty obvious to me... they're just trying to create a CSV file.

terribly.

Re: Insert Comma?

2010-01-06 09:06 • by 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?

Re: Insert Comma?

2010-01-06 09:07 • by Mike Caron (unregistered)
Gosh, I hope none of the variables used with this method have quotes in them...

Re: Insert Comma?

2010-01-06 09:09 • by random.next
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.

Re: Insert Comma?

2010-01-06 09:11 • by frits
Anyone heard of StringBueller? Anyone? Anyone?

Re: Insert Comma?

2010-01-06 09:14 • by Bob (unregistered)
294938 in reply to 294937
frits:
Anyone heard of StringBueller? Anyone? Anyone?


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

Re: Insert Comma?

2010-01-06 09:16 • by 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!

Re: Insert Comma?

2010-01-06 09:17 • by Anon (unregistered)
294940 in reply to 294938
Bob:
frits:
Anyone heard of StringBueller? Anyone? Anyone?


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


Whoosh!

Re: Insert Comma?

2010-01-06 09:18 • by 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?

Re: Insert Comma?

2010-01-06 09:18 • by 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)) + ...;

Re: Insert Comma?

2010-01-06 09:19 • by 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."

Re: Insert Comma?

2010-01-06 09:23 • by 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.

Re: Insert Comma?

2010-01-06 09:24 • by HUH (unregistered)
294945 in reply to 294942
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

Re: Insert Comma?

2010-01-06 09:29 • by toth
Hey, at least he used a StringBuilder instead of concatenating one string. Not that that would probably make much difference in this case.

Re: Insert Comma?

2010-01-06 09:35 • by 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.

Re: Insert Comma?

2010-01-06 09:38 • by akatherder
294948 in reply to 294941
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.

Re: Insert Comma?

2010-01-06 09:40 • by DaveAronson
comment = insertComment ("");

Re: Insert Comma?

2010-01-06 09:41 • by the beholder (unregistered)
294950 in reply to 294943
[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]

Re: Insert Comma?

2010-01-06 09:42 • by 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 ...

Re: Insert Comma?

2010-01-06 09:47 • by VB Programmer (unregistered)
294952 in reply to 294934
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.

Re: Insert Comma?

2010-01-06 09:47 • by Occasional Reader (unregistered)
294953 in reply to 294948
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

Re: Insert Comma?

2010-01-06 09:50 • by NightDweller
294954 in reply to 294948
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.

Re: Insert Comma?

2010-01-06 09:51 • by highphilosopher (unregistered)
294955 in reply to 294944
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.

Re: Insert Comma?

2010-01-06 09:53 • by frits
294957 in reply to 294953
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%.

Re: Insert Comma?

2010-01-06 09:55 • by java.lang.Chris;
294958 in reply to 294947
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).

Re: Insert Comma?

2010-01-06 09:55 • by 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.

Re: Insert Comma?

2010-01-06 09:58 • by 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??

Re: Insert Comma?

2010-01-06 10:00 • by java.lang.Chris;
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;
}

Re: Insert Comma?

2010-01-06 10:03 • by oops (unregistered)
294962 in reply to 294957
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?

Re: Insert Comma?

2010-01-06 10:10 • by frits
294963 in reply to 294962
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%.

Re: Insert Comma?

2010-01-06 10:14 • by 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.

Re: Insert Comma?

2010-01-06 10:15 • by Frank (unregistered)
294965 in reply to 294959
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.

Re: Insert Comma?

2010-01-06 10:15 • by Stephen (unregistered)
294966 in reply to 294946
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.

Re: Insert Comma?

2010-01-06 10:22 • by 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) + ...;

Re: Insert Comma?

2010-01-06 10:25 • by anonymous internet wanker (unregistered)
294968 in reply to 294962
don't talk about midgets like that. they prefer the term "little people".

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

Re: Insert Comma?

2010-01-06 10:25 • by BlueKitties
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);

Re: Insert Comma?

2010-01-06 10:28 • by dkf
294970 in reply to 294966
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.

Re: Insert Comma?

2010-01-06 10:35 • by Edward Royce (unregistered)
Hmmmm.

Needs XML.

Re: Insert Comma?

2010-01-06 10:35 • by smbarbour
294973 in reply to 294934
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.

Re: Insert Comma?

2010-01-06 10:43 • by Bob Granger (unregistered)
294976 in reply to 294946
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.

Re: Insert Comma?

2010-01-06 10:47 • by Anon (unregistered)
294977 in reply to 294973
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.

Re: Insert Comma?

2010-01-06 10:51 • by BlueKitties
294978 in reply to 294970
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.

Re: Insert Comma?

2010-01-06 10:52 • by 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.

Re: Insert Comma?

2010-01-06 10:54 • by toth
294980 in reply to 294976
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.

Re: Insert Comma?

2010-01-06 10:57 • by bleech (unregistered)
294981 in reply to 294963
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

Re: Insert Comma?

2010-01-06 11:03 • by j1 (unregistered)
maybe its used to send the developers into a coma

Re: Insert Comma?

2010-01-06 11:05 • by alendar (unregistered)
294983 in reply to 294969
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.

Re: Insert Comma?

2010-01-06 11:16 • by jordanwb (unregistered)
294986 in reply to 294940
Anon:
Bob:
frits:
Anyone heard of StringBueller? Anyone? Anyone?


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


Whoosh!


Like a supersonic jet.
« PrevPage 1 | Page 2 | Page 3Next »

Add Comment