- 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 gotta love pointless if statements.
Admin
First ? Funny how alot of buggy code will never execute because of statements like this. Since it can't be both null and blank at the same time on todays computers
Admin
Admin
You know Oracle? I love the fact that an emptry string is null...
Admin
I think he just didn't know the throw keyword.
He must have intended to do this:
Admin
It's been a while since I've worked with Java, so I may be off here, but if I recall correctly, Java will throw a NPE when you call an instance method on a null object. Which means that second snippet is worse than pointless, it's completely broken.
Admin
In defense of the second bit of code, it probably meant to do ||, not &&.
Of course,
if (string.equals("")) { string = ""; }
is a no-op, so all you have to do is
if (string == null) { string = ""; },
but ....
Admin
I would have thought he was trying to do this
if ((reportGroup == null) || (reportGroup.equals("")) {
reportGroup = ""; }
Although he could have done (assuming C#.NET):
reportGroup = String.IsNullOrEmpty(reportGroup) ? String.Empty : reportGroup;
captcha: CockKnocker
Admin
Can anyone explain why this is done?
reportGroup.equals("")
I know what it does (obviously), but I've picked up some maintenance work (C#/asp.net) and it is littered with them. Why does this happen so often? Is it in some example code somewhere that every new programmer reads?
NS
Admin
String.IsNullOrEmpty() looks like a horribly error prone method.
Imagine the likelihood of forgetting to use it instead of String.Equals() (or whatever C# has) in some code already relying heavily upon it.
Admin
Not sure what you mean Luke
Admin
Correct me if I'm wrong (as I don't know JSP), and I know you will, but that first one doesn't appear to me to be a WTF. If I am reading it correctly, the JS variable is first being set to either "true" or "false" (as strings) and then being converted to a boolean. I know that just shows how poorly typed JS is, and it could just as easily be done in code by using "hasPriv=='true'" where it needs the boolean value, but all in all, not a WTF and probably makes for smaller code going over the wire.
Admin
Admin
Isnt it missing a closing parenthesis ")" at the end there?
Admin
why not add && new String("").length()==0 to the if while your at it
Admin
Admin
"it could just as easily be done in code by using "hasPriv=='true'" where it needs the boolean value"
WTF?
Why not: var hasPriv = <%=hasPriv%>;
Admin
That is a useless test, it should be:
Admin
I forgot, the code i posted should be wrapped in a try/catch, and on the catch we should do something like:
catch(Exception e){ log.write("warning, we set reportGroup to "" because something went wrong.. I think"); reportGroup=""; }
Admin
Um. If the server-side hasPriv is a boolean that evaluates to true or false, then I fail to see how:
var hasPriv = "<%= hasPriv %>"; hasPriv = eval(hasPriv);
is "smaller code going over the wire" than:
var hasPriv = <%= hasPriv %>; // ex.: var hasPriv = true;
As for your comment about client-side JavaScript being "badly typed". You can very easily set a variable to true or false, you don't need to set a string to "true" and compare it to 'true'. Just goes to show you're complaining about a language you don't even understand.
Admin
I recently hit this piece of pointless code in our own codebase:
When checking to see if something is null, is it better late than never? Also note the amazingly pointless use of final on a static method.Admin
reportGroup = reportGroup ?? string.Empty;
Admin
Yes, thanks. (Fixed.)
Admin
My Java-Fu is admittedly weak, but my first thought was "did he want an OR there instead of AND?"
Admin
Admin
Maybe for C# 3, who knows...
Admin
Admin
Admin
To know Oracle is to Hate Oracle!. To know NVL() is to hate Oracle a little less...
Admin
Well, that comment was totally useless without the quote, so here goes again...
To know Oracle is to hate Oracle. To know NVL() is to hate oracle a little less...
Admin
Admin
The code sounds perfectly reasonable for me. After all, what would all those pretty optimizing compilers with dead code/pointless if detection be good for if no one wrote code for them...?
(just to make it clear: </SARCASM!!!>)
Admin
?? only evaluate null value. so it misses the original authors wish to evaluate ""
Admin
This is more idiomatic in Java:
if (reportGroup == null || reportGroup.length() == 0) { // Handle null case }
Admin
Why not: var hasPriv = <%=hasPriv%>;
The right-hand hasPriv is a Java boolean. What if somebody forgot to assign a value to it (or its value is fileNotFound)?
The result would be "var hasPriv = ;" which is invalid JavaScript code.
Myself, I would have checked to see if (Java) hasPriv was set before trying to use it in the (Javascript) hasPriv assignment. Or maybe done something upstream to confirm that hasPriv was always set before that point.
Admin
All that the original code does if reportGroup is the empty string ("") is set it to the empty string. Thus he doesn't really care if it's already an empty string - he just wants null to be replaced with empty.
The null evaluator works perfectly fine for this. No need to check for an empty string when you're not going to do anything with it.
Admin
Admin
How about Oracle?
Admin
[quote user="DigitalXeron]How about Oracle?[/quote] No thanks!
Admin
Admin
I'm just curious which multi-billion dollar company was taken to its knees because this seemingly useless bit of code was actually relevant for the core applications to run properly.
Admin
Why do you think final on a static is pointless?
Without the final, any class which extends this class can implement a method with the same signature which gets really confusing if someone were to call [instance of child class].isEmpty(). I will admit that calling a static method on an instance is bad practice, but is is possible and you never know what the new CS grad the company hires to support you is going to try and do.
Admin
The first thing you learn in Java 101 is how to use == to compare two ints.
Then, they ask you to compare two Strings, and you learn that == doesn't work on objects like you might expect, and you're taught to use String.equals() to compare strings.
Now, using this basic knowledge, you need to test a string to see if it's empty. Logically, you use String.equals("") -- it's the string comparator you're most familiar with.
Using String.length > 0 would probably be better, but they don't teach you that on the first day of Java 101.
Admin
I don't suppose a second thread was constantly toggling reportGroup between NULL and empty string. Hmm, I guess not. Just thinking outside the box.
Admin
I've only been hanging around here for a couple of months, but this particular comments page is the best collection of obvious-stating, point-missing and non-reading-of-article I've seen yet.
Admin
Isn't
(exp) ?? a : b
syntactic sugar for:
if (((exp) == true) || ((exp) != false)) return a; return b;
captcha: Doom (ain't that the truth)
Admin
string.Empty is the real WTF.
Admin
the first ones solved like this
the second one is solved like this.
Admin
I saw code like this all over an ASP application that I was working on for years. Until that fateful day when a German customer started experiencing problems. As it turns out, casting a boolean to a string in German is the word "falsh" and casting that back again to a boolean on an english web server turns out to be BOOM!
-K
Admin
You're missing a ? in your ternary expression.