Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Needless casts?

Reply
Thread Tools

Needless casts?

 
 
Joona I Palaste
Guest
Posts: n/a
 
      04-24-2004
I've seen a few casts in the Java code some of my colleagues write I
think are totally needless. For example, various upcasts. For example:

public class Core {
public void update(Component c) { /* ... */
}
}

public abstract class Component { /* ... */
}

public class MagicComponent extends Component { /* ... */
public void doThings() { /* ... */
new Core().update((Component)this);
}
}

That I could think would be explained by reminding people (who are
too stupid to look at the class declaration) that MagicComponent
extends Component, but then there are casts that cast an object to
the class it already is:

Item i = new Item();
component.add((Item)i);

and I once even saw this:

String error_notInitialized = (String)"Not initialized.";

What's the point in *that*?

(All code above has been paraphrased. The syntax is preserved but
the semantics aren't. The real code is under NDA.)

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The large yellow ships hung in the sky in exactly the same way that bricks
don't."
- Douglas Adams
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      04-24-2004
Joona I Palaste <(E-Mail Removed)> scribbled the following:
> That I could think would be explained by reminding people (who are
> too stupid to look at the class declaration) that MagicComponent
> extends Component, but then there are casts that cast an object to
> the class it already is:


> Item i = new Item();
> component.add((Item)i);


I meant, of course, cast a *reference* to the class it already is. You
can't cast objects in Java, only references. Sorry about the sloppy
terminology.

--
/-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Ice cream sales somehow cause drownings: both happen in summer."
- Antti Voipio & Arto Wikla
 
Reply With Quote
 
 
 
 
Tony Morris
Guest
Posts: n/a
 
      04-24-2004

"Joona I Palaste" <(E-Mail Removed)> wrote in message
news:c6cv4p$iha$(E-Mail Removed)...
> I've seen a few casts in the Java code some of my colleagues write I
> think are totally needless. For example, various upcasts. For example:
>
> public class Core {
> public void update(Component c) { /* ... */
> }
> }
>
> public abstract class Component { /* ... */
> }
>
> public class MagicComponent extends Component { /* ... */
> public void doThings() { /* ... */
> new Core().update((Component)this);
> }
> }
>
> That I could think would be explained by reminding people (who are
> too stupid to look at the class declaration) that MagicComponent
> extends Component, but then there are casts that cast an object to
> the class it already is:
>
> Item i = new Item();
> component.add((Item)i);
>
> and I once even saw this:
>
> String error_notInitialized = (String)"Not initialized.";
>
> What's the point in *that*?
>
> (All code above has been paraphrased. The syntax is preserved but
> the semantics aren't. The real code is under NDA.)
>
> --
> /-- Joona Palaste ((E-Mail Removed)) ------------- Finland --------\
> \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
> "The large yellow ships hung in the sky in exactly the same way that

bricks
> don't."
> - Douglas Adams


Yes, I see it often too, but what is your point ?
Are you questioning whether or not it is poor form ?
I am of the firm opinion that an explicit (and therefore, redundant) upcast
is "up there" with all the other abuses of the language.
In fact, I have my development environment raise a flag when it occurs.

If it was your own code, it creates needless clutter, that hinders
readability for no advantage.
If it is somebody else's code it also causes a distrust in the competency of
that developer to design before implementation (as well as other things,
such as understanding fundamental language semantics).
I do not, and never will, condone the EAIAD* software development paradigm,
albeit it being used so often.

*Excuses After Implementation After Design

I'd fail this code in a comprehensive code review.

--
Tony Morris
(BInfTech, Cert 3 I.T.)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
Sun Certified Programmer for the Java 2 Platform (1.4)
Sun Certified Developer for the Java 2 Platform


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-24-2004
On 24 Apr 2004 05:49:13 GMT, Joona I Palaste <(E-Mail Removed)>
wrote or quoted :

> new Core().update((Component)this);
> }
>}
>
>That I could think would be explained by reminding people (who are
>too stupid to look at the class declaration) that MagicComponent
>extends Component, but then there are casts that cast an object to
>the class it already is:


That sort of a cast says "I am only using this object qua Component.
If I try to get smart and use methods of MagicComponent, javac will
smite me down."


You are quite correct though they all could be removed, and it would
normally generate the same byte code, with exceptions such as this:


print( magicComponent );
and
print ( (Component) magicCompenent);

will match different signature versions of print. (Component) forces
use of the generic version.






--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-24-2004
On 24 Apr 2004 05:49:13 GMT, Joona I Palaste <(E-Mail Removed)>
wrote or quoted :

>Item i = new Item();
>component.add((Item)i);


This is merely documentation on what sort of animal is being added and
a verification that it indeed is. It has no runtime overhead.

Try reading someone else's code that has 50+ collections NONE of which
document anywhere what sort of critter is stored in each. It is like
reading a sentence like this:

Somebody did a thing to partya and stored a whatchamacalit in the
thingabamob filed under some key or other.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-24-2004
On 24 Apr 2004 05:49:13 GMT, Joona I Palaste <(E-Mail Removed)>
wrote or quoted :

>String error_notInitialized = (String)"Not initialized.";


Yucch! That drives me almost as nuts as

String error_notInitialized = (String) new String("Not initialized.");

or

if ( ( a == true) == true )

see http://mindprod.com/jgloss/newbie.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      04-24-2004
Roedy Green wrote:


> String error_notInitialized = (String) new String("Not initialized.");


Just by the way, the

new String("blah")

construct is semantically meaningful and even, albeit *very* occasionally,
useful. It guarantees that the String is a unique object, rather than possibly
sharing its identity with another, coincidentally existing, String literal
somewhere in the execution.

The cast of course, is simply an abomination....

-- chris


 
Reply With Quote
 
VisionSet
Guest
Posts: n/a
 
      04-24-2004
"Roedy Green" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> Yucch! That drives me almost as nuts as
>
> String error_notInitialized = (String) new String("Not initialized.");
>
> or
>
> if ( ( a == true) == true )


I can sort of understand someone uneccesarily writing:

if ( ( a = true) == true )

but what possible excuse is there for:

if ( ( a == true) == true )

--
Mike W



 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      04-24-2004
VisionSet wrote:

> but what possible excuse is there for:
>
> if ( ( a == true) == true )


Hereditary insanity ?

"if (a == true)" is bad enough...

-- chris


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      04-24-2004
Roedy Green wrote:

> print( magicComponent );
> and
> print ( (Component) magicCompenent);
>
> will match different signature versions of print. (Component) forces
> use of the generic version.


Which is why such casts are more than just redundant, they are actively *wrong*
unless they are intended to control method resolution.

-- chris



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Maven generates needless javadocs for generated classes Jukka Lahtinen Java 1 02-24-2011 07:27 AM
Needless copying in iterations? James Stroud Python 22 09-17-2007 04:44 AM
optparse, needless argument, mac os x intel Aaron Smith Ruby 2 03-12-2007 06:39 AM
More Needless single-user start-up choice Paul Hoberg Computer Support 1 08-14-2003 03:42 PM
Needless single-user start-up choice Paul Hoberg Computer Support 1 08-13-2003 09:22 PM



Advertisments