- 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
You're referring, I think, to the fact that srcmp might return -1, so the false test would fail. Good point. Perhaps this is where one of those famous tri-bools that show up so often could be used? Something like if(strcmp(str1, str2) == FILE_NOT_FOUND)
Or it might return 2, or -2, or -3 or -4 or well any other valid integer.
Admin
You're referring, I think, to the fact that srcmp might return -1, so the false test would fail. Good point. Perhaps this is where one of those famous tri-bools that show up so often could be used? Something like if(strcmp(str1, str2) == FILE_NOT_FOUND)
To properly handle strcmp() you'd need more than a tri-bool. strcmp() can return any int value: -154 means the same thing as -1.
The only returns you can count on from strcmp() is less than 0 , 0, or greater than 0. There's nothing in the standard which says which less than zero or greater than zero value will be returned, and nothing can be inferred from that value except that the strings do not match and which is 'less' than the other.
Admin
I'd go for
if( Pow( strcmp( str1, str2 ), 2 ) == false )
:P
(Hmm.. the manpage for strcmp() says that "[strcmp returns] an integer less than, equal to, or greater than zero". Looks like it's not that easy anyway :#)
Admin
Great, not only am I late with my post, but the forum killed my smilies. Bah.
Admin
Ever looked at the login page here? Yeah, it's probably generated crap, but it's damned annoying to not have just a plain vanilla normal form submit like practically every other login page I come across.
In my meaner moments, I think about submitting it here... The code in question is:
<a id="_ctl0__ctl0_loginButton" class="Button" href="javascript__doPostBack('_ctl0$_ctl0$loginButton','')">Login</a>
Admin
Let's see if I can get this right:
Admin
It could be worse, I suppose - it could complile a small C program to add 1 to the value and then run that...
Admin
It seems like overkill to me, but in the guy's defense, doesn't this prevent a potential spelling error? So, it's not a complete WTF, is it?
Admin
That's a pretty normal way to do increments in shell.
Here's my favorite:
x=
echo dummy | awk '{ x = '"$x"' + 1 } END { print x }'
Actually seen in production code.
Better, seen dozens of times, as it was used to iterate through numbers for a menu! I really miss that installer. It generated a 40-line menu for a 24-line xterm, requiring support to explain to people how to resize the window so they could see the menu items, of which only the first five were ever used.
Admin
"Which brings up something I often thing about: how can coders, who must master a precise syntax and set of symbols, how can they turn out to be such bad, bad spellers?"
Well, if you look at the English language you will note that it has no precise syntax in spelling or speech. So, one really has nothing to do with the other. Note:
know
no
there
their
they're
I could go on, but you get my point?
Admin
Yes this is, making JS-disabling users completely unable to use a website is nearly always idiotic (if only because there are very few reasons to).
That kind of crap is usually pulled either by people who don't know jack about what they're doing (cargo cult programming and all), or by shitty CMS.
Admin
Why oh why do people have so much trouble with boolean values in C? There must be thousands of projects that contain code like this:
#define TRUE (0 == 0)
#define FALSE (0 != 0)
#define NOT(x) (x == TRUE)? FALSE : TRUE
if(NOT(y) == TRUE)
{
...
(For those of you who missed the WTF: x isn't parenthesized in the macro.)
Admin
Yes, I had to do this again.
Hooray. (CAPTCHA = doom)
Admin
Could it also be the already-internationalized-for-you way?
Admin
Actually, there's a very... well, I won't say good reason, but at least a rather compelling one from the POV of the less scrupulous sites, namely, those who are looking to use a Bastard Helper Object to insert a spyware downloader onto the visitors' systems. Lovely, eh?
Admin
Ignore the above; I must have an attack of the dumbass. Feh.
Admin
No, its not internationalized for you, its just a dumb way of writing "true". It looks like something thinks they know more about Java than they really do. Not that thats an unusual thing.
Admin
I can see doing this in the case of i18n, but it is still quite a stretch.
Alex, you really need to learn about "whose".
I am wondering why the use of export. IIRC, it is so the shell variable is available to further shell invocations.
Sincerely,Gene Wirchenko
Admin
Because you may want to improve readability:
private static final string PARM = "ThisIsAParameter".toUpperCase();
Admin
A common misconception with some potential adverse consequences (e.g. I've watched a colleague spend 3 days on it, until he finally sought assistance) so I feel compelled to point it out, and it seems to be TDWTF culture anyway. The above declaration is not a constant. I have demonstrated this misunderstanding here: http://jqa.tmorris.net/GetQAndA.action?qids=13
Admin
Which, of course, is why the pager on The Daily WTF forums requires JavaScript. (In fact, it's even more of a WTF - it uses POST rather than GET. Have fun bookmarking/sending links to the second and subsequent pages. What's even more WTF-worthy is that there is a way of getting to the second and subsequent pages using GET requests, but it's only used by the "last comment" links on the forum index. Oh, and the URL the comment-submission uses to send you to your posted comment always goes to the first page - though if your comment is on the first page, is does go to it OK.)
Admin
Admin
ba-bom-ksh!
Admin
Or it might return 2, or -2, or -3 or -4 or well any other valid integer.
Which is on itself another WTF. Even if a language doesn't support any abstract data types or enums, you could still use clearly named constants for the three types of responses to this function. Making it return any positive or negative number is just exposing too much of the implementation. Why not just call the function strsubstract in the first place? How any one would ever come up with adding a function in this shape to a language or library is a mystery to me.
Admin
lol. yeah he did it like that because he wanted all of his string constants to be type-checked. You could in theory go all the way with this:
(new Sentence (
(new Person ("me")).toString(),
(new Verb ("want")).toString(),
(new Item ("money")).toString()
)).toString();
But thats just madness, if all you wanted was the string constrant "i want money".
Admin
in ksh you can do:
let SLEEP_COUNT=SLEEP_COUNT+1
He obviously didn't know that, so he used <font size="5">`</font> to do a textual substitution of the output of the command expr into the assignment statement.
Admin
Best to keep it simple. Try this:
s/who's/whose/g
Admin
Actually, I reckon this is from code that heavily used the strcmp family of functions - they return 0 if two strings are equal, so you end up with a lot of code like if(strcmp(str1, str2) == 0) all over the place (yes, I know you shouldn't use strcmp anyway, but bear with me). These funky defines would let you write if(strcmp(str1, str2) == true) which is slightly more readable - at the expense of making the rest of your code an impenetrable nightmare.
You could try #define EQUAL 0 #define GREATERTHAN 1 #define LESSTHAN -1
Only problem is that, IIRC, strcmp et al. don't guarantee to return particular absolute values - just that the return will be <0, ==0 or >0...
Admin
At least this particular idiot correctly used the 'onclick' event. I usually see the <a href="javascript:..."> variety, which is even more of a WTF.
Admin
Please ignore my previous "return value will be..." post. It seems that this forum is incapable of coping with code in posts unless you know exactly how to quote it.
Which is actually pretty ironic, given the general topic :\
Not sure what happened with the #define one though. Perhaps it's my use of Opera...?
Admin
I nipped outside to check. It's still England here. WTF?
Admin
Or it might return 2, or -2, or -3 or -4 or well any other valid integer.
That's what >= and <= are for.
So:
if(strcmp(str1, str2) >= false || strcmp(str1, str2) <= FILE_NOT_FOUND)
{
........
Or we could of course just use:
if(strcmp(str1, str2) >= false || strcmp(str2, str1) >= false)
{
.......
Admin
most portable ? That's the WTF
Personally I would :
SLEEP_COUNT = `{echo $SLEEP_COUNT + 1 | bc}
Admin
The ksh in old unix day does not support $(( ))
Admin
Which is on itself another WTF. Even if a language doesn't support any abstract data types or enums, you could still use clearly named constants for the three types of responses to this function. Making it return any positive or negative number is just exposing too much of the implementation. Why not just call the function strsubstract in the first place? How any one would ever come up with adding a function in this shape to a language or library is a mystery to me.
Actually, the design of strcmp() is quite clever. Observe:
a == b -> strcmp(a, b) == 0
a > b -> strcmp(a, b) > 0
a <= b -> strcmp(a, b) <= 0
a != b -> strcmp(a, b) != 0
Perfectly regular, perfectly predictable, perfectly comprehensible. Introducing constants like STRCMP_GREATER or whatever will actually make things LESS flexible: I'd rather write
if (strcmp(a, b) >= 0) {...}
than what I suppose you're suggesting, which would either be
if (strcmp(a, b) == STRCMP_EQUAL || strcmp(a, b) == STRCMP_GREATER) {...}
or
if (strcmp(a, b, STRCMP_GREATER_OR_EQUAL)) {...}
because the standard C way of doing it is the only way that includes the standard operator that indicates what I'm actually interested in testing.
Admin
The first ones java not vb. (In vb its ToString not toString)
Admin
strcmp() should really be called strdiff().
I find this a little non-intuitive to read:
<FONT face="Courier New" size=2> if ( strcmp("hello world","hello world") ) {</FONT>
<FONT face="Courier New" size=2> printf("hello\n");</FONT>
<FONT face="Courier New" size=2> }</FONT>
Anyone who didn't know the peculiarity of strcmp() would probably expect to see "hello" printed.
Admin
Pot. Kettle.
Admin
Of course it is a constant. It is not a compile-time constant expression and therefore cannot be inlined. That doesn't mean it is not a constant, i.e. a variable whose value cannot be changed.
Admin
when I do, I do increments that same way.<i>
Well, c'mon. POSIX sh specifies '$(('. You can rely on it as baseline functionality on sensible platforms.
http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_04
Admin
Re: ksh stuff
Right, the reasons why it's a WTF. Let's remind ourselves of the script:
#!/bin/ksh
export SLEEP_COUNT=
expr $SLEEP_COUNT + 1
On the bash vs sh vs ksh vs ksh93 vs perl (evil evil language) there is no comparision: ksh88 or ksh93 + awk wins hands down. (Before you ask, bash screws up scope and the vi editing mode in bash blows goats; finally - look at the manual page for bash under bugs, then you should understand).
Also, Bourne shell is not really the lowest common denominator anymore. I've never seen a Unix or Unix a like since the 1990s that doesn't support either ksh or bash.
Admin
Actually, the design of strcmp() is quite clever. Observe:
a == b -> strcmp(a, b) == 0
a > b -> strcmp(a, b) > 0
a <= b -> strcmp(a, b) <= 0
a != b -> strcmp(a, b) != 0
Perfectly regular, perfectly predictable, perfectly comprehensible. Introducing constants like STRCMP_GREATER or whatever will actually make things LESS flexible: I'd rather write
if (strcmp(a, b) >= 0) {...}
than what I suppose you're suggesting, which would either be
if (strcmp(a, b) == STRCMP_EQUAL || strcmp(a, b) == STRCMP_GREATER) {...}
or
if (strcmp(a, b, STRCMP_GREATER_OR_EQUAL)) {...}
because the standard C way of doing it is the only way that includes the standard operator that indicates what I'm actually interested in testing.
Which in itself is another WTF: that you can't overload operators and have functions that dispatch based on the types or their arguments. In haskell the quicksort function below, obviously also works for strings, but do you see a stringCompare anywhere?
So whats going on? Is the ( >= ) operator overloaded for strings ? Well, look at the type:
quicksort :: Ord a => [a] -> [a]
It takes a list of things of type 'a and returns such a list. Yet the whole function is bound by the context of 'Ord a'. Which says that whatever type you choose for 'a, there needs to exists an instance of Ord a. Which exists both for Lists and for Characters (strings are just list of characters). You can make instances for your own datatypes if you want to, or have them automatically inferred using deriving:
data Maybe a = Just a | Nothing deriving Ord
The deriving Ord part makes that you can compare Maybes.
Admin
That's all well and good, but does Haskell support ternary logic {True, False, FileNotFound}? If not, then it's unusable in real life projects.
Admin
I agree. I think this is in fact the only way to increment in POSIX shell, which should
be used for any shell scripts. ksh or bash or zsh or whatever specific dialects are much less
portable.
IMHO this is perfectly valid, portable code. Of course it is a waste of resources and
may appear clumsy, but thats what is available if you want to be portable.
In most cases it won't matter anyway....
Admin
That's a grammar error, not spelling ;)
Admin
Umm, you realize that strcmp() was defined something like 30 years ago, right? It may not be a great function definition, but operator overloading and other OO techniques were developed a bit later - you know, when computers had more than 16KB of RAM.
Admin
Yup, but
whose', not
who's'...Admin
That's why you use editor that wraps long lines. Not only because somebody else might have been evil, but creating a code like that is too easy to accidentally do by yourself (accidentally hit del at the end of long line when next line has deep indentation, for example).
Admin
Admin
The correct way is:
<a href="normal.html" onclick="dosomething(); return false;">
The browser will fall back to just going to normal.html when there is no javascript support, otherwise it will dosomething(). Works in all modern browsers without a problem.