- 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
ToUpper() anyone ?
Admin
I prefer ToLower(). It saves space!
Admin
I prefer tolower. Or Tolower. Or TOlower. Or TOLower. Or TOWLOwer. Or TOWLOWer. Or TOWLOWEr. Or TOWLOWER....
Admin
Yepp. :) But Capitals look better.
By the way: ToLower is ofter faster than ToUpper. And saving microticks of CPU-Power is very imporant nowadays.
Regards
Admin
Ugh, there's a lot of this kind of code in our product, mostly because it was tossed in around `98, back when Java was the Hot, New Thing, and a lot of the methods didn't actually exit. There was no appropriate sort method, so we had to write one. And create an interface to indicate that things could be sorted. And toss that interface into EVERYTHING.
There's a lot of ugly, old code that I don't dare touch in the product, because it works just fine.
Normally, I'd rush to the defense of this code, saying things like "It might be time critical; converting to lowercase might take too long!" and "The string might be several megabytes!" but I'm guessing that this is part of a library for SQL calls, so the strings won't be much longer than 1k.
Then again, this is TDWTF.
Admin
Could you possibly be a veteran of the PLATO system? The native word length on the CDC host was 60 bits, so this held 10 six-bit characters. Capital letters were represented with a shift code followed by a letter...so capital letters took up twice as much storage as lower case.
With more special codes to give a diacritic to the previous letter, half-spaces, backspaces, etc...centering the contents of a string on the screen could be tricky. (In our projects, we determined display length by printing the string in "mode erase" (black-on-black) and then noting the location of the cursor)
Admin
toLoler()
Admin
Good thing they weren't looking for "pneumonoultramicroscopicsilicovolcanoconiosis"...
Admin
Okay, I'm a rusty old programmer, but won't this think that 'intMandy' is part of an 'AND', or is it smarter than that?
Admin
I see it grew an extra 'W' at some point there...
Admin
This was obviously written in a shop that rated programmer performance by lines of code. Still, the programmer could have boosted his/her performance even more by unrolling those nasty loops and complex table lookups ...
Admin
Not to mention that the 3 methods do exactly the same, that there probably is an IndexOf equivalent in Java... And case insensitivity is just an extra parameter in .NET when using IndexOf.
Admin
Calling either ToLower() or ToUpper() is, in general, a flawed way of performing a case-insensitive comparison, although it does depend on the platform you're using. It assumes that there's only one way of upper/lower-casing a string - when in fact it's culture-dependent. For instance (and this is where I first learned about the problem - it bit me) in Java, this code doesn't always return true:
"mail".toUpperCase().equals("MAIL")
In Turkey, the upper case version of "i" isn't "I".
Where possible, it's better to use a case-insentitive comparison instead of equality (.NET is good on this front). Otherwise, you can use the invariant culture (or the equivalent on your platform) to get the same results regardless of the default culture of the environment you're running in.
i18n has a lot to answer for.
Admin
I tend to use ToLowerInvariant() or ToUpperInvariant(), for the Turkish ı->I, i->İ reason mentioned previously.
Admin
Hey, at least the code has comments!
Admin
How old is this code? it's hard to believe that in 2007 there are still people who wouldn't know that any language has helper classes for something like searching for strings. Maybe 10-20 years ago it would've surprised a few more people, i dunno... its a shock to see this. I wonder how many company man-hours are wasted reinventing these wheels.
Admin
Admin
This is .net code no? I know in java there is a string compare function that ignores case so there is no need to call .toUpper or .toLower. My .net skills are a bit rusty does such a compare fuction exist?
Admin
I bet this dude is not only a new programmer but also gets paid by line of code
Admin
Has anyone in the history of programming ever typed "oR"? It's not all caps, all lowercase, or english caps. If I ever saw anyone programming in leet speak, they'd get a shiv in the ribs.
Admin
Admin
http://www.lolcode.com
You'd better get your shivs ready.
Admin
I was about to suggest the same thing. Bleh.
Does anyone actually still get paid by line of code? I'd have thought that procedure was gladly defunct in all but the most retarded of institutions now.
Admin
String.toLowerCase() from a JDK 1.0.2 API reference copied from the sun website in june 1996, as the site says. I couldn't actually find the docs on Suns website anymore :(
P.S. You probably meant "exist", although I have indeed seen alternative implementations of standard String methods that would (given the right inputs) never "exit", either.
Admin
The Real W ... er ... I mean, I wonder why he doesn't break out of the loops when he finds a match. That would certainly save time.
Also, he could save the arrays somewhere along with a score for each string, and then increment it on every call. Then he could order the strings by score, and thus increase the likelihood of getting an early match.
Admin
[quote user="Someone"][quote user="bpk"]This is .net code no?[/quote] No. .net methods are pascal-cased (first letter is a captital).
it's not required that they be pascal-cased and on a site that showcases bad code can you really assume it's not .net just because it isnt pascal-cased
Admin
Yes, I did. Sadly, I'm not allowed to edit my post (thanks Alex).
This wasn't a simple string compare, it was a "Does this string exist inside of this other string?" for which the appropriate usage is .indexOf (which does not have a case insensitive version). I can easily see this happening with someone who is new to Java, and is spooked at the idea of casting .toLowerCase, for fear of modifying the original string.
Admin
final is a java keyword if it was sealed I would say .NET
Admin
Or what ever your locale is? It is in the APIs, and any code checking software (find bugs and the like) will throw an error on it.
Admin
I am one of those scarred PLATOites. Many idioms had to be used every day in coding, as the original Tutor language was "designed" by "nonprogrammers".
The wonderfull and horrible "Arrow" command-- "judge rejudge", "press Shift-Stop" to end a program, er, "lesson". No subdirectories. Requiring a call to the sysop to have a file, any file, created. A globally-smeared file name space, so if somebody in Iran named a lesson "hangman", nobody else could. Having only 150 60-bit variables worth of RAM. Allowing only 640 characters in each source code page, because once there had been a disk drive with that sector size. No visible cursor. The grab-bag of embed codes. The pneumatic slide selector. The proprietary non-error-checking 1800 baud modems. The low reliability of the 2megaword ECS memory, which went down for about an hour a day, most days. The slowness of CDCNET connections. The awful Viking 721 keyboard feel. The list is almost endless.
Then again, saved and shared common was wonderful.
Admin
Admin
We should all remember that posts are often anonymized by showing the code in a different language. The real offending code may well be in a language other than Java.
Regardless of what the real language is, we all know every language even ten years ago had ways to do this sort of thing in one or two lines. When I read this code, I get the sense that it was written by someone who's used to programming in assembly.
Admin
My emacs window is colored orange on black in homage to my PLATO roots.
OBwtf: The 150 60-bit per-process variables were named n1 through n150 if being used as integers or strings and v1 through v150 if being used as floating point. However, a facility was available to define more meaningful aliases for these variables.
But it wasn't all bad...ordinary text files could contain animation, the 512x512 screen was pixel addressable or could use a programmable character set (amazing for time period), and variables smaller than 60-bits could be packed (an array of 16 7-bit values would only take 2 of the 60-bit variables).
Admin
This is supercalifragilisticexpialidocious! Or supercalifragilisticexpialidociouS. Or supercalifragilisticexpialidocioUs. Or...
Admin
He is looking for the first (hence the name "min"). Breaking out will find a match.
Admin
Wait... you mean that this isn't a site to borrow code from? But you are on the front page of Google for most of my coding problems...
Admin
It's still Java.
For some reason the Java developers decided that, while it was perfectly reasonable to abbreviate "integer" as "int", abbreviating "boolean" to "bool" was out of the question. C# uses the "bool" keyword; this code uses "boolean".
But more to the point of the post you're replying to, there are calls to built-in functions in string: "int min=expression.length();" for instance. In .NET that would be "expression.Length". So while the programmer could have used camel-casing on his own methods, he still has to use Pascal-casing on the existing ones.
Admin
C'mon, no one is giving this programmer credit. The combinatorics are right.
The 3 letter AND, NOT need 23 (yes, I learned Fortran) = 8 variants, and the OR needs 22 = 4 variants. At least, the arrays cover all the cases.
Admin
My guess was for wide chars.
Admin
Admin
Then why didn't they use 'bool'?
Wikipedia gives the earliest version of Java, 1.0, released Jan 23, 1996. 'bool' was discussed in The Design and Evolution of C++, published in 1995, so it was at least out in the open that it was a likely addition at that point even if it wasn't in the language at that point. (It's not in the ARM, from 1990. I don't know if it was added between then or not until standardization.)
I also think that if they were concerned about compatibility with the C++ mindset, 'int' vs. 'integer' should been the least of their worries.
Addendum (2007-07-02 18:12): Upon further investigation, it seems that (1) bool had been accepted by the standards committee by November 1994, and (2) reviews of D&E were out as early as August 1994.
In other words, there was really no excuse for Java to not follow in C++'s footsteps if they wanted to. I can't imagine that the inertia behind "boolean" would have been too great to overcome by that point.
Admin
Admin
Obviously the code is far too inflexible. They should have used a dictionary with the key of the lower case letter and the value of the upper case letter and used that to build all the combinations of the word you are trying to find the index of. (joke, obviously)
Admin
The only reason "int" was not expanded to "integer" was to be C/C++ compatible. I wouldn't be surprised if even that was debated at length.
Admin
Dang! memcmp is case sensitive! :D
Admin
Mmm, O(n^2) text search algorithms...
Admin
No excuse? There was 14 months between Java's release and the acceptance of bool into the C++ standard (by your figures). Do you think that Sun rolled out Java the day after it was built? I doubt it, and the amount of time, effort and money required to make such a subtle and inconsequential change would make it prohibitive anyway.
Admin
Hence the bit of my post which you snipped:
It's better to use the invariant culture if there is one defined, rather than "UK" - it indicates the intent more clearly, IMO.
Jon
Admin
hmm FindOr(), FindNot()....
try { findAnd(); break Yoda; } catch (WTFException wtf) {}
Yoda: FindOr(); FindNot(); // there is no Try
Admin
Ah. And what did you think this is for when it bit you? ;o) (see below) BTW, toLowerCase() without parameter does the same using the default locale...
from java.lang.String:
public String toUpperCase(Locale locale)
Converts all of the characters in this String to upper case using the rules of the given Locale. Case mapping is based on the Unicode Standard version specified by the Character class. Since case mappings are not always 1:1 char mappings, the resulting String may be a different length than the original String.
Examples of locale-sensitive and 1:M case mappings are in the following table.
I