- 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
Bugger off, troll.
Admin
This is not being transleted to StrangBufer calls.
Admin
The only reason I can think of is when the ordering of the enum entries is meaningful. In such a case, you might use it to sort values for a drop-down box, or to compare whether one value is logically before or after another...
Admin
Admin
Not necessarily. A compiler needs to be smarter to translate the loop version into StringBuffer calls, but if the string is never read within the loop, it's a reasonable optimisation to make. I don't think Java 6 was smart enough to do it, but 7 might be...
Admin
I doubt the javac compiler will, but if you mean hotspot, then sure, I can see it in some cases unrolling the loop. But in the general case (such as reading until end of stream), it can't.
My point was primarily about those kinds of loops being the only case where we (might) need to worry about explicitly using SB, as opposed to simple s = a + b + c + ... cases, which people often erroneously think lead to Java's "inefficient" string concatenation.
Admin
Oh, I guess I missed part of your point. As to the bit about the String not being read, I don't think that matters. The SB approach is the implementation that the current Oracle compilers happen to use, as would most compilers I assume, but it's not required. So I don't think that an optimizer can assume there are no side effects that need to be preserved.
We're far into an area where I don't really have any expertise though, so I could be wrong about this part of it.
Admin
During code reviews, somebody I know likes to replace "should never happen" comments with:
When the original coder complains, he simply points out that it should never happen, so the System.exit line shouldn't be a problem, right?
Admin
I think you've still missed the point of the post - we're not talking about runtime optimization at all. What I'm saying is that the optimizations performed by the compiler can be extended to cover the loop case. That is, the example you give above should result in identical bytecode to:
Now, I'm not aware that any existing compiler takes the optimizations this far. But there's no reason I can see why that couldn't change in future, if someone thought it would be worth it.
Admin
Okay, so, yes, IF javac starts doing optimizations it has typically left for hotspot, and IF it can confirm that the String is never read inside the loop, then, yes, it could do that optimization, skipping the unneeded creation of intermediate Strings.
Admin
Okay, so, yes, IF javac starts doing optimizations it has typically left for hotspot, and IF it can confirm that the String is never read inside the loop, then, yes, it could do that optimization, skipping the unneeded creation of intermediate Strings.
Admin
Although, again, this is an aside from my original post in this subthread, the point of which was that we don't need to worry about using SB outside of this situation.
Admin
I actually have a ShouldNeverReachThisPointException (a RuntimeException) which I use for that kind of situation. Mostly it's there for cases where it really can't reach that point but the compiler isn't smart enough to recognise it - keeps the compiler happy, and serves as clear documentation...
Admin
Confirming it's never read shouldn't be too hard. Within the loop, the value must never be assigned to another string or passed as a parameter to a method call, and none of the string's methods may be called. That'd cover the basic cases, but you could go a bit further, depending on how much complexity you want to add to the compiler...
Admin
Troll tera baap!
Admin
Hard, no. A significant deviation from the approach for the last, what, 8-10 years, of saving the optimization for the runtime, yes. Not that it couldn't be done, just that it would be a philosophical shift.
Admin
I often just use AssertionError for this. Even though it's an Error, not a RuntimeException, It's purpose fits the bill.
Admin
... or sending it across the network to another computer... or saving it to a file, or loading it from a file, or handing it off to another process via shared memory or pipe or whatnot.
Basically any time you are handing the value off to some piece of code that wasn't guaranteed to be compiled at the same time as the current code, then the underlying value becomes important.
Admin
Yes, I suppose that would be the most appropriate choice... and being an Error, it has the benefit of not being caught by the random-monkey coding that considers "catch (Exception e){}" blocks to be an appropriate form of error handling.
Doesn't do anything about "catch (Throwable t){}" blocks, of course. But frankly, doing that without good reason should be a sacking offense...
Admin
If you need to serialize your enums, you should be assigning an explicit value to each member that is documented to require stability, not relying on the implicit ordinal that someone else might change without realizing it could ever break anything.
Pretty much the only sound use I can think of for Enum.ordinal() is in the implementation of java.util.EnumSet and java.util.EnumMap. And your JRE developers already wrote those for you.
Admin
Admin
Admin
TRWTF is Java, as always. Mwahaha
Admin
How terribly original. That never gets old and is not the least bit obvious or pathetic or unfunny. I hope every single wtf that even mentions Java, or where any of the people involved have ever heard of Java, or have drunk a cup of coffee, makes the hilarious observation that "TRWTF is Java", as this shows a level of insight and sophistication that can do naught but elevate the value of this site, propelling its humor and educational value to the stratosphere.
Thank you so much, my friend. We all owe you an enormous debt of gratitude. And, though it be rude of me in the extreme to dare ask more of you, might I please entreat you to come to my house every morning and wake me up be screaming in my ear that "TRWTF is Java"? Being reminded of that perfectly correct, perfectly valuable, always relevant, never trite, and forever inspirational bit of wisdom is the best way I can imagine to start my day, short of feeding my nuts into a meat grinder while squirting boiling dog vomit up my nose.
Admin
So, after reading through all the comments I fail to understand why enum would have an implicit conversion to integer. Surely the only valid comparison is to the enumerated type values (how that is done across compilation units would be an implementation detail for the javac). Since this is so important, I say TRWTF is the Java language reference, and the thousands of shitty books which show anything other than comparison to an enum'd value. Furthermore, while I do understand the need for a ToString member, the description of that function should contain a huge warning that the string representation should not be used for comparing enums.
Admin
Apart from idleness, ignorance and foolish childish arrogance, is there any reason not to program such a loop like taht in the first place?
Admin
In such cases I have been known, so as to ensure backward compatibility, to implement a "dbValue()" to return the specific number that is stored in the database to identify this enum.
What you do is create the enum with a constructor containing all the ancillary information: database value, UI-friendly description, etc. All the tedious infrastructure associated with that enum can also be programmed into it and the interface can then be as clean as you like.
Oh, and sorting on the enum is as simple as being determined by the order in which they are declared, but there's nothing wrong with writing as many comparators as you like, and even define extra sort fields if you've got a really challenging set of requirements.
But it should never be necessary to use the ordinal().
Admin
You mean why would one not use the preferred append() approach, and instead fall back on the += approach? Sometimes if I know the sizes and iteration count will be small, especially for things like developer tools and one-offs, I'll be lazy and go with the +=. But otherwise, no, there's no good reason to use += over append().
But I don't think anybody was advocating += anyway.
Admin
Back when I was a kid and was playing games in DOS, I always had to tweak memory to run this or that game. I absolutely hated Aldo Adventures cause it didn't clean up after itself, leaving a gaping memory hole behind. Fun game, but seriously huge memory leak! If you write the same simple game nowadays, and write it as shitty as they obviously did (otherwise, a little 8-year old kid wouldn't be cussing at something not cleaning up its memory usage), you won't have that memory loss after the game's been closed, thanks to those high-level languages.
So on one hand, yes. High-level languages allow people to write crappy code without any real consequences (besides maintenance and such). On the other hand, no. They're not worse than low-level languages because of that, since they actually keep it in mind and automatically provide tools that deal with the worst problems.
Admin
And I wrote my comment at three in the afternoon, whilst I get up at six in the morning, and despite that I'd been writing some damn good code before, my brain wasn't fried.
Then, in the evening, I totally sucked at playing Just Cause, but hey, can't have everything.
Admin
I would consider allowing us to get away with writing crappy code by cleaning up our mess for us to be a major negative.
On the other hand, I don't consider not cleaning up the memory I've used because the language is designed to do that for me as "writing crappy code." It's using the language the way it was intended to be used.
Admin
Admin
Can't tell if this is a troll or not. Just in case, StringBuffer was introduced right at the start, version 1.0. The more efficient (for single-threaded usage) StringBuilder was introduced in 1.5.
Admin
Corrected, I stand.
Admin
Umm.... gosh if you put it that way, yes you should have some idea of what you are asking the machine to do, just like every other technical profession.
Admin
I am troll (this is a good thing), but in this case, I just forgot - Java and Java was soooooo long ago for me.
Anyway, strings in Java can be fucked up in various way
Admin
Admin
Admin
Admin
But my "hats off" to the "brilliant" architect who knew that it WOULD happen -- every time they add another enum value!!!
Admin
And then it could be inlined.
Admin
Admin
Thanks for the reply, it was awesome :) Though a bit bloated...
Admin
TRWTF is unfunny wall of text
Admin
I really like Java Enums because they can be used to extend other classes or implement interfaces, and create a set of related singleton classes. Also the ability to create an abstract method in an enum makes things like a set of comparators for an object, functions, and filters easy to manage. It's nice to be able to write something like transactions.Sort(Transaction.Comparators.PRICE), or filter.execute(parameter1, parameter2). The nice thing is that the enum can be used by the UI to make a drop-down of available Comparators.
Admin
"The dark side of the source clouds everything. Impossible to see the feature is."
Admin
Admin
Admin
Admin
Enum type in Java is a class by definition, but many programmers tend to forget this, because they rather relate it to "a list of allowed values" as in some other languages. It's more than that.
So, to avoid those switch statements it might be reasonable to put some code and additional methods in the enum class. There's almost never a need to create a separate "enum-like real class".