Signs of stupid Java code

J

Joona I Palaste

Can you think of examples of Java code that are signs of programmer
stupidity? I'll start:

synchronized (new Object()) {
/* ... */
}

The synchronisation is utterly pointless in this case.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"'So called' means: 'There is a long explanation for this, but I have no
time to explain it here.'"
- JIPsoft
 
T

Thomas G. Marshall

Joona I Palaste said:
Can you think of examples of Java code that are signs of programmer
stupidity? I'll start:

synchronized (new Object()) {
/* ... */
}

The synchronisation is utterly pointless in this case.

Yep.

If the intent of this newsgroup thread is to show junior engineer mistakes,
then you're likely to get a bunch of interesting and humorous examples here,
provided that they are /real/ and not made up. I'm really interested in
this one, since I've bumped into quite a few: I'll go digging and see if I
can find them.

If the intent of the newsgroup thread is to show "pet peeves" and perceived
mistakes in the java language proper, then you'll likely start a HUGE
argument newsgroup thread, which is also instructive enough.

Of the latter, I'll throw in:

import <package>.<package>.<class>.*;

....for unqualified access to all id's within <class>.
 
K

Kevin

Hi, Thomas! I'm a Java newbie so I have to ask this question: What is
wrong with
import <package>.<package>.<class>.*; ? I've seen this in a lot of my Java
books and thought
that this was ok (but what do I know?). I don't want to be making a mistake
by doing this if it is wrong.

Thanks,

Kevin

--
"Experience is a hard teacher because she gives the test first, the lesson
later."

"Thomas G. Marshall" <[email protected]>
wrote in message
....
 
A

Anton Spaans

The import statement can be used to import classes into your source-code.

So,

import <package1>.<package2>.<class>;
just importing the class <class> is fine,

so is this, using a 'wildcard':
import <package1>.<package2>.*;

importing all classes from <package2>

but <class>.* does not make any sense.... since <class> is a class not a
package.
(like treating a file as a directory, for example)
-- Anton.
 
T

Thomas G. Marshall

The import statement can be used to import classes into your
source-code.

So,

import <package1>.<package2>.<class>;
just importing the class <class> is fine,

so is this, using a 'wildcard':
import <package1>.<package2>.*;

importing all classes from <package2>

but <class>.* does not make any sense.... since <class> is a class
not a package.

NO, it is allowed!

import java.lang.Math.*; // ok

I was recently educated on that horrific construct myself in this newsgroup.
 
C

Christophe Vanfleteren

Thomas said:
NO, it is allowed!

import java.lang.Math.*; // ok

I was recently educated on that horrific construct myself in this
newsgroup.

I thought that was only allowed in java 1.5, where that construct is called
"static imports" ?
 
T

Thomas G. Marshall

Christophe Vanfleteren said:
I thought that was only allowed in java 1.5, where that construct is
called "static imports" ?

1. Don't know, (don't even WANT to know :) )
2. Doesn't matter: this was an example of pet peeves and [my] perceived
mistakes in the java language proper, as specified.
 
D

Darryl L. Pierce

Joona said:
Can you think of examples of Java code that are signs of programmer
stupidity? I'll start:

synchronized (new Object()) {
/* ... */
}

The synchronisation is utterly pointless in this case.

Hashtable values = new Hashtable();

values.put((Object )foo,bar);
 
D

Darryl L. Pierce

Tor said:
But keep in mind that casting to Object is not always pointless:

In the above case it is. Even if the value of foo were null, it's still an
instance of Object as far as the compiler and VM are concerned.
public void foo(Object o) { }

public void foo(String s) { }

foo( (Object)null ); // Need to qualify null

A very contrived example to say the least.
 
T

Tor Iver Wilhelmsen

Darryl L. Pierce said:
values.put((Object )foo,bar);

But keep in mind that casting to Object is not always pointless:

public void foo(Object o) { }

public void foo(String s) { }

foo( (Object)null ); // Need to qualify null
 
G

Guest

Ben_ said:
Strange enough that Roedy hasn't posted this link yet :):
"How to Code Like a Java Newbie" (http://mindprod.com/newbie.html)

Well, in the nature of a good flame war, I'm going to take
exception with one of his examples:

===
If Redundancy

if (i <= 10)
{
/* ... */
}
else if ( 11 <= i && i <= 15 )
{
/* ... */
}

should be written:
if (i < = 10)
{
/* ... */
}
else if ( i <= 15 )
{
/* ... */
}
===

The problem with coding this way is that often times
it will mean that you have to touch long runs of
if-else constructs in multiple places if you make
a change to one condition. Yes, it's *logically* and
even *stylistically* cleaner code, but it may be
harder to maintain.

Anyway, I commonly use redundant tests in cases where
I expect the code may be dinked with later. Sometimes
I even add comments indicating this.

--arne
 
J

John C. Bollinger

Darryl said:
A very contrived example to say the least.

Not at all! It is not such an uncommon situation, especially as it
applies equally if you want to invoke the generic method with an
argument of type String. It wouldn't quite be right to say that I see
this kind of thing all the time, but I certainly see it often enough to
consider it a mainstream example.


John Bollinger
(e-mail address removed)
 
T

Thomas G. Marshall

Darryl L. Pierce said:
In the above case it is. Even if the value of foo were null, it's
still an instance of Object as far as the compiler and VM are
concerned.


A very contrived example to say the least.


Not really---I've bumped into precisely that issue something like 4 times in
8 years, which is enough IMHO to warrant understanding it.
 
D

Darryl L. Pierce

Thomas said:
Not really---I've bumped into precisely that issue something like 4 times
in 8 years, which is enough IMHO to warrant understanding it.

I've hit code like that too. And, it fits perfectly into this thread. ;)
 
D

Darryl L. Pierce

John said:
Not at all! It is not such an uncommon situation, especially as it
applies equally if you want to invoke the generic method with an
argument of type String. It wouldn't quite be right to say that I see
this kind of thing all the time, but I certainly see it often enough to
consider it a mainstream example.

If you go back to my original post on the subject of casting to Object, it
was related to casting the key for a Hastable to Object. I work with an
engineer who does exactly that pathologically. Even after telling him it's
unnecessary, he still does it.

As for having to cast null to be an object, that would sound like the class
you're invoking should have a no-arg version of foo(), rather than you
having to cast null in order to invoke a method.
 
T

Thomas G. Marshall

Darryl L. Pierce said:
I've hit code like that too. And, it fits perfectly into this thread.
;)

Was I clear? Maybe not, if not then I'm sorry.

The point I was making was that code like that is /not/ an example of bad
code, and is a perfectly acceptable practice. I've only seen the need for
it 4 times in 8 years, but there is definately a need for it.

You may be seeing many cases where the casts are not warranted, particularly
by folks who don't understand polymorphism. But you were wrong when you
said that the example:

foo((Object)null);

was very contrived. It isn't.
 
T

Tor Iver Wilhelmsen

Darryl L. Pierce said:
A very contrived example to say the least.

Why do you think so? Unless you don't like generalized examples and
have little experience with the situation yourself. Or do you never
ever pass null as a parameter to a method? Are you perhaps opposed to
polymorphism and instead would call the methods fooObject() and
fooString() respectively?

A concrete example, then: JDialog's constructors with no parent, you
need to be explicit whether that's no parent Dialog or no parent
Frame.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top