Needless casts?

Discussion in 'Java' started by Joona I Palaste, Apr 24, 2004.

  1. 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 () ------------- 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
    Joona I Palaste, Apr 24, 2004
    #1
    1. Advertising

  2. Joona I Palaste <> 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 () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Ice cream sales somehow cause drownings: both happen in summer."
    - Antti Voipio & Arto Wikla
    Joona I Palaste, Apr 24, 2004
    #2
    1. Advertising

  3. Joona I Palaste

    Tony Morris Guest

    "Joona I Palaste" <> wrote in message
    news:c6cv4p$iha$...
    > 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 () ------------- 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
    Tony Morris, Apr 24, 2004
    #3
  4. Joona I Palaste

    Roedy Green Guest

    On 24 Apr 2004 05:49:13 GMT, Joona I Palaste <>
    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.
    Roedy Green, Apr 24, 2004
    #4
  5. Joona I Palaste

    Roedy Green Guest

    On 24 Apr 2004 05:49:13 GMT, Joona I Palaste <>
    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.
    Roedy Green, Apr 24, 2004
    #5
  6. Joona I Palaste

    Roedy Green Guest

    On 24 Apr 2004 05:49:13 GMT, Joona I Palaste <>
    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.
    Roedy Green, Apr 24, 2004
    #6
  7. Joona I Palaste

    Chris Uppal Guest

    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
    Chris Uppal, Apr 24, 2004
    #7
  8. Joona I Palaste

    VisionSet Guest

    "Roedy Green" <> wrote in message
    news:...

    > 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
    VisionSet, Apr 24, 2004
    #8
  9. Joona I Palaste

    Chris Uppal Guest

    VisionSet wrote:

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


    Hereditary insanity ?

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

    -- chris
    Chris Uppal, Apr 24, 2004
    #9
  10. Joona I Palaste

    Chris Uppal Guest

    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
    Chris Uppal, Apr 24, 2004
    #10
  11. On Sat, 24 Apr 2004 11:20:45 +0100, VisionSet wrote:

    > I can sort of understand someone uneccesarily writing:
    >
    > if ( ( a = true) == true )
    >
    > but what possible excuse is there for:
    >
    > if ( ( a == true) == true )


    Multiple small changes could result in that. From if(!b) to if(!b==true) to
    if((b==false)==true) to if((a==true)==true).

    Sometimes if(someLongCondition==false) is more readable than
    if(!someLongCondition), IMHO.
    Timo Kinnunen, Apr 24, 2004
    #11
  12. VisionSet <> scribbled the following:
    > "Roedy Green" <> wrote in message
    > news:...
    >> 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 )


    As I mentioned in another thread, some of my colleagues actually prefer
    to write:

    boolean v = false;
    if (value >= minimum) {
    if (value <= maximum) {
    v = true;
    }
    }
    return v;

    to writing:

    return value >= minimum && value <= maximum;

    I of course prefer the latter to the former, and even the Sun Java
    code conventions agree with me.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "The trouble with the French is they don't have a word for entrepreneur."
    - George Bush
    Joona I Palaste, Apr 25, 2004
    #12
  13. Joona I Palaste

    Roedy Green Guest

    On Sat, 24 Apr 2004 11:20:45 +0100, "VisionSet" <>
    wrote or quoted :

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


    by the way, since "a" is a boolean expression already the way you
    should write that is:

    if ( a )


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 25, 2004
    #13
  14. Joona I Palaste

    Roedy Green Guest

    On 25 Apr 2004 08:15:03 GMT, Joona I Palaste <>
    wrote or quoted :

    >return value >= minimum && value <= maximum;


    In mathematics that expression would be written

    minimum <= value <= maximum

    So I mirror that as closely as possible in java with:

    if ( minimum <= value && value <= maximum )


    If you are consistent about that it is always clear which is the lower
    and upper bound.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Apr 25, 2004
    #14
  15. Joona I Palaste

    Filip Larsen Guest

    Joona I Palaste wrote

    > return value >= minimum && value <= maximum;
    >
    > I of course prefer the latter to the former, and even the Sun Java
    > code conventions agree with me.


    Range tests also look good when you do them like

    return minimum <= value && value <= maximum;

    which can be read like "minimum <= value <= maximum" and that makes for
    good proofreading.


    Regards,
    --
    Filip Larsen
    Filip Larsen, Apr 25, 2004
    #15
  16. Joona I Palaste

    Icemerth Guest

    "Filip Larsen" <> wrote in message news:<c6g7pq$289c$>...
    > Joona I Palaste wrote
    >
    > > return value >= minimum && value <= maximum;
    > >
    > > I of course prefer the latter to the former, and even the Sun Java
    > > code conventions agree with me.

    >
    > Range tests also look good when you do them like
    >
    > return minimum <= value && value <= maximum;
    >
    > which can be read like "minimum <= value <= maximum" and that makes for
    > good proofreading.
    >
    >
    > Regards,


    y
    Icemerth, Apr 25, 2004
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2hyaXMgRGF2b2xp?=

    Web casts in ASP.Net

    =?Utf-8?B?Q2hyaXMgRGF2b2xp?=, Oct 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    498
    clintonG
    Oct 19, 2005
  2. James Stroud

    Needless copying in iterations?

    James Stroud, Sep 15, 2007, in forum: Python
    Replies:
    22
    Views:
    623
    =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=
    Sep 17, 2007
  3. Jukka Lahtinen
    Replies:
    1
    Views:
    862
    Jukka Lahtinen
    Feb 24, 2011
  4. Aaron Smith
    Replies:
    2
    Views:
    109
    Aaron Smith
    Mar 12, 2007
  5. Rainer Weikusat
    Replies:
    10
    Views:
    314
    C.DeRykus
    Mar 25, 2013
Loading...

Share This Page