- 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
<font size="1">
</font><font size="1">!= is cooler looking than &&</font>
Admin
These have to be cases of "Paid by the Hour" versus "Paid by the Line" :)
Why the hell else would anyone have spent so much time being so damn useless......
-space
Admin
Yeah, cooler and wronger
Admin
They left out a bunch of useful stuff.
Random generator = new Random(123456);
int r = generator.nextInt();
<FONT color=#0000ff>public static String</FONT> MAYBE = (r % 2 == 0 ? <FONT color=#ff0000>"perhaps" : "who knows");</FONT>
<FONT color=#ff0000></FONT>public static String SOMETIMES = (r % 7 == 0 ? "not bloody likely" : "shit happens");
public static String IDOUBTIT = (r % 31 == 0 ? "shouldn't happen" : "remember the prime directive");
public static String RIIIIIIIIIGHT = (r % 59 == 0 ? "a subspace fracture" : "two to beam up");
Admin
This seems to be saying "if you provide a name, you have to provide both a first name and a last name. Not that WTFey, just badly documented.
Admin
"!= is cooler looking than &&"
It's a shame that that's not its meaning though.
(Did nobody test it with both first and last name blank?)
Admin
:P
ROLL ON THE FLOOR @
public static String RIIIIIIIIIGHT = (r % 59 == 0 ? "a subspace fracture" : "two to beam up");
omg rofl
-space
Admin
rog wins. You all can go home now.
Admin
I've done vaguely similar things to test whether any of several numerical fields were 0 and flag that situation.
Assume a, b, c, d, e are the fields in question.
<FONT face="Courier New">bool isValid = (a * b * c * d * e != 0);</FONT>
Of course I put a comment explaining WTF I was doing that.
Admin
Or more importantly, with one blank and the other not?
Admin
Not that it would be likely, but isn't there the possiblity of an overflow error with that?
Admin
or I could be the idiot to not test the proper failing condition...
Admin
I'm still waiting on someone to explain why this is good way to check for a negative number. Don't let me down, people.
Admin
I like living dangerously.
Admin
It's a better way than
<FONT color=#0000ff>function</FONT> isNegative( n ) {
<FONT color=#0000ff>return</FONT> n != 0 && n / n == -1
}
or
<FONT color=#0000ff>function</FONT> isNegative( n ) {
<FONT color=#0000ff>return</FONT> n != 0 && Math.abs( n ) / <FONT color=#000080>Math</FONT>.<FONT color=#0000ff>abs</FONT>( n ) == -1
}
Admin
The irony here is that the abs() function usually checks for negative in itself. So instead of a simple comparison statement, it has maybe three compares (when including the one from abs()), a function call, a boolean operation, a negation operation (inside of abs()), and a division.
At least it doesn't invoke the for-switch paradigm.
Admin
Actually not, considering that != misses only one case while && misses two plus won't allow even valid queries ;-) So who's wronger now?
Admin
Well, it's obvious, isn't it? If the absolute value of a non-zero number is negative, then the number must belong to the set of numbers that are Not Ever Gonna Appear Twice In Value Entities. You've just mistaken the camel-case version of the acronym for the common english phrase "is negative".
Admin
Umm...because it looks cooler than:
Admin
Nevermind -- old man, bad glasses, small fonts, failed to see the "n/" part. I really gotta adjust this browser.
Admin
Getting late.. still, it's two against one
Admin
I'll be surprised if they didn't also created isPositive() just to cover all the bases (and I wonder if a zero would be considered positive in that respect).
Admin
Hrm... possibly against his beliefs to call anything "false", he is forced to say that FALSE = "not true". After all, if FALSE = "false" then that would be true and thus a vicious cycle. FALSE must be not "true" (to, of course, distinguish from TRUE). Therefore, we must define it as FALSE = "not true".
Admin
There are many ways to deal with that. One simple, elegant way is the following:
function isPositive(n, strict = true)
{
return (n >= 0 && (strict ? n != 0 : true));
}
Of course it'd be safer to have two separate functions, but I'll leave that as an exercise to the reader.
Admin
Yeah, || would be more accurate to use in the place of != (or &&) there. However, the developer also fails to check for the use of "spaces" in addition to it being blank (I knew a QA department that always tested to see if that was allowed).
Admin
I don't know Java. Does it really not have a built-in "false" constant?
Admin
Well, != looks a lot cooler than ||.
Admin
So, was nobody going to comment on string compares in Java?
Admin
It has a boolean type that has two values possible values: true and false. No 0 and 1 or anything like that.
It also has a Boolean Object type that has two constants TRUE and FALSE. I'm guessing these are supposed to be to evaulate some sort of input, I hope.
Admin
Now this is easy to explain. The code is Javascript, which is embedded in HTML. Early Browsers had Problems with ">" characters embedded in Javascript, so the programmer tried to avoid both of these "dangerous" characters.
Still, Math.abs(n) != n would have been more obvious.....
Admin
I had assumed this was a validation JavaSCRIPT for an HTML page, but then it would have been this.txtLastName.value and not .Text. Hrm... good call there. It would definitely require .equals().
Admin
Finally someone who thinks before writing.
Admin
Except that wouldn't be the same thing...
Consider:
(a==b) != (c==d)
This returns false if either ( (a==b) && (c==d)) or ((a!=b) && (c!=d)), else true
whereas
(a==b)&&(c==d) would return false if only one of them (a,b or c,d) were the same.
In other words, that code is trying to make sure that EITHER none of the strings is empty OR both are empty. && (instead of !=) wouldn't do that.
-dave-
Admin
I am not the smartest programmer in the world, but RogtheFrog, in the last WTF you were advocating for the use of delimited strings within a database field and now this.
Admin
The coder here is clearly a postmodernist who feels that there is no such thing as absolute truth. By defining the constants this way (he made a mistake declaring them final, as truth claims cannot ever be final in the postmodernist viewpoint), he makes it easier for future developers to define their own truth without making substantial alterations to the code..
Admin
Given the naming convention (Hungarian-style prefixes, Pascal-cased property names), this is probably C#, not Java. In which case <font color="#0000ff">this</font>.txtLastName.Text=="" is correct.
Admin
Aww, not a fan of the Brute Force Technique?
Also, that first gem not only would risk overflow, but I guarantee that 5 multiplies and a compare will always be slower than just 5 compare statements ;)
Admin
I wasn't advocating for the wanton use of delimited strings in a db, nor am I advocating for the wanton use of this shortcut either. Using delimited strings in a db is perfectly legitimate IN SOME VERY SPECIFIC INSTANCES. The a * b * c trick is an example of something I used once in a script I wrote for myself where the variables were controlled. And the "I like living dangerously" was ironic. So nyah.
Admin
That's what I figured too. Not that I've done much C-Pound :D programming, but I knew it wasn't Java.
Admin
I figured this is what happens when you let Math majors write code. Points off for not including a Proof of Correctness in the comments.
Admin
You're neglecting the possibility that the first element is zero. Those multiplications will scream.
Admin
return n != Math.abs(n);
isn't ugly enough?
Admin
Not necessarily. Compares represent branches, which can cause pipline stalls and restarts on modern processors. And, for example, x86 comes out with a compare and jump instruction for each compare, while the multiply version just has a multiply for each followed by a single compare / jump pair. That's 10 vs. 7 instructions for five vars. (Determined by looking at compiler (gcc) output for the following C code:)
main(int ac, char **av)
{
int a, b, c, d, e ;
int res1 , res2 ;
a = 16384 ; b = 32768 ; c = 2 ; d = 2 ; e = 2 ;
res1 = ( (a != 0) && (b != 0) && (c != 0) && (d != 0) && (e != 0) ) ;
res2 = ( a * b * c * d * e != 0 ) ;
printf("res1 is %d, res2 is %d\n", res1, res2) ;
}
Which, for me, yields "res1 is 1, res2 is 0" (a 32-bit int machine).
Admin
I simply assumed this was written in D flat, you know one of the .NOT languages.
Admin
Unless I'm quite mistaken, modern processors do integer multiplies in constant time, independent of the data, so the first element being zero has no effect on speed...
Admin
Isnt that a little dangerous?
int a = 65536;
Console.WriteLine(a*a);
if (a*a == 0 && a!= 0) { Console.WriteLine("WTF a*a is zero"); }
Admin
Well, it could be worse, they could have done something like this:
function isNegative(n) {
if n == 0 then {
if n/Math.abs(n) == -1 then {
return true
}
else {
return false
}
}
else {
return false
}
}
<font style="font-family: arial;" size="3">Be thankful for small mercies ;-)</font>
Admin
<o:p>
I wouldn’t want to put the programmer out and make him write a few more lines to determine which field
was empty and have an error message that tells the user exactly what to correct. I myself prefer to
validate a form of numerous fields with a single massive if statement, and throw up the ever helpful error
message “Validation failure on string(s).”
--RA
Why is this forum SW refusing to wordwrap?</o:p>
Admin
I've use code similar to the last quirky validation. It is the shortest way to do it, but I always worry if other programmers are going to understand it without a lot of thought. It doesn't get used enough to become a familar idiom. Usually "short", "simple", and "clear" are compatible, but in this case maybe not.
Admin