Comment On Friday Java Jumble

Last week's Friday Smorgasbord seemed to work out fairly well, so here's another go at it. But unlike last week, this all comes from one person (Koumynyka) who has collected these Java snippets from code reviewes and by purising the source tree. Names changed to protect the guilty and wrapped in a class for fun ... [expand full text]
« PrevPage 1 | Page 2Next »

Re: Friday Java Jumble

2005-03-18 13:32 • by Sweets
"lastName.charAt(0) == new String("*").charAt(0)"

Hey, whatever works right!

Re: Friday Java Jumble

2005-03-18 13:38 • by SurfaceTension
Alex Papadimoulis:

int getHttpPort() throws IllegalArgumentException {
/// and which is the illegal argument?
}






That one is my fav. What's funny is due to some other method that's
called within this one, the compiler probably forces him to put that
throw statement there since he didn't handle it within this method.
That's assuming the method's body isn't just a comment (:

Re: Friday Java Jumble

2005-03-18 13:47 • by rlewallen
This is got to be in the top 5 list of worst/funniest things I have
ever seen on this site, or any site for that matter.  You almost
have write crap like that on purpose.

Re: Friday Java Jumble

2005-03-18 13:48 • by Rick
31355 in reply to 31352

Sweets:
"lastName.charAt(0) == new String("*").charAt(0)"
Hey, whatever works right!


Unless timeliness is important.

Re: Friday Java Jumble

2005-03-18 13:49 • by Rick
31356 in reply to 31353
SurfaceTension:







 Alex Papadimoulis wrote:




  int getHttpPort() throws IllegalArgumentException {
/// and which is the illegal argument?
}





That one is my fav. What's funny is due to some other method that's called within this one, the compiler probably forces him to put that throw statement there since he didn't handle it within this method. That's assuming the method's body isn't just a comment (:


Java will not let you throw in superfluous 'throws'.

Re: Friday Java Jumble

2005-03-18 13:57 • by
Alex Papadimoulis:

 public horribleBreakingLoops() {
  try {
   for (int i = 0; i < 100; i++) {
    if (i == 98) {
     // Yes, java does have 'break' to exit loops
     throw new Exception();
    }
   }
  } catch (Exception e) {}
}
 
protected int myHorrorIndex = new Integer("1").intValue();


Those two are quality [:)]


I do have a concern that this is real production code - I mean, where's the usual horde of wrappers and interfaces that seem almost a compiler requirement for Java code? [:)]

Re: Friday Java Jumble

2005-03-18 14:01 • by Mike R

"protected int myHorrorIndex = new Integer("1").intValue();"


Why?!!


... No, really, why?


Did this guy have some vendetta against his employer and coworkers when he wrote this? Does he just hate java, Is he out to prove that java is a useless language, does he know something we don't about the simple statement:


protected int myHorrorIndex = 1;


As in, the statement I just wrote is horribly inefficient and could cause the world to collapse in on itself.


Was he smoking a good deal of crack when he wrote that? It just doesn't seem like inexperience, it seems more drug-induced.

Re: Friday Java Jumble

2005-03-18 14:03 • by
31359 in reply to 31353
SurfaceTension:



That one is my fav. What's funny is due to some other method that's
called within this one, the compiler probably forces him to put that
throw statement there since he didn't handle it within this method.





IllegalArgumentException is a subclass of RuntimeException.  The
compiler does not force you to explicitly handle RuntimeExceptions like
it does for all other types of exceptions.  It's essentially an
unchecked exception in a kludgy way.

Re: Friday Java Jumble

2005-03-18 14:11 • by Jeff S

Great post, excellent stuff.  Best we've had in a while.


I, too, am a huge fan of:


protected int myHorrorIndex = new Integer("1").intValue();


That's a classic!

Re: Friday Java Jumble

2005-03-18 14:29 • by znaps
31361 in reply to 31360


This one is my favourite for sheer demonstration that he doesn't have a clue about any of the language features.



[code language="c#"]
public boolean compareObjects(Object obj1 , Object obj2) {
if (obj1.equals(obj2) == true)
return true;
else
return false
;
}
[/code]



Re: Friday Java Jumble

2005-03-18 14:50 • by skicow

I agree


protected int myHorrorIndex = new Integer("1").intValue();


is the best one...it takes the idea of OOP to another level...."I can't just assign the number 1 to this integer variable, I need to construct a number 1 object, using a string, and then assign it! brilliant!" [H]

Re: Friday Java Jumble

2005-03-18 15:34 • by
To me, the real winner here is checkIsNull.  Prevent your code
from throwing NullPointerExceptions by trying to call the equals()
method of the possibly-null object, thereby throwing the
NullPointerException preemptively!  If that's actually the
behavior they want, they could have just left off the if.



Re: Friday Java Jumble

2005-03-18 16:46 • by
31368 in reply to 31362
protected int myHorrorIndex = new Integer("1").intValue();

To understand this you have to know something about how java works under the hood.  You see, in Java, everything is represented as a string at the lowest level, so if you said myHorrorIndex = 1 it would have to convert it into a string before casting it to an int.  Casting can be a very expensive operation in terms of performance, so your best bet is to start with a string and use the intValue method to return an int directly like the author of this code did.  I think I saw an article on this once but I can't find it now.

Re: Friday Java Jumble

2005-03-18 16:49 • by loneprogrammer
31369 in reply to 31368
To understand this you have
to know something about how java works under the hood.  You see,
in Java, everything is represented as a string at the lowest
level


Yes . . . a string of ones and zeros.  [*-)]

Re: Friday Java Jumble

2005-03-18 16:56 • by
if( !a.equals(null) ) {
Won't this NPE if a is null, thereby totally defeating the purpose?

Re: Friday Java Jumble

2005-03-18 17:01 • by bjmarte
31371 in reply to 31368

:
protected int myHorrorIndex = new Integer("1").intValue();

To understand this you have to know something about how java works under the hood.  You see, in Java, everything is represented as a string at the lowest level, so if you said myHorrorIndex = 1 it would have to convert it into a string before casting it to an int.  Casting can be a very expensive operation in terms of performance, so your best bet is to start with a string and use the intValue method to return an int directly like the author of this code did.  I think I saw an article on this once but I can't find it now.


Maybe in javascript that is true. Maybe.


I will not tolerate any more VB programmer comments after this.

Re: Friday Java Jumble

2005-03-18 17:07 • by bjmarte
31372 in reply to 31361
znaps:


This one is my favourite for sheer demonstration that he doesn't have a clue about any of the language features.



public boolean compareObjects(Object obj1 , Object obj2) {
if (obj1.equals(obj2) == true)
return true;
else
return false
;
}

 



This is my favorite just because of the absolute ignorance of basic programming concepts displayed.


1) Right off the bat you don't even need this function because it is exactly what the equals function does.


2) Ok, bad enough he wrote a function for it but they didn't even just return the value of the equals function. Nope, gotta write an if statement around it to check the value.


3) Ok, bad enough he used an if statement to check which boolean value to return but the coder has to check the return value of the equals function within the condition of the if statement itself.


I've never seen more idiotic code.

Re: Friday Java Jumble

2005-03-18 17:28 • by
Say it ain't so, say it ain't so! [:'(]

Re: Friday Java Jumble

2005-03-18 18:13 • by Rank Amateur

if (obj1.equals(obj2) == true)
   return true;


Whoa... how can he be sure that true is necessarily true? Shouldn't it be:


if (obj1.equals(obj2) == true
   if (true == true)
      return true;


--RA

Re: Friday Java Jumble

2005-03-18 19:59 • by
It almost looks like he read these guidelines and followed them...

Re: Friday Java Jumble

2005-03-18 21:17 • by diGriz
NYARRRRGGHHHH!!! The pain! The pain!



Wow... I never thought I'd get real bad headaches by reading crappy source code. Thanks for showing me it's possible.

Re: Friday Java Jumble

2005-03-18 22:43 • by drgonzo
31378 in reply to 31368
You better be joking.



:
protected int myHorrorIndex = new Integer("1").intValue();

To
understand this you have to know something about how java works under
the hood.  You see, in Java, everything is represented as a string
at the lowest level, so if you said myHorrorIndex = 1 it would have to
convert it into a string before casting it to an int.  Casting can
be a very expensive operation in terms of performance, so your best bet
is to start with a string and use the intValue method to return an int
directly like the author of this code did.  I think I saw an
article on this once but I can't find it now.

Re: Friday Java Jumble

2005-03-19 10:43 • by Jeff S
31380 in reply to 31376

:
It almost looks like he read these guidelines and followed them...


GREAT link ! [Y]  (now THAT's a good anonymous post )

Re: Friday Java Jumble

2005-03-19 16:34 • by BACON
31381 in reply to 31374
Rank Amateur:

if (obj1.equals(obj2) == true)
   return true;


Whoa... how can he be sure that true is necessarily true? Shouldn't it be:


if (obj1.equals(obj2) == true
   if (true == true)
      return true;


--RA



Rank Amateur:

if (obj1.equals(obj2) == true)
   return true;


Whoa... how can he be sure that true is necessarily true? Shouldn't it be:


if (obj1.equals(obj2) == true
   if (true == true)
      return true;


--RA





Personally, I think the following would have been a lot more prudent...



if (obj1.equals(obj2) == true && obj2.equals(obj1) == true)
      return true;





...or maybe even...



if (obj1.equals(obj2) == true && obj2.equals(obj1) == true && true == true && true != false)
      return true;





You can never be too careful, particularly when it comes to your definition of "equivalency".

Re: Friday Java Jumble

2005-03-19 16:36 • by BACON
31382 in reply to 31381
Wow, and apparently you can never be too careful while using the "copy" command.

For those wondering, I quoted that text twice as a failsafe in case one of them got corrupted in transit.  Remember, always keep a backup copy.

Re: Friday Java Jumble

2005-03-19 16:44 • by Irrelevant
wow, some great stuff in there. However...



bjmarte and drgonzo, you both get a metaphorical kick in the shin for
having no sense of sarcasm. I don't understand how anyone could've
missed that that comment was a joke.

Re: Friday Java Jumble

2005-03-20 09:48 • by andyandy
31386 in reply to 31383
Hm.. could this be code from a kindergarten's "Java Programming 101" assignment?
Hope so ;)

Re: Friday Java Jumble

2005-03-20 17:11 • by evnafets

Actually my favorite would have to be:

[code language="javascript"]public long fearOfCastingHorror() {
  return System.currentTimeMillis() + new
Double
(Math.random()).longValue();
  //instead of
return System.currentTimeMillis() + (long)Math.random();

}[/code]

Forget all the stuff about casting Doubles to longs.  You do realise that Math.random() returns a value between 0 and 1?  ie 0 <= Math.random() < 1. 


Given this little bit of knowledge, plus the fact that java converts to long by discarding the decimal portion, the result of (long)Math.random() will always be 0.


In otherwords, that whole Math.random thing is a NO-OP.  They might just as well have returned the System.currentTimeMillis()


WTF?
evnafets

Re: Friday Java Jumble

2005-03-20 18:19 • by phx

All classics ;)


My flatmate was reviewing and tidying code at work. He didnt have the heart to fix the various gems of:


if(o.GetType().Equals("".GetType())) ...


It was changed to:


if(o.GetType().Equals("What idiot wrote this?".GetType())) ...


Changes submitted.... :P

Re: Friday Java Jumble

2005-03-20 19:06 • by bjmarte
31389 in reply to 31383

Irrelevant:
wow, some great stuff in there. However...

bjmarte and drgonzo, you both get a metaphorical kick in the shin for having no sense of sarcasm. I don't understand how anyone could've missed that that comment was a joke.


Sorry, I don't think there is anything in your post to make it obvious that you were joking and I don't hang out here enough to know anything about you.  There are some pretty dumb comments sometimes.

Re: Friday Java Jumble

2005-03-20 22:48 • by Jon Limjap
31401 in reply to 31374
Rank Amateur:

if (obj1.equals(obj2) == true)
   return true;


Whoa... how can he be sure that true is necessarily true? Shouldn't it be:


if (obj1.equals(obj2) == true
   if (true == true)
      return true;


--RA





No no no! It has to be



if (obj1.equals(obj2) == true){

    return true;

}

else {

    return true2;

}



Hehehe.

Re: Friday Java Jumble

2005-03-21 01:41 • by brill
Wow... I'm an experianced Java guy, and I've seen some yucky code... but that is some of the worse I've seen.



Have you set up a "wall of shame" in your office yet?



Re: Friday Java Jumble

2005-03-21 01:43 • by brill
31416 in reply to 31353
SurfaceTension:
Alex Papadimoulis:
  int getHttpPort() throws IllegalArgumentException {
/// and which is the illegal argument?
}






That one is my fav. What's funny is due to some other method that's
called within this one, the compiler probably forces him to put that
throw statement there since he didn't handle it within this method.
That's assuming the method's body isn't just a comment (:




Actually IllegalArgumentException is a runtime exception and doesn't need to be caught... so no, the fellow just didn't have a clue.



 

Re: Friday Java Jumble

2005-03-21 02:53 • by V.
Surely this is for some class ore something.

A nice example for the students how they can fail? [:|]

Re: Friday Java Jumble

2005-03-21 04:19 • by Lorenzo Gatti
I'm in the minority that prefers the more subtly stupid

public stringsHorror(String lastName) {
if (lastName.charAt(0) == new String("*").charAt(0)
|| lastName.charAt(0) == new String("%").charAt(0)) {
// here goes the code ;)
}
}

because it has a number of possible compactions, from normal

char c=lastName.charAt(0);
if (c=='*'||c=='%'){
//...
}
to "clever"

if ("*%".indexOf(lastName.charAt(0))>=0){
//...
}


Re: Friday Java Jumble

2005-03-21 08:09 • by Anonymous
He's actually a genius. Such a genius, that he's written a genetic algorithm to write his programs.



The next few million iterations and it'll be better. Honest.



That, or a helper monkey.

Re: Friday Java Jumble

2005-03-21 09:05 • by mugs
31441 in reply to 31371
bjmarte:

:
protected int myHorrorIndex = new Integer("1").intValue();

To understand this you have to know something about how java works under the hood.  You see, in Java, everything is represented as a string at the lowest level, so if you said myHorrorIndex = 1 it would have to convert it into a string before casting it to an int.  Casting can be a very expensive operation in terms of performance, so your best bet is to start with a string and use the intValue method to return an int directly like the author of this code did.  I think I saw an article on this once but I can't find it now.


Maybe in javascript that is true. Maybe.


I will not tolerate any more VB programmer comments after this.



I'm not a java guy, but even if what he said IS true, he would have to recognize that the "1" in the statement above IS a string, so it would have to be parsed.  So this statement is doing the very thing that he claims it is intended to avoid.


Troll: 75%; Poor attempt at humor: 20%; Serious: 5%

Re: Friday Java Jumble

2005-03-21 09:08 • by withheld
31442 in reply to 31361
I'm afraid to have to report that some of the senior level developers at my 
current employer would reject the compareObjects method on code review, as it
violates the single entry/single exit principle. The "corrected" form would
be:

/**
* Compares two objects and returns true if they are equal or false if they
 * are not
*
* @returns true if the two arguments are equal or false if they are not
 */

public boolean compareObjects( Object obj1 , Object obj2 )
{
// initialize the return value
boolean bReturnValue = false;

// compare the two objects
if( obj1.equals(obj2) == true)
{
// The test passed: set the return value to true
bReturnValue = true;
}
else
{
// The test failed: set the return value to false
return false
;
}

// return the result
return( bReturnValue );
}

Re: Friday Java Jumble

2005-03-21 09:09 • by mugs
Alex Papadimoulis:

 public boolean compareObjects(Object obj1 , Object obj2) {
  if (obj1.equals(obj2) == true)
   return true;
  else
   return false
;
}




This one is my favorite.  If he had just condensed it down to "return obj1.equals(obj2);" he might have recognized the uselessness of the function.

Re: Friday Java Jumble

2005-03-21 11:55 • by Stephan Rose

For those of you who doubt that this may be production code, I have sad and breaking news for you. It is!!!


I used to have a co-worker, used to be a java programmer, and he was supposed to help me with a C# project I was working on. I promise you, this guy comitted EVERY SINGLE one of those atrocities up there, and then some!


Here's some of my favorites...


float value1 = 1.0f;
double value2 = double.Parse(value1.ToString());


--

OneOfMyControls instance1 = new OneOfMyControls();


instance1.Propery = someValue;


// Later in the code 5 source files later in a totally unrelated class


OneOfMyControls instance2 = new OneOfMyControls();


variable = instance2.Propery;


// Next, I get an e-mail stating the control I had written for him to use does not work, when he assigns a value to the property, the property does not get assigned. Yes, I did bang my head on the desk.


This is just the start....


 

Re: Friday Java Jumble

2005-03-21 13:36 • by Vasil
31472 in reply to 31441
mugs:
bjmarte:

:
protected int myHorrorIndex = new Integer("1").intValue();

To
understand this you have to know something about how java works under
the hood.  You see, in Java, everything is represented as a string
at the lowest level, so if you said myHorrorIndex = 1 it would have to
convert it into a string before casting it to an int.  Casting can
be a very expensive operation in terms of performance, so your best bet
is to start with a string and use the intValue method to return an int
directly like the author of this code did.  I think I saw an
article on this once but I can't find it now.


Maybe in javascript that is true. Maybe.


I will not tolerate any more VB programmer comments after this.



I'm not a java guy, but even if what he said IS true, he would have
to recognize that the "1" in the statement above IS a string, so it
would have to be parsed.  So this statement is doing the very
thing that he claims it is intended to avoid.


Troll: 75%; Poor attempt at humor: 20%; Serious: 5%





Well... I'm a Java programmer and will say that this is definetely NOT true.



int literals are stored in the java class file constant pool and are
read as ints and not as strings while loading the class file.



So there IS a difference between:



protected int myHorrorIndex = new Integer("1").intValue();

and

protected int myHorrorIndex = 1;




One possible reasons for this ... nuisance (besides the lack of ... experience of the developer :)) :



The guy may use some decompiled code. As most of the java developers
know - constants are directly inlined in class files at compile time.
So if the original code was:



protected int myHorrorIndex = new Integer(ConstantHolder.CONSTANT_1).intValue();



and CONSTANT_1 was a String equal to "1" then at decompile time we'll have the 'horror' line :)





This reminds me of a common error that Java newbies make:

Like I wrote - constants are inlined in class files at compile time. So
if you change a constant holder then you MUST recompile all classes
that reference the changed constant. Several years ago I had a
colleague who lost several hours wondering why his changed constant
does not change the application behavior - he just didn't care to
recompile the whole project but only the constant holder :).




Re: Friday Java Jumble

2005-03-22 11:47 • by GalacticCmdr
31542 in reply to 31360
protected int myHorrorIndex = new Integer("1").intValue();



I am pretending that was actually automatically generated by some
horribly disfigured tool, but on the plus side knowing that people code
like this does keep my future bright.



Re: Friday Java Jumble

2005-03-23 03:27 • by Mad Hatter
31588 in reply to 31453
Anonymous:
OneOfMyControls instance1 = new OneOfMyControls();

instance1.Propery = someValue;


// Later in the code 5 source files later in a totally unrelated class


OneOfMyControls instance2 = new OneOfMyControls();


variable = instance2.Propery;


// Next, I get an e-mail stating the control I had written for him
to use does not work, when he assigns a value to the property, the
property does not get assigned. Yes, I did bang my head on the desk.



This is just the start....




That's bad object design anyway. You should stay far away from
having public fields in your classes. Who knows what code may get a
reference to the object and change the field without permission.


Re: Friday Java Jumble

2005-03-23 05:49 • by th c# reader
31592 in reply to 31588
It's not a public field, it's a PROPERTY. He said it's C#, not Java.

Re: Friday Java Jumble

2005-03-23 05:51 • by th c# reader
31593 in reply to 31588
Anonymous:
Anonymous:
OneOfMyControls instance1 = new OneOfMyControls();

instance1.Propery = someValue;


// Later in the code 5 source files later in a totally unrelated class


OneOfMyControls instance2 = new OneOfMyControls();


variable = instance2.Propery;


// Next, I get an e-mail stating the control I had written for him
to use does not work, when he assigns a value to the property, the
property does not get assigned. Yes, I did bang my head on the desk.



This is just the start....




That's bad object design anyway. You should stay far away from
having public fields in your classes. Who knows what code may get a
reference to the object and change the field without permission.




It's not a public field, it's a PROPERTY. He said it's C#, not Java.




Re: Friday Java Jumble

2005-03-23 11:43 • by benjim
31613 in reply to 31588
Anonymous:
Anonymous:
OneOfMyControls instance1 = new OneOfMyControls();

instance1.Propery = someValue;


// Later in the code 5 source files later in a totally unrelated class


OneOfMyControls instance2 = new OneOfMyControls();


variable = instance2.Propery;


// Next, I get an e-mail stating the control I had written for him
to use does not work, when he assigns a value to the property, the
property does not get assigned. Yes, I did bang my head on the desk.



This is just the start....




That's bad object design anyway. You should stay far away from
having public fields in your classes. Who knows what code may get a
reference to the object and change the field without permission.





You've just done Java 101 haven't you? You've just quoted page 2! ;-)



It's C# code, so doesn't expose the feild itself. The property's accessor and mutator (get / set) are used like:



public string Foo {
get {
return foo;
}

set {
foo = value;
}
}


Re: Friday Java Jumble

2005-03-23 14:24 • by completely irrelevant
31644 in reply to 31383
Irrelevant:
wow, some great stuff in there. However...



bjmarte and drgonzo, you both get a metaphorical kick in the shin for
having no sense of sarcasm. I don't understand how anyone could've
missed that that comment was a joke.




Really.  Sounds to me like you're trying to save face after blowing your cover as a Java programmer...

Re: Friday Java Jumble

2005-03-24 02:25 • by Koumynyka
31712 in reply to 31356
It is not 'superfluous' ;) It is
RuntimeException and the Jaca compiler always allows to declare a
runtime exception even if you don't throw it. (although that you would
normally never declare that a runtime exception is thrown, except in
the javadoc)



However, the funniest thing is that the whole code really returns just
an int, whithout doing any additional computations. Just the int!









Re: Friday Java Jumble

2005-03-24 07:12 • by ananke

What do you think of this piece of code:


...


hashTable.put(Integer.toString(key.hashCode()), object);


...

Re: Friday Java Jumble

2005-03-24 09:51 • by dubwai
31728 in reply to 31723
ananke:

What do you think of this piece of code:


...


hashTable.put(Integer.toString(key.hashCode()), object);


...



It's stupid.

« PrevPage 1 | Page 2Next »

Add Comment