Comment On Double Take

This is the story of two Daves. At first, they may appear to be from completely different worlds. One does client-side web development, one server-side web services. On further reflection, however, they have quite a bit in common. [expand full text]
« PrevPage 1 | Page 2Next »

Re: Double Take

2007-05-02 09:17 • by Mickey (unregistered)
You gotta love pointless if statements.

Re: Double Take

2007-05-02 09:19 • by pitchingchris
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

Re: Double Take

2007-05-02 09:26 • by Skizz (unregistered)
134565 in reply to 134564
pitchingchris:
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

Must be anticipating those quantum computers then.

Re: Double Take

2007-05-02 09:36 • by odi (unregistered)
134566 in reply to 134564
pitchingchris:
Since it can't be both null and blank at the same time on todays computers


You know Oracle? I love the fact that an emptry string is null...

Re: Double Take

2007-05-02 09:39 • by Dirk (unregistered)
134567 in reply to 134564
I think he just didn't know the throw keyword.

He must have intended to do this:

if (reportGroup == null) {
throw new NullPointerException();
}

Re: Double Take

2007-05-02 09:45 • by Anonymous (unregistered)
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.

Re: Double Take

2007-05-02 09:47 • by dbt (unregistered)
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 ....

Re: Double Take

2007-05-02 09:48 • by Keith Hackney (unregistered)
134570 in reply to 134567
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

Re: Double Take

2007-05-02 10:03 • by NS (unregistered)
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

Re: Double Take

2007-05-02 10:04 • by Luke (unregistered)
134572 in reply to 134570
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.

Re: Double Take

2007-05-02 10:11 • by Keith Hackney (unregistered)
134573 in reply to 134572
Not sure what you mean Luke

Re: Double Take

2007-05-02 10:13 • by trianglman (unregistered)
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.

Re: Double Take

2007-05-02 10:16 • by Welbog
134576 in reply to 134571
NS:
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
Are you meaning to say that you don't understand why people want to know whether a string is empty in the first place, or you don't understand why they choose that particular method to test whether a string is empty?

Re: Double Take

2007-05-02 10:16 • by Squeese (unregistered)
Isnt it missing a closing parenthesis ")" at the end there?
if ((reportGroup == null) && (reportGroup.equals("")) {..}

Re: Double Take

2007-05-02 10:18 • by pointless (unregistered)
why not add && new String("").length()==0 to the if while your at it

Re: Double Take

2007-05-02 10:19 • by 1emming (unregistered)

if ((reportGroup == null) && (reportGroup.equals("")) {
reportGroup = "";
}

That should be:

try{
if( reportGroup.equals("") && reportGroup.equalsIgnoreCase("")){
throw new Exception("empty");
}
}catch(Exception e){
if(e.getMessage().equals("empty")){
throw new Exception("Report group may not be empty");
}else{
throw new Exception("Report group is most likely 'null'");
}
}
//to be sure..
if(reportGroup==null)
throw new Exception("Report group is most likely 'null'");

Re: Double Take

2007-05-02 10:19 • by Elmo (unregistered)
"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%>;

Re: Double Take

2007-05-02 10:21 • by 1emming (unregistered)

if ((reportGroup == null) && (reportGroup.equals("")) {
reportGroup = "";
}


That is a useless test, it should be:

try{
if( reportGroup.equals("") && reportGroup.equalsIgnoreCase("")){
throw new Exception("empty");
}
}catch(Exception e){
if(e.getMessage().equals("empty")){
throw new Exception("Report group may not be empty");
}else{
throw new Exception("Report group is most likely 'null'");
}
}
//to be sure..
if(reportGroup==null || reportGroup.length()<0)
throw new Exception("Report group is most likely 'null', or the length is not enough

Re: Double Take

2007-05-02 10:23 • by 1emming (unregistered)
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="";
}

Re: Double Take

2007-05-02 10:24 • by Grant (unregistered)
134583 in reply to 134575
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.

Re: Double Take

2007-05-02 11:00 • by Thuktun
I recently hit this piece of pointless code in our own codebase:
public static final boolean isEmpty(String value){

return value.trim().equals("") || value == null;
}
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.

Re: Double Take

2007-05-02 11:01 • by XIU
134587 in reply to 134570
Keith Hackney:
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


reportGroup = reportGroup ?? string.Empty;

Re: Double Take

2007-05-02 11:08 • by Derrick Pallas
134590 in reply to 134577
Squeese:
Isnt it missing a closing parenthesis ")" at the end there?
if ((reportGroup == null) && (reportGroup.equals("")) {..}


Yes, thanks. (Fixed.)

Re: Double Take

2007-05-02 11:12 • by AGould
134592 in reply to 134563
Mickey:
You gotta love pointless if statements.


My Java-Fu is admittedly weak, but my first thought was "did he want an OR there instead of AND?"

Re: Double Take

2007-05-02 11:20 • by dkf (unregistered)
134593 in reply to 134587
XIU:
reportGroup = reportGroup ?? string.Empty;
That one makes me wonder if there is a ?! operator (the WTF?! operator) too...

Re: Double Take

2007-05-02 11:28 • by XIU
134594 in reply to 134593
dkf:
XIU:
reportGroup = reportGroup ?? string.Empty;
That one makes me wonder if there is a ?! operator (the WTF?! operator) too...


Maybe for C# 3, who knows...

Re: Double Take

2007-05-02 11:32 • by Thuktun
134595 in reply to 134593
dkf:
XIU:
reportGroup = reportGroup ?? string.Empty;
That one makes me wonder if there is a ?! operator (the WTF?! operator) too...
Syntax of this operator is as follows:
condition ?! ifTrue : ifFalse : fileNotFound

Re: Double Take

2007-05-02 11:32 • by snoofle (unregistered)
134596 in reply to 134585
Thuktun:
I recently hit this piece of pointless code in our own codebase:
public static final boolean isEmpty(String value){

return value.trim().equals("") || value == null;
}
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.

What about the anti-static Cling-Free (TM)? - you can never really be sure how final something is unless you explcitly specify it, and even then....

Re: Double Take

2007-05-02 11:40 • by Hel (unregistered)
134597 in reply to 134566
To know Oracle is to Hate Oracle!. To know NVL() is to hate Oracle a little less...

Re: Double Take

2007-05-02 11:42 • by Hel (unregistered)
134598 in reply to 134566
odi:
pitchingchris:
Since it can't be both null and blank at the same time on todays computers


You know Oracle? I love the fact that an emptry string is null...


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...

Re: Double Take

2007-05-02 11:43 • by I like your thinking (unregistered)
134599 in reply to 134596
public static final boolean isEmpty(String value){

return value.trim().equals("") || value == null;
}

Let's remember the other boolean values as well:


if (x == sortOfNull || // randomly has value
x == prettyNull || // has little value
x == nullerThanMost || // mostly null
x == null || // absolute-null
x ?! null : itsDeadJim : itsAliveJim : itJustWontDieJim) {
...
}

Re: Double Take

2007-05-02 11:45 • by PSWorx
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!!!>)

Re: Double Take

2007-05-02 11:47 • by hmm... (unregistered)
134601 in reply to 134587
?? only evaluate null value. so it misses the original authors wish to evaluate ""

Re: Double Take

2007-05-02 12:25 • by Jim (unregistered)
134607 in reply to 134576
This is more idiomatic in Java:

if (reportGroup == null || reportGroup.length() == 0) {
// Handle null case
}

Re: Double Take

2007-05-02 12:32 • by Rootbeer
134611 in reply to 134580
<i>Why not: var hasPriv = <%=hasPriv%>;</i>

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.

Re: Double Take

2007-05-02 12:36 • by Aaron (unregistered)
134613 in reply to 134601
hmm...:
?? only evaluate null value. so it misses the original authors wish to evaluate ""

Sarcasm?

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.

Re: Double Take

2007-05-02 12:38 • by Aaron (unregistered)
134614 in reply to 134611
Rootbeer:
<i>Why not: var hasPriv = <%=hasPriv%>;</i>

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.

Since when is boolean a nullable type? If it isn't set, it will evaluate to the default value (false) when the page is rendered.

Re: Double Take

2007-05-02 13:05 • by DigitalXeron
134629 in reply to 134614
Aaron:
Rootbeer:
<i>Why not: var hasPriv = <%=hasPriv%>;</i>

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.

Since when is boolean a nullable type? If it isn't set, it will evaluate to the default value (false) when the page is rendered.


How about Oracle?

Re: Double Take

2007-05-02 13:10 • by WTFNamingException (unregistered)
134631 in reply to 134629
[quote user="DigitalXeron]How about Oracle?[/quote]
No thanks!

Re: Double Take

2007-05-02 13:31 • by Malfist (unregistered)
134640 in reply to 134570
Keith Hackney:
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

Or is java:
reportGroup = (reportGroup == null || reportGroup.equals("")) "" : reportGroup

Re: Double Take

2007-05-02 13:33 • by tastey (unregistered)
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.

Re: Double Take

2007-05-02 14:53 • by Xero (unregistered)
134686 in reply to 134585
Thuktun:
I recently hit this piece of pointless code in our own codebase:
public static final boolean isEmpty(String value){

return value.trim().equals("") || value == null;
}
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.


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.

Re: Double Take

2007-05-02 15:01 • by merreborn
134689 in reply to 134571
NS:
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


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.

Re: Double Take

2007-05-02 15:29 • by iMalc (unregistered)
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.

Re: Double Take

2007-05-02 15:36 • by bobbo (unregistered)
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.

Re: Double Take

2007-05-02 16:05 • by Tyrathect (unregistered)
134715 in reply to 134705
Isn't

(exp) ?? a : b

syntactic sugar for:

if (((exp) == true) || ((exp) != false))
return a;
return b;


captcha: Doom (ain't that the truth)



Re: Double Take

2007-05-02 18:53 • by Jon (unregistered)
134759 in reply to 134587
XIU:
Keith Hackney:
reportGroup = String.IsNullOrEmpty(reportGroup) ? String.Empty : reportGroup;
reportGroup = reportGroup ?? string.Empty;
reportGroup = reportGroup ?? "";

string.Empty is the real WTF.

Re: Double Take

2007-05-02 19:22 • by brendan (unregistered)
the first ones solved like this




the second one is solved like this.

if (reportGroup == null)

reportGroup = "";

Re: Double Take

2007-05-02 19:32 • by mage (unregistered)
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

Re: Double Take

2007-05-02 19:43 • by Nonymous (unregistered)
134765 in reply to 134640
Malfist:

Or is java:
reportGroup = (reportGroup == null || reportGroup.equals("")) "" : reportGroup


You're missing a ? in your ternary expression.
« PrevPage 1 | Page 2Next »

Add Comment