- 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
Don't worry, it happens to the best of us.
Admin
"-3e4" is valid for parseFloat i think? at least its a valid number in vb.net, sometimes have to use own validation
Admin
Personally, I thought that WAS the point... that it was in this library and did NOTHING - guess everyone else had a different take on it :O
I am surprised at the number of people actually got this wrong :O
Don't you mean void setNull(char &*variable);?
Admin
errr... char *&, rather
<-- feeling dumb
Admin
Yes, but, if we are limiting the discussion to the C language, then no. C++ has pass-by-reference with the & character, but C does not.
In C, you would have to pass a pointer to the pointer that you wanted to set to null. But what is easier, this . . .
char *str;
setNull(&str);
or . . .
char *str = NULL;
?
Admin
Obviously it's easier to just set the pointer to NULL. I was amazed at the amount of people who didn't understand how to properly write a function to do it, however. My reply was specific to the person I quoted, who's answer was akin to setting a character to NULL using *C++* (which would throw a compiler error if you didn't cast NULL, assuming of course NULL is defined as ((void *)0) instead of just 0).
Admin
Given just this information, we can't know for certain that this is a worthless function. Here's a scenario where it's justified;
You have a function pointer that is used to point to the correct function for some behaviour that changes in non-deterministic but constrained way (this is the whole reason why C++ has "virtual" - because it happens a lot in C). The function pointer is called frequently but occasionally it is not necessary for it to actually do anything. Checking that the function needs to be called via an "if" condition will actually be slower most of the time because it has the potential to stall the CPU - this is especially important in FPS games. So what do you do?
Answer; you write a function that does nothing and reference that.
The function must follow the declaration prototype of the pointer, so it will have that one parameter - but because the code doesn't reference it, the compiler will emit a warning message (about an unreferenced parameter) that looks like a mistake.
variable = NULL does nothing, and the compiler knows this because there can be no alias. The compiler optimizes the line out of existence but does not emit a warning anymore.
The only WTF for this type of code is not leaving a comment that tells the maintenance programmer why the function appears to be pointless.
Admin
Curiosity, only. . .
How many of you commenters are gainfully employed writing real programs?
Has it occurred to anyone that there are compilor implemtations that require rolling your own?
(ever tried a sprintf of more than 128 chars in Turbo C) ?
Sigh.
Admin
*waves*
Actually, at the moment I don't do a real lot of programming myself - end up just operating as a consultant and fixing others disasters.
Seems there is a point where one reaches a thershold and becomes more valuable directing others than doing the work themselves... or some shit.
Admin
I write Javascript that mangles the DOM, and I write ASP/JScript and itbits of PHP that mangle the output strings.
I don't consider myself a true programmer until I've actually written an OS application, or preferrably, a game -- since games are about behaviours and so is programming.
Admin
So, '...', '---' and even ';.-0;' are all valid Numbers, eh?
Admin
Yes.
Is Compilor a villain from an 80s children's show?
And the issue here is not JUST that the idiot rolled his own function for doing this, but that he did it in a horribly inefficient way, with a tool that could have done it a lot easier, AND to top it off the function doesn't really do anything useful, and certainly doesn't do what it's supposed to.
Admin
Javascript, RegEx are specific objects using a perl-like syntax.
That, or a string if you use the "new RegExp()" construct, but that one sucks, no one in his right mind would use it unless he has to.
Except that this is fucking javascript, not TurboC or whatever insane language/compiler you may enjoy, and this code therefore has roughly a WTF per line
Admin
Ah yes, laugh if you will, but I wrote my own number parser. The standard paraser will fail on something like "1,234.56", where a comma causes an exception.
The parser gracefully handles things like:
"-1,234.56" = -1234.56
"1,234.56-" = -1234.56
"1,,,,2,,3,,4,,5.6.7.8" = 12345.678
"12oops34"= 1234
"-123.34-" = -123.34
and so on. Of course I deal with Web users.....
Admin
Sweet Jesus.
Well, I guess everyone has their own philosophy of what constitutes input validation. But I'd have second thoughts about silently rewriting a user input into, say, a quantity field which reads "100 ha ha no i really meant 3" as "1003".
(Unless it doesn't really do that, in which case.... well, no, sorry, it still looks sketchy to me.)
Admin
Then it would be nice if it was explained right from the beginning. It's been now a week I'm lurking on this web site and I really miss the fact that there is barely no mention of the languages used. Some silly things in a language may not be that silly in other ones.
What I can see is some people laughing at others while they have no clue about the programming language used and think with the one they use all the time.
Admin
What surprises me is that this is a WTF. It DOES do some checking that a lot of the regular functions do not support. Yes it does accept some bad input values. That fact hardly makes it a WTF though. Its entirely possible that in the context provided he just wants to avoid anything with basic text, but wants to accept values such as -9, and 9.5.
Without knowing the context this is used in it could be that this is a totally valid peice of code that works 100% of the time.
Yes there are some c functions that would have made this work in a simpler manner, but again-thats hardly WTF worthy.
Admin
Iit looks like JavaScript to validate a telephone number to me.. although if that is the case I'm surprised )( aren't included or stripped off . Besides not being very thorough, I'm not getting the WTF here in that context.
Admin
Sorry, but sometimes there are valid reasons to "re-invent" date formatting.
Like for instance when working with VBA in Excel.
Microsoft has an annoying habit of being "HELPFUL"
for instance, in Excel, all date formats are in american format. ALWAYS, irrespective of what the locale settings are.
BUT, if you try to interpret a date with a day greater than 12, the date format routine detects this and swaps the month and day. HOW HELPFUL...
so, you write your code and test it on the 15th of the month, it goes through formal testing a week later on the 22nd and is rolled into production a week later on the 29th.
Which gives you a day or so to "get out of dodge" before the fit hits the shan.
Gee, Thanks M$!
So, writing your own date formatting and validation can sometimes be your only defence.
Admin
^-?\d+.\d+$ How hard was that?
Hmmm... If we ignore the unescaped "."
42 is not a number.
how about *-?\d+(.\d+)?$
Admin
Which illustrates nicely that C pointers are dangerous. Powerful, but dangerous.