| « Prev | Page 1 | Page 2 | Next » |
|
I've seen the first one heaps of times, it's not funny...
Is the comments system broken? Or am I the first to reply? :P |
|
What is wrong with the StringToLong method?
The loop just remove all the zeros from the start of the string. (Yes it is a stupid way to do it, but is there something even worse that I am missing?) |
Re: Breaking Out of the Box
2007-01-22 09:09
•
by
Daniel15
(unregistered)
|
Bah, that came out wrong, I was meant to say something like "I've seen the first one so many times that it's just not funny any more." |
|
My mind has been boggled by these loops
|
Re: Breaking Out of the Box
2007-01-22 09:11
•
by
Bunny
(unregistered)
|
|
Curious, what would be a better way to do it?
|
Re: Breaking Out of the Box
2007-01-22 09:23
•
by
Bunny
(unregistered)
|
Should be in reply to: The loop just remove all the zeros from the start of the string. (Yes it is a stupid way to do it, but is there something even worse that I am missing?) -Sorry |
|
I might point out that the first function isn't really doing the same thing as a break statement would. When ( condition ) is true in the original WTF, /* something else important */ still happens, whereas with a break it wouldn't. Now I'm not saying that the snippet is the best way of writing it, but it's not as simple as just "use break, dammit"
|
|
if ( result == null ) return;
return result; I don't think this will even compile, as a function either returns something (null OK if it is an object) or doesn't return anything (void return type) And I'm not going to waste my time checking this. As for the first one, back in the FORTRAN IV days, I had to use tricks like that; I was pleased with more modern languages and the break statement. captcha: yummy well, breakfast (eating it while reading wtf) was OK. |
Re: Breaking Out of the Box
2007-01-22 09:27
•
by
Richie
(unregistered)
|
|
long StringToLong( String s )
{ for ( int j = 0 ; (j < s.length()) && ( s.chatAt(0) == '0' ); j++ ) { s = s.substring(1); } return Long.parseLong( s ); } |
Re: Breaking Out of the Box
2007-01-22 09:28
•
by
nobody
(unregistered)
|
The break goes after the /* something else important */ And I wonder if that code actually uses the value of i, because it will get 11. |
How about: while(s.length() && s.charAt(0) == '0') s = s.substring(1); |
|
chatAt ?
Thats were you get to chat to a string? |
|
Better way (for example):
s = s.replaceAll("^0+",""); // have not tested RegExp |
|
That's why I prefer proper {}'s.. Properly {}'ed:
It's not _that_ bad - without a break it returns the last item found instead of the first.. ;) ..And shouldn't that be 'charAt' and not 'chatAt'? ;) |
Re: Breaking Out of the Box
2007-01-22 09:30
•
by
TheD
(unregistered)
|
Why would it ever be 11? |
What if s = "0"? Besides, Long.parseLong is perfectly capable of handling leading 0's on its own. |
|
StringToLong
1) I had no idea that Long.parseLong() couldn't handle leading zeros (I'm guessing that it can) 2) I'd feel really nervous about s.length() as a condition in the for loop since we're modifying the length of s inside the loop. This probably only works because there are more non-zero digits than leading zeros (and because the Long.parseLong() handles leading zeros). For instance, an input of '00001' would reduce to '01', not '1'. 3) What's wrong with a while loop? while( s.length()> 0 && s.charAt(0) == '0') s = s.substring(1); return parse.parseLong(s); |
|
The StringToLong is completely foobar..
Why are they even trying to remove the '0's? Im pretty certain that parseLong will succeed at parsing "0002" and give the same result as with "2". |
In Java, JavaScript and others, a leading zero signifies an octal number, just like the prefix "0x" signifies Hex.. Hint: Always specify 10 as the radix.. ;) |
Re: Breaking Out of the Box
2007-01-22 09:40
•
by
LizardKing
(unregistered)
|
The fact that Long.parseLong handles leading zeros:
No loop necessary, despite the idiotic posts that use charAt and substring (they're WTF material themselves). |
Re: Breaking Out of the Box
2007-01-22 09:41
•
by
sys<in
(unregistered)
|
|
What if the spec was "if condition X occurs, set the value of i to 11, do something-else-important, and exit the loop"?
Another possibility is that the value of I has no bearing in the commented-out parts, but the condition else can only be tested at the point where the if test was inserted. Without knowing what the removed parts parts do, there is no way of knowing if that is a WTF or not. captcha:ninjas |
|
long StringToLong (string s){
return Long.valueOf(s).longValue(); } or new Long(Long.parseLong(s)).longValue(); Which really shouldn't even be a method and just done inline (in Java 1.5 at least). |
Re: Breaking Out of the Box
2007-01-22 09:44
•
by
Rich
(unregistered)
|
|
In some languages (e.g.: PHP) string-to-long parsing assumes a 'number' with a leading zero to be octal (base-8), in the same way that a 'number' with a leading "0x" is assumed to be hexadecimal (base-16).
So, "010" would return a decimal value of 8, whereas "10" would return a decimal value of (yes, you guessed it) 10. This looks like Java though -- a quick 30 second look at the API shows that Long.parseLong(String s) ALWAYS assumes decimal. There is a separate Long.parseLong(String s, int radix) that lets you explicitly specify the radix if you need to. I wonder how many WTFs could be avoided by a quick look at the API... =) |
|
Iteration; see loop
Loop; see Iteration |
Seems my Java is too rusty again.. When parsing, the radix is always 10 by default. However, when specifying literals, like "int foo=010;", you get octal numbers with leading zeros. However, when parsing numbers in JavaScript, leading zeros indeed signify octal numbers. That's why many poorly written date selectors fail for august and september.. |
Re: Breaking Out of the Box
2007-01-22 09:50
•
by
dcmet
(unregistered)
|
"0001".TrimStart('0'); A regex should also do it, but it's extra effort. |
Re: Breaking Out of the Box
2007-01-22 09:51
•
by
binky
(unregistered)
|
Oh yeah, forgot about Octal. How about: Long.parseLong(s, 10).longValue(); // base 10 |
Re: Breaking Out of the Box
2007-01-22 09:53
•
by
jrrs
(unregistered)
|
The statement if ( condition ) i = 11;terminates with a ';' so that /* do something else important */ always gets executed no matter what 'condition' is. Setting i to 11 if 'condition' is true just ensures the loop will terminate (eventually?). |
|
The real WTF is that StringToLong doesn't look for a leading "-" even though it returns a signed value.
|
Re: Breaking Out of the Box
2007-01-22 09:58
•
by
Hux
(unregistered)
|
I don't know this for a fact or anything, but does Long.parseLong cause the function to interpret strings with leading zeroes as octal? |
Re: Breaking Out of the Box
2007-01-22 10:04
•
by
poo
(unregistered)
|
Recursion; see recursion |
Re: Breaking Out of the Box
2007-01-22 10:06
•
by
Rich
(unregistered)
|
|
No, in Java it's always base-10.
Please iterate over the earlier posts once again... =) |
Re: Breaking Out of the Box
2007-01-22 10:11
•
by
tchize
(unregistered)
|
Yes, the loop does not remove all '0' but instead does remove s.length()/2 rounded up leading zeros. let it be s="00005" j=0, s="00005", s.length()=5 j=1, s="0005", s.length()=4 j=2, s="005", s.length()=3 j=3, s="05", s.length()=2 3 > 2 , end of loop call is then done to Long.parseLong("05"); :D |
Re: Breaking Out of the Box
2007-01-22 10:17
•
by
Robert Watkins
(unregistered)
|
|
[quote]In Java, JavaScript and others, a leading zero signifies an octal number[quote]
long foo = 00020; // octal long bar = Long.parse("00020"); // decimal assert foo = 16; assert bar = 20; Long.parse() assumes a radix of 10 if you don't specify - see the Javadoc. |
Re: Breaking Out of the Box
2007-01-22 10:19
•
by
Shareware
(unregistered)
|
|
Perl always win.
#lame code ahead @twosomething = (1..255); foreach $ip (@twosomething) { `pring 192.168.0.$ip`; } that way you can have a "named section" @salesIps = (13..98); foreach $ip (@salesIP) { callShutdown($ip); } Thats much better than this one (c-ish): for($t=$minimalSalesIp;$t<=$maxSalesIp;$t++){ callShutdown($t); } People whine that Perl is unreadable, but tome can be even more readable than everything else If you try to. Of course, thinks like Ruby can add something to this... #Fake Ruby code ahead salesIp.each {|ip| callShutdown(ip) }; Still.. I think the Perl way is more readable for most people, acustomed to the C way object-less, or acustomed to the poor objectability of Java. |
|
My last job forbade the use of breaks in anything but a switch/case statement.
|
Re: Breaking Out of the Box
2007-01-22 10:43
•
by
RobertB
(unregistered)
|
Dammit, now I've got to wait for a stack overflow before I can read the rest of the posts. captcha: digdug -- an inferior clone of Mr. Do! |
Re: Breaking Out of the Box
2007-01-22 10:46
•
by
Foo
(unregistered)
|
Strings in Java are immutable, so every time s = s.substring(1) is called it is creating a brand new string and copying the substring into that new string. If this utility code is running on the server and it's in a high volume application that's a lot of extra load on the garbage collector. IIRC, also Long.parseLong(s) strips leading zeros. Even if it doesn't, there are far more efficient ways of doing this like scanning through the string looking for the first non-zero character. |
Re: Breaking Out of the Box
2007-01-22 10:50
•
by
El Quberto
(unregistered)
|
If you incremented "i" inside of the loop. I could see that in something like this: for (int i = 0; i < myString.length-1; i++) { if (myString.substring(i,i+1).equals("A")) { i++; continue; } // blah } Note: I didn't say it was a good practice |
Uh yeah it is, it's just that the break goes after the other important thing...
Well the fact that parseLong works without having to strip the leading 0s sure makes me wonder why the hell you'd bother stripping them manually.
Dudes, how about just using long StringToLong(String s) {
Cause it sure seems to work on my machine...
Not in Java, or at least not for parseLong: if you don't give a radix, parseLong defauts to base 10, not to the language's parsing of literals, as mentioned in the Javadoc:
Dude, your code's a trainwreck, Long.parseLong returns a long not a Long, and it defaults to base 10 when no radix is given...
No it doesn't, go read the damn javadoc already, please. |
|
Kiss the snake
[call_shutdown(ip) for ip in sales_ips] |
Re: Breaking Out of the Box
2007-01-22 11:25
•
by
Sick Boy
(unregistered)
|
|
I think everyone's forgeting that the best way to solve any problem is a case statement.
int j = 0; while(s.CharAt(j) == '0') j++; switch(j) { case 1: s=s.substring(1);break; case 2: s=s.substring(2);break; case 3: s=s.substring(3);break; case 4: s=s.substring(4);braek; etc... } return Long.parseLong(s); |
Re: Breaking Out of the Box
2007-01-22 11:29
•
by
Sean
(unregistered)
|
Not exactly. String.substring() uses a package level constructor on the String class that allows a substring to share the data of its parent string. It does not need to copy the data each time because it knows that the object it is passing the data array to is another immutable string. Anyway, that doesn't make the code any less of a wtf since Long.parseLong() could have been used on the string with leading zeroes. |
'foobar' as in F*cked Object-Oriented Beyond All Reason/Recognition ? And why does everyone keeps posting the CAPTCHA's? Aren't you guys affraid the Evil Spammers (tm) will make a list and start spamming this forum :) -Edoode |
I think I might be part of the problem then. If these loops weren't likely to be expanded on, I'd write them as: my @twosomething = (1.255); my @salesIps = (13..98); `ping 129.168.0.$_` for @twosomething; callShutdown($_) for @salesIps; Perl most certainly, doesn't always win though, lets return a reference to a array of upper cased entries in the name column of a database table passed as the only argument.
|
|
Snakelin, I am with ya all the way. Half the comments were a WTF in and of themselves.
|
|
Manager: Why set it to 11?
Coder: Well, it would be one better then, wouldn't it? |
|
Bad programmers makes baby Jesus cry.
|
|
This topic reminded me a bit of code my co-worked showed me last week. It looked like:
|
| « Prev | Page 1 | Page 2 | Next » |