Comment On containsNotEqualBackwards

Ahh, the things you can find in an old, gigantic system! Chris B recently uncovered this gem in an old Java application. It might be misnamed, but then again... how could one possibly describe what this is supposed to do... [expand full text]
« PrevPage 1 | Page 2Next »

Re: containsNotEqualBackwards

2007-09-24 09:16 • by death
Are they really reversing BOTH strings!?

I mean, it would make some sense if only one was reversed but this O_O.

Re: containsNotEqualBackwards

2007-09-24 09:19 • by Sven (unregistered)
I'd name the function "returnsTrueSometimesAndFalseOtherTimesYeahIDontReallyNowEither".

At least it'd be honest.

Re: containsNotEqualBackwards

2007-09-24 09:20 • by flux (unregistered)
And why are they taking the substring from 0 to 11? they might as well call this method method0.

Re: containsNotEqualBackwards

2007-09-24 09:20 • by Mith (unregistered)
I'd go further and spell "Know" correctly

Re: containsNotEqualBackwards

2007-09-24 09:20 • by awt (unregistered)
Good job the developer didn't spoil the fun by filling in the "description" in the comment block.

Re: containsNotEqualBackwards

2007-09-24 09:23 • by anon (unregistered)
And it's an instance method that doesn't in any way seem to care about the instance it is being invoked on.

Re: containsNotEqualBackwards

2007-09-24 09:24 • by Code monkey (unregistered)
I guess the programmer was supposed to check if the last 11 chars of a string match something else. So he reversed both strings and then took the now-first 0-11 range.

Probably because he couldn't think of a different way of getting the last 11 chars from a string.

Re: containsNotEqualBackwards

2007-09-24 09:30 • by MrTweek (unregistered)
public boolean last11CharsAreEqualButTheRestIsNot(String, String);

weird.

Re: containsNotEqualBackwards

2007-09-24 09:31 • by Dave (unregistered)
Oh, I see the WTF. He didn't reverse the bits in each character.

Re: containsNotEqualBackwards

2007-09-24 09:32 • by death
Fear the programmers who can use substring to create a WTF and still don't know how to get LAST 11 chars.

Re: containsNotEqualBackwards

2007-09-24 09:32 • by Vic (unregistered)
You call a code written in 2002 old? How old are you?

Re: containsNotEqualBackwards

2007-09-24 09:32 • by StrideColossus
And they missed the most important JavaDoc element:

@author clueless_dolt

Re: containsNotEqualBackwards

2007-09-24 09:37 • by Erik N (unregistered)
It's good they did what they did because.......

A.trim();
return A.endsWith(B.substring(B.length - 11));

Is sooooo confusing.

Re: containsNotEqualBackwards

2007-09-24 09:39 • by bstorer
You think that's bad? You should see mightContainEqualButNotSameObjectSlantwise.

Re: containsNotEqualBackwards

2007-09-24 09:43 • by bstorer
154414 in reply to 154411
Erik N:
It's good they did what they did because.......

A.trim();
return A.endsWith(B.substring(B.length - 11));

Is sooooo confusing.

Ah, but you also need to make sure they aren't equal, too.

Re: containsNotEqualBackwards

2007-09-24 09:46 • by Licky Lindsay
Awww... this was coded on Valentine's day. How sweet.

Re: containsNotEqualBackwards

2007-09-24 09:49 • by foo (unregistered)
154417 in reply to 154401
And it's an instance method that doesn't in any way seem to care about the instance it is being invoked on.

It all makes sense when you realize that the class is called StringReversedNotEuqalStrategyStrategy.

Re: containsNotEqualBackwards

2007-09-24 09:51 • by jesstech (unregistered)
154418 in reply to 154416
Well, that explains it. The programmer was probably busy doing.... other things. Alone.

Re: containsNotEqualBackwards

2007-09-24 09:52 • by KattMan
154419 in reply to 154416
Licky Lindsay:
Awww... this was coded on Valentine's day. How sweet.


In honor of the great valentines day massacre I'm sure.

Re: containsNotEqualBackwards

2007-09-24 09:52 • by allo (unregistered)
there is a Bug: if string1==string2==strings1.reverse() "otto"?, then it returns false, when it should not.

Re: containsNotEqualBackwards

2007-09-24 09:57 • by Rob G (unregistered)
154422 in reply to 154420
allo:
there is a Bug: if string1==string2==strings1.reverse() "otto"?, then it returns false, when it should not.


So, someone knows what the code is for.. we may have our culprit ;)

(Captcha - doom, takes you back to the glory days of PC gaming)...

Re: containsNotEqualBackwards

2007-09-24 10:01 • by bstorer
154423 in reply to 154420
allo:
there is a Bug: if string1==string2==strings1.reverse() "otto"?, then it returns false, when it should not.

How on Earth can you reasonably say what it's supposed to do?

Re: containsNotEqualBackwards

2007-09-24 10:08 • by DivineGod (unregistered)
154426 in reply to 154420
"otto" would fail the substring.

Re: containsNotEqualBackwards

2007-09-24 10:11 • by PerdidoPunk
WTFery:

1)reversing both substrings
2)what about palindromes?

Re: containsNotEqualBackwards

2007-09-24 10:14 • by jimi (unregistered)
154428 in reply to 154420
allo:
there is a Bug: if string1==string2==strings1.reverse() "otto"?, then it returns false, when it should not.

The palindrome bugs always get ya.

Re: containsNotEqualBackwards

2007-09-24 10:19 • by n9ds
Dumb question...shouldn't there be a test to make sure there actually *are* at least 11 characters first? It may be antiquated thinking (but hey, I'm 45 and I know COBOL...I'm an antique), but testing the first 11 characters of a 10 character string smells like buffer over-runs to me!

Re: containsNotEqualBackwards

2007-09-24 10:20 • by Gir
154430 in reply to 154428
This could be the way it's meant to function.

Re: containsNotEqualBackwards

2007-09-24 10:21 • by Gir
154431 in reply to 154429
It's java, the programmer doesn't have to care about buffer over-runs anymore :'(
Or it just throws an exception, which crashes teh program \o/ unless it's caught, but looking at the code it's probably not.

Re: containsNotEqualBackwards

2007-09-24 10:26 • by Strider
if(s1.equals(s2))
return false;
else if(s1.length() >= 11)
return s1.substring(s1.length() - 11).trim().equals(s2.trim());
else
throw new CompareFromStringShorterThanElevenCharactersException();

Re: containsNotEqualBackwards

2007-09-24 10:32 • by M&M (unregistered)
containsNotEqualBackwards("abcdefghijklmnop ", "fghijklmnop") won't work either

compareFrom.substring(0,11).trim()
should have been:
compareFrom.trim().substring(0,11)

Re: containsNotEqualBackwards

2007-09-24 10:36 • by Phlip
So...
* Returns false if the strings match exactly (including whitespace)
* Returns true if the second string matches the last 11 characters of the first string (ignoring leading/trailing whitespace)
* Returns false otherwise.
* Throws IndexOutOfBoundsException if the strings don't match exactly and the first string is less than 11 characters long.

I think that covers it.

Christ knows what it was actually intended to do though (actually, maybe he doesn't, I don't remember the part of the bible where he learned how to program in Java).

Re: containsNotEqualBackwards

2007-09-24 10:36 • by SkittlesAreYum (unregistered)
154436 in reply to 154431
Gir:
It's java, the programmer doesn't have to care about buffer over-runs anymore :'(
Or it just throws an exception, which crashes teh program \o/ unless it's caught, but looking at the code it's probably not.


Java won't throw an exception. If the end index is beyond the length of the string, it will just return up to the end of the string. Same with Perl's substring(), IIRC.

Re: containsNotEqualBackwards

2007-09-24 10:37 • by Blort (unregistered)
154437 in reply to 154408
Vic:
You call a code written in 2002 old? How old are you?


It should be impossible to call any Java "old". Some C might be old. A fair amount of FORTRAN is old. Almost all COBOL is old. Java isn't even middle-aged!

Perhaps calling 5 year-old code "old" some sort of insight into the mind of the Java developer? "Ohh, that's more than 12 months old and doesn't use the latest java.util.flibFlobStringParserWoble class, I'd better re-write it!"

Re: containsNotEqualBackwards

2007-09-24 10:37 • by brazzy
154438 in reply to 154429
n9ds:
Dumb question...shouldn't there be a test to make sure there actually *are* at least 11 characters first? It may be antiquated thinking (but hey, I'm 45 and I know COBOL...I'm an antique), but testing the first 11 characters of a 10 character string smells like buffer over-runs to me!

To elaborate Gir's answer
Nope, no buffer overruns in Java, since array boundaries are automatically checked on access. It will just throw an ArrayIndeyOutOfBoundsException or some such thing. Which is probably swallowed silently somwhere, judging from the code we see.

Re: containsNotEqualBackwards

2007-09-24 10:38 • by SkittlesAreYum (unregistered)
154439 in reply to 154436
SkittlesAreYum:
Gir:
It's java, the programmer doesn't have to care about buffer over-runs anymore :'(
Or it just throws an exception, which crashes teh program \o/ unless it's caught, but looking at the code it's probably not.


Java won't throw an exception. If the end index is beyond the length of the string, it will just return up to the end of the string. Same with Perl's substring(), IIRC.


Okay I'm going to go ahead and take this back. I read the Java documentation again and I missed the

Throws:
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

Re: containsNotEqualBackwards

2007-09-24 10:39 • by shadowman
154440 in reply to 154395
Does using monstrosities of parameter names like 'theStringToCompareFrom' actually lend any readability to the code? It seems like it detracts from it more than anything. Something short like fromString and toString (oh dammit, I guess that wouldn't work either), or whatever, would probably suffice.

Re: containsNotEqualBackwards

2007-09-24 10:41 • by Valdis Kletnieks (unregistered)
I'm surprised not a single person has noted that in multinational companies, there *might* just be a need to process BIDI (BidDirectional) langauges, or ones that are read right-to-left, so comparing the *last* 11 characters might actually be The Right Thing To Do.

Re: containsNotEqualBackwards

2007-09-24 10:51 • by snoofle
154443 in reply to 154412
bstorer:
You think that's bad? You should see mightContainEqualButNotSameObjectSlantwise.

I don't even want to think about the definition of slantwise ;)

Re: containsNotEqualBackwards

2007-09-24 10:55 • by punissuer
154444 in reply to 154437
Blort:
Vic:
You call a code written in 2002 old? How old are you?


It should be impossible to call any Java "old". Some C might be old. A fair amount of FORTRAN is old. Almost all COBOL is old. Java isn't even middle-aged!

Perhaps calling 5 year-old code "old" some sort of insight into the mind of the Java developer? "Ohh, that's more than 12 months old and doesn't use the latest java.util.flibFlobStringParserWoble class, I'd better re-write it!"

Consider that that whole field of web development is only around 12 years old, and how much Java grew and changed in its first few years. Also, would you use a web browser that was five years old?

Re: containsNotEqualBackwards

2007-09-24 10:57 • by Mcoder
154445 in reply to 154400
awt:
Good job the developer didn't spoil the fun by filling in the "description" in the comment block.


Or giving any piece of information that is not at the function declaration...

He gets bonus points for using a @return tag with the type of the return value. Oh, yes, and follows all those design by contracts gidelines...

Most usefull javadoc I eve saw :)

Re: containsNotEqualBackwards

2007-09-24 11:00 • by joh6nn (unregistered)
154446 in reply to 154444
punissuer:

Consider that that whole field of web development is only around 12 years old, and how much Java grew and changed in its first few years. Also, would you use a web browser that was five years old?


you mean IE6?

Re: containsNotEqualBackwards

2007-09-24 11:03 • by Mr Fred (unregistered)
154448 in reply to 154435
That's because all programming languages are the work of the devil.

Re: containsNotEqualBackwards

2007-09-24 11:04 • by Blort (unregistered)
154449 in reply to 154444
punissuer:
Blort:

Perhaps calling 5 year-old code "old" some sort of insight into the mind of the Java developer? "Ohh, that's more than 12 months old and doesn't use the latest java.util.flibFlobStringParserWoble class, I'd better re-write it!"


Consider that that whole field of web development is only around 12 years old, and how much Java grew and changed in its first few years.


True, but that just means that peoples perceptions are skewed. Just because your language is only a baby it doesn't make five year old code "old". It also doesn't mean it is automatically a good idea to continually re-write functioning code simply because the Sun Marketing Department thought up a new buzzword.

Also, would you use a web browser that was five years old?


Are you suggesting that Firefox (or any other modern web browser) doesn't contain any code written more than five years ago?

Re: containsNotEqualBackwards

2007-09-24 11:11 • by Mr Fred (unregistered)
154450 in reply to 154442
Valdis Kletnieks:
I'm surprised not a single person has noted that in multinational companies, there *might* just be a need to process BIDI (BidDirectional) langauges, or ones that are read right-to-left, so comparing the *last* 11 characters might actually be The Right Thing To Do.

Re: containsNotEqualBackwards

2007-09-24 11:14 • by Mr Fred (unregistered)
154451 in reply to 154442
Valdis Kletnieks:
I'm surprised not a single person has noted that in multinational companies, there *might* just be a need to process BIDI (BidDirectional) langauges, or ones that are read right-to-left, so comparing the *last* 11 characters might actually be The Right Thing To Do.


That is because the right to left vs left to right is merely the spatial display convention indicating which characters are first vs last. It has nothing to do with the ordinality of the string elements which is present in the string array.

Re: containsNotEqualBackwards

2007-09-24 11:29 • by MET
For me, the real WTF is just another case of people naming functions by how they are implemented rather than what they are for. The last 11 characters may be some domain specific information that may be guaranteed to be present by the time this function is called. For example if it were renamed to orderSpecialFeatureEqual() or whatever the real use was it would be less of a WTF.

OTOH reversing both the strings is just mental :)

Re: containsNotEqualBackwards

2007-09-24 11:30 • by FredSaw
154456 in reply to 154435
Phlip:
Christ knows what it was actually intended to do though (actually, maybe he doesn't, I don't remember the part of the bible where he learned how to program in Java).
This happened in that undocumented period between age 12 and age 30, and was done mainly so that he could program the CNC machines in Joseph's furniture factory.

Re: containsNotEqualBackwards

2007-09-24 11:45 • by Joon (unregistered)
154462 in reply to 154456
FredSaw:
This happened in that undocumented period between age 12 and age 30, and was done mainly so that he could program the CNC machines in Joseph's furniture factory.


That is beautiful...

Re: containsNotEqualBackwards

2007-09-24 11:50 • by Loren Pechtel (unregistered)
I also at first thought of backwards languages & UTF characters but that's not what's going on here. It's simply an attempt to see if the last 11 characters of the first string match the whole second string.

Re: containsNotEqualBackwards

2007-09-24 12:04 • by punissuer
154467 in reply to 154449
Blort:
Are you suggesting that Firefox (or any other modern web browser) doesn't contain any code written more than five years ago?
No, my point is just that the computing field moves very quickly, and anything dealing with the web seems to move more quickly still. Five-year-old code might still be in use, but I would definitely consider it old--sometimes that's a good thing. ;)
« PrevPage 1 | Page 2Next »

Add Comment