- 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
This makes perfect sense. If SQL would ever be translated into Swahili, you just have to change a couple of constants. It's called defensive programming.
Admin
This is pretty good. I never thought of it myself, but the character used for a COMMA could actually change in the future...
Admin
I suppose if you really want to write truly defensive code, that you could write that procedure with strings and dynamically compile and execute it...
Admin
What happens when you translate SQL into a language with different word order conventions, though? Adam's coworker needs to combine the flexibility of guarding against new rules of punctuation with this egregious example I had to deal with once:
string.format("{0} {1}, {2}, {3} /* snip */ ", SELECT, COL1, COL2, COL3, ..., FROM, TABLE, WHERE, ...)
Admin
This is actually happening to me. The project manager insist that if it is a string, then it has to be a constant and the rest of the code should reference the constant. And there is not way to reason with this guy, he wants to rule applied globally and blindly.
Admin
That's what happens when you tell people "string literals are bad, [n]NEVER[/b] use them" - they start to create string literals like
COMMA = ','
or
OPEN_PARENTHESIS = '('
or my personal favorite:
LETTER_CAPITAL_A = 'A'.
Admin
return LETTER_I + SPACE + STR_PITY + SPACE + STR_YOU + DOT + DOT + DOT;
Admin
Does he not know that the .NET compiler does that automatically with string literals?
Admin
sb.append(OPEN_PAREN);
should be
eval ( SBAPPEND OPEN_PAREN CONSTANT_OPEN_PAREN CLOSE_PAREN SEMICOLON )
except I can't figure out how to concatenate the constants, and I still haven't eliminated the parens.
Moar recursion mebbe?
XML?
Admin
OMG! Ponies!
(trust me on this, there are hidden ponies here and there)
Admin
Captcha: jumentum, as in "Converting all of our strings to constants caused us to lose our jumentum".
Admin
Nicely formatted SQL in code? Why not? dim s as string = <sql> Select Col1, Col2, Col3 From MyTable <sql>.toString()
Admin
If this code is called a lot, then standard Java™ practices state that you should use constants. When you use ",", it creates a whole new instance to a string which will need to be garbage collected (when the garbage collector gets around to it). The author is using StringBuilder, which has the advantage of creating only one extra string after all the constants are added. Whoever submitted this was a Java™ newb (or the real Nagesh).
Effective Java (Item 5), Joshua Bloch Sun Microsystems, 2008
Admin
Not enterprisey enough! It should make a call over the Enterprise Service Bus to a web service to do the construction of the queries to look up the constants for concatenating to make the query string. Like that it'll be fully future-proof.
(And if it's like real enterprise-grade software, it'll have the host IP address of the web service encoded as a number directly in the code. Maybe the MAC address too. It's not enterprisey if it isn't insanely fragile and sclerotic…)
Admin
Having things like the comma be constants is stupid and a WTF; having the column names be constants however is not, since you run the risk of spelling typos by hard-coding it. Other than the comma thing, this is a common "builder" pattern when one can't use an ORM.
Admin
Start changing the constants to make it really confusing for him and everyone else suchas this wonderful code:
NOT_TRUE = false NOT_NOT_TRUE = true NUMBER_ONE = 10 MY_NAME_IS_FUCKING_IDIOT = <managers name>
Admin
You know, I've seen a lot of stuff like this, and I've probably even written stuff like this (YIKES!). But I find that most of the time the issue is that the programmer just doesn't understand the API's and what's possible with the framework.
I bet when the guy who originally wrote this looks back on it in ten years (since it's public now, haha), he's going to say, what was I thinking?!?
LOL - this is just awesome - I've seen so much stuff like this. Let me give you an example:
Admin
Uh. NO.
Java will build a lookup table with strings so they all reference the same string (if the same string is indeed referenced multiple times) until the last reference goes out of scope; here GC will probably kick in a clean up the string.
So "," will not create a whole new instance each time.
[quote user="Nagesh"]If this code is called a lot, then standard Java™ practices state that you should use constants. When you use ",", it creates a whole new instance to a string which will need to be garbage collected (when the garbage collector gets around to it). The author is using StringBuilder, which has the advantage of creating only one extra string after all the constants are added. Whoever submitted this was a Java™ newb (or the real Nagesh).
Admin
[quote user="What?"]Uh. NO.
Java will build a lookup table with strings so they all reference the same string (if the same string is indeed referenced multiple times) until the last reference goes out of scope; here GC will probably kick in a clean up the string.
So "," will not create a whole new instance each time.
[quote user="Nagesh"]If this code is called a lot, then standard Java™ practices state that you should use constants. When you use ",", it creates a whole new instance to a string which will need to be garbage collected (when the garbage collector gets around to it). The author is using StringBuilder, which has the advantage of creating only one extra string after all the constants are added. Whoever submitted this was a Java™ newb (or the real Nagesh).
[/quote]
Do you care to back up that incorrect statement? I gave you my reference.
Admin
That's not the point. Defining string literals as constants has indeed various benefits, like making it a lot easier to change them later. I just stops being useful when you add string constants for things which are guaranteed to never change, like keywords in another language. Another case of following the rule but not the intention is to name string constants by their string content.
Admin
My favorite:
StringUtils.EMPTY
Admin
Here, read my white paper on String: http://charles.forsythe.name/documents/10180/0/java.lang.String.pdf
Admin
Bah! At least it's not loaded from an XML file or from the database.
It's funny thought, that almost every project I've seen its code, ends up implementing its own ORM abstraction to do this kind of stuff.
Admin
It might have been that the author was uncomfortable embedding code in strings (after all, an accidental keystroke that isn't noticed could break it, and nobody would notice until the code ran at run-time, or perhaps reviewed in source control). At least with symbol names the compiler can check at build-time instead of run-time for simple mistakes like that. I don't find it overly hard to read either. Unnecessary, but not the end of the world. I see it as a normal phase in a beginner's growth. EXP++;
Admin
StringBuilder sbComment = new StringBuilder(); sbComment.append(FRIST); sbComment.append(EXCLAMATION);
//crap, took too long
sbComment.prepend(SPACE); sbComment.prepend(NOT);
return sbComment.toString()
Admin
Translation: I don't care if you ARE an aircraft carrier, I'm a lighthouse.
Admin
Admin
I'm surprised that no one has pointed out that the the StringBuilder and append calls are a complete waste of CPU and memory. In Java, concatenation of String literals occurs at compile time. Instead of being stupid and inefficient, this method could at least be stupid and efficient.
Admin
The other WTF is the fact that they used a StringBuilder, but did not chain the append() calls.
Admin
From your own reference, only if you explicitly create a new string using the new operator does it create a new string. Good troll though, intentionally misreading the reference that points out you're wrong under the assumption that people would look for a /different/ reference instead of just read yours.
Admin
Why are you all assuming it's Java?
Could equally be c#.
Admin
Admin
Actually probably is java. The toString method in c# is ToString
Admin
In some code I inherited, there is:
(yes, with the random spelling errors). This is used in gems like:
(with filed variable names to protect the innocent:)). Still wondering why 1 and 2 are still hardcoded. Where is the softcoding police when we need it?
Admin
That's why I wrote the document I referenced. I even cleared up some of my own misconceptions while I was writing it.
Admin
Ponies? You mean unicorns and rainbows, right? I was surprised the first time I saw them too, but apparently there in every article Remy writes. Just view source and you'll see the cornify.
Admin
That would be true IF this was String concatenation. To take it a step further, String concatenation uses a lock to do its work (Strings are immutable.)
The author is using StringBuilder. Backing store is a char[], access is unsafe, and it's faster (no locks - 'tis why StringBuilders should be local to a method or thread-safe object.)
String constants are fine.
Admin
Admin
This is why C++ allows string concatenation across quoted lines and macros:
Admin
Please read http://www.xyzws.com/Javafaq/what-is-string-literal-pool/3
Admin
Sorry, I can only rate this as blue apples out of love, would not troll again. Moving the goalposts is not an interesting trolling technique anymore. U we're dueling sew welt b4, 2.
Admin
Captcha: Hey Persto! They appeared like magic.
Admin
But for the record, I found your paper interesting. :) I even picked up a few things (like the + operator creating a new StringBuilder behind the scenes) though I already knew a lot of it beforehand.
Admin
[ For constant strings only referenced once (as above in scope to the snippet), see Manifest Constant ]
Admin
Admin
Admin
You win you've now convinced the world that .Net sucks and Java is the way to go.
I agree that string literals should be compile time really strange that they are treated like variables in a framework that explicitly treats strings as immutable.
Admin
Also: thread?
Also, too: if you really are clueless and not trolling, you wouldn't understand the explanation anyway.
And: if you really are clueless and not trolling, you came to this site because someone posted your code to it. Go ahead, admit it.
Admin
No no no no no no no! You don't use constants like this!
Doing
does not make sense, as the name already tells you the value (and if you change it it gets even more confusing). You should do it like this:So this code snippet should be
Ah, much better.
Admin
LOL@efficiency, when has that ever been a concern? Then why do people use Java? See if his stack ever unwinded from a concern of efficiency, he would replace each append parameter with a string literal, then to be more efficient, he would skip the append calls and concatenate with operator+... then to be more efficient or readable, he wouldn't use a string builder and use a regular built-in String, then to be more efficient, he would put the string's into longer and readable lines to improve readability for efficiency of the company and compile-time.
Obviously this guy was taught Java in college, where he learned all them nice Object Oriented practices, never was very good at it, and probably chose Computer Science as his major because of the "job market."
Hmm, you can figure out a lot about a person by their code.