- 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
Admin
Wouldn't empty strings be accepted too ?
Admin
Admin
Are you saying that the "Alex is dead" guy is dead ?
Admin
Imagine this thing is written in PHP Half of the comments is about how much PHP sucks.
Admin
Admin
Wow, in that short code snippet, and three different ways to represent true vs false! ("Y"/"N", "1"/"0", and the boolean) 'Hate to get bogged down in consistency or anything.
Still, it could have been worse. At my last job I worked on a system where the original developers used the strings "true" and "false". I came across a bug once where they had written, if (flag.equals("flase")), i.e. they had a typo spelling "false". As of course "false".equals("flase") is false, false looked like true, with unproductive results.
The same programmer stored all his numbers as strings, too. When he had to do arithmetic, he converted to Double, did the arithmetic, then converted the result back to a string.
Admin
Anyway, some of you miss that spaces, hash, stars or hypens could be added to the list of legal characters in a phone number at some point in future, and that even O(n*m^2) algorithm is fair enough for n,m < 20, as long as it is easier to write, test, and maintain than O(n) one. But I really doubt this one is one of those.
Admin
When I was in school, I was taught that writing
is wrong. The book said that this relies on the assumption that the character set represents digits with contiguous values, i.e. that if we arranged the characters in order, we wouldn't have letters or punctuation marks interspersed with digits. (This isn't unthinkable. See, "EBCDIC".) The "right" way to do it, the textbook informed us, was to create an array and loop through the elements comparing to each.
Interesting argument, but it seems to me that any code you write makes assumptions about how the computer works. Putting values in an array assumes that the elements in the array are stored contiguously, so that if I loop through them none will be skipped. It assumes that comparing a character stored in a scalar to a character stored in an array is a valid operation. Etc etc.
Okay, I'll give that textbook author this: There's a difference between relying on things documented as part of the language spec, and relying on things that could at least theoretically be different if you recompiled the code on a different platform.
Admin
You missed one:
Admin
You danced around the most important fail: it's an O(n^2) solution that could easily be solved in linear time. Most of the other WTFs in that code stem from that problem.
Admin
Blegh, forgot that both loops aren't working on the same set, so it's actually O(nm), and since the valid character array is a constant size, it technically simplifies to O(n), but it's still extremely inefficient.
Admin
Toothbrushing accident.
These things happen...
CAPTCHA: praesent - It's not a present.
Admin
I hereby rechristen this site "The Demiweekly WTF".
Admin
Well, turns out that the Alex is Dead guy was really a particular Flame infestation on one of the computers in the DailyWTF office. Then a Flame command system sent the instruction to delete itself ...
Admin
Which is fine if it's only called occasionally and reduces the number of globals cluttering the library.
Some systems may have to deal with EBCIDC, so you can't just assume numbers are in order.
What's wrong with that? Small objects are efficiently handled by garbage collection, and substring with a fixed length is a constant time function.
This is clearly a security function, and exiting early would make it vulnerable to side channel attacks.
A boolean requires one word... a reference to a literal string requires one word. How is a boolean less efficient?
Admin
I was surprised that this was quite THAT slow. I know it's not exactly cheap to do the invocation of an anonymous function (which is, iirc, what the lambda gets turned into), but that seems extra slow.
Maybe using Any() with a negated condition would allow for early exit and speed it up a little?
Captcha: facilisi. Underwater sealab.
Admin
I tried both an the execution times are about the same. FWIW, both Any and All will early exit (as will the regex versions), as can be seen by putting the invalid characters towards the beginning of the string instead of the end:
Results: ValidateLinqAll valid: 613.0613 ValidateLinqAll invalid: 620.062 ValidateLinqAll w/ early exit: 64.0064 ValidateLinqAny valid: 603.0603 ValidateLinqAny invalid: 630.063 ValidateLinqAny w/ early exit: 66.0066 ValidateRegex valid: 2362.2362 ValidateRegex invalid: 3833.3833 ValidateRegex w/ early exit: 788.0788 ValidatePrecompiledRegex valid: 1982.1982 ValidatePrecompiledRegex invalid: 3442.3442 ValidatePrecompiledRegex w/ early exit: 403.0403 ValidateForLoop valid: 85.0085 ValidateForLoop invalid: 86.0086 ValidateForLoop w/ early exit: 11.0011
Admin
This is great! I'm gonna run right out and make a StringyString class!
Admin
Because checking the output of TryParse to see if it's negative is so complicated...
Either int.TryParse() or long.TryParse() was my first thought too; you could always use a lazy loop over the string and use the char.IsDigit() or char.IsNumber() methods, depending on what you were actually trying to achieve (IsDigit() checks that the character is strictly a radix-10 digit, IsNumber() checks whether the character is classified by Unicode as a number).
Admin
Public Function IsValid(value as string) as boolean Return IsNumeric(value) End Function
There, VB.NET fixed it :P
PS: I Know The Wrapping Function is kinda obsolete, but this way it gives more Context here ;)
Admin
Having recently worked with a company providing this kind of service ... it is unsurprising...
Admin
This comment assumes that we talk about C# and .Net :)
captcha: validus - your argument is invalidus
Admin
Ahhh, should've used MUMPS.
It'd be a one liner..
; If there's anything left over it's not an integer. I $L($TR(DEST, "0123456789")) > 0
I'll get my coat...
Admin
Or they decide to start accepting hex...
Admin
What? No frist idiots any more? Looks like I cleaned this place of them.
Admin
Admin
Well, yeah, you wanna make sure that if there's ever a need to accept numbers after the decimal point, you can add those in too.
Admin
Here's the super-duper-timesaving method of doing this exact same thing (I'm assuming C# here):
private string checkChars(string destination) { return (destination.ToCharArray().All(d => Char.IsNumeric(d))); }
You're welcome.
Admin
Admin
Looks like C#, so wow, how about just
function ContainsOnlyNumbers(string input) { foreach(var s in input) { if (!Char.IsNumber(s)) { return false; } } return true; }
Quits ASAP and uses presumably fast built-in functions. Alternatively a reg-ex that only accepts numbers should be quite smart.
Admin
return $destination =~ /^\d*$/;
or rather the whole sub
sub checkChars { $_[0] =~ /^\d*$/; }
Not that anyone would bother making a function for anything as trivial as this.
Admin
Admin
I think it's just the function name that's misleading. Should have been checkCharsAndSpinWait
Admin
Looks like Martin should have no difficulty achieving some stunning performance gains, if management even cares.
Admin
Hah, you would be surprised…
http://www.scp-wiki.net/scp-033
Admin
Hello,
It is probably an entry for an obfuscation contest ? ;-)
Never heard of isInt() ?
Admin
Well here's a place to start:
Admin
This is what happens when bad programmer writes ann ill-conceived implementation that doesn't even work, and competent programmer just simply gets it working by throwing in the validChars boolean.
Alas the business mindset is to "don't touch it now that it's working". I really do wish that weren't the case, because this seriously should have been overhauled.
Addmittedly, fixing the return type could prove to be a lot more work because who knows how many times the bad programmer has called his Frankenstein.