Overkill = minor WTF



  • <FONT color=#2b91af size=2><FONT color=#2b91af size=2>

    Convert</FONT></FONT><FONT size=2>.ToInt32(dr[dr.GetOrdinal(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"numobjects"</FONT></FONT><FONT size=2>)].ToString())</FONT>

    <FONT size=2></FONT> 

    <FONT size=2>Clarification: dr is an <FONT color=#2b91af size=2><FONT color=#2b91af size=2>OleDbDataReader

    </FONT></FONT></FONT>


  • System.Collections.List<Microsoft.VisualBasic.Compatibility.Variant> dr;






    ...?



  • Clarification:

    OP is getting an integer from the database, then using it as an index in the result of the same DB query.



  • @MiffTheFox said:

    Clarification:

    OP is getting an integer from the database, then using it as an index in the result of the same DB query.

     

    Oh? Because to me it looked like he was getting an int, converting it toString() just to convert it back to an Int32.



  • @amischiefr said:

    @MiffTheFox said:

    Clarification:

    OP is getting an integer from the database, then using it as an index in the result of the same DB query.

     

    Oh? Because to me it looked like he was getting an int, converting it toString() just to convert it back to an Int32.

     

    Here's the deal

    <FONT size=2>dr.GetOrdinal(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"numobjects"</FONT></FONT><FONT size=2>) returns the index of the column named "numobjects"</FONT>

    <FONT size=2>dr[dr.GetOrdinal(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"numobjects"</FONT></FONT><FONT size=2>)] returns the value in the colum with the index of the column named "numobjects"</FONT>

    this is redundant because you can just do <FONT size=2>dr[</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"numobjects"</FONT></FONT><FONT size=2>]</FONT>

    calling ToString on it is pointless because the Convert.ToInt32() accepts an object.

     so this: 

    Convert<FONT size=2>.ToInt32(dr[dr.GetOrdinal(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"numobjects"</FONT></FONT><FONT size=2>)].ToString())</FONT>

    <FONT size=2>could have been (in my opinion) more cleanly written as </FONT>

    <FONT size=2>Convert<FONT size=2>.ToInt32(dr[</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"numobjects"</FONT></FONT><FONT size=2>])</FONT></FONT>



  • @campkev said:

    [quote user="amischiefr"]

    [quote user="MiffTheFox"]

    Clarification:

    OP is getting an integer from the database, then using it as an index in the result of the same DB query.

     

    Oh? Because to me it looked like he was getting an int, converting it toString() just to convert it back to an Int32.

    [/quote]

     

    Here's the deal

    <font size="2">dr.GetOrdinal(</font><font color="#a31515" size="2"><font color="#a31515" size="2">"numobjects"</font></font><font size="2">) returns the index of the column named "numobjects"</font>

    <font size="2">dr[dr.GetOrdinal(</font><font color="#a31515" size="2"><font color="#a31515" size="2">"numobjects"</font></font><font size="2">)] returns the value in the colum with the index of the column named "numobjects"</font>

    this is redundant because you can just do <font size="2">dr[</font><font color="#a31515" size="2"><font color="#a31515" size="2">"numobjects"</font></font><font size="2">]</font>

    calling ToString on it is pointless because the Convert.ToInt32() accepts an object.

     so this: 

    Convert<font size="2">.ToInt32(dr[dr.GetOrdinal(</font><font color="#a31515" size="2"><font color="#a31515" size="2">"numobjects"</font></font><font size="2">)].ToString())</font>

    <font size="2">could have been (in my opinion) more cleanly written as </font>

    <font size="2">Convert<font size="2">.ToInt32(dr[</font><font color="#a31515" size="2"><font color="#a31515" size="2">"numobjects"</font></font><font size="2">])</font></font>

    [/quote]

    Okay, my mistake. I've only ever used ADO.NET once, and I seemed to (falsely) remember that Ordinal is it's terminology for an int, just like Variant is it's terminology for an object*.

    (* Also a false memory, Variant is from classic VB)



  • Here's a representative line from a pile of crapcode I currently have the "pleasure" of maintaining:


    float USTProz = (float) (new Double( (core.getFloat (VariablesNames.USTProz)) )).floatValue();

    Here, core is basically a wrapper for a HashMap; apparently the coder thought it would be cool not to store variables as members of classes, but to mash them all up into a big ball that you can pull them out of by calling them by their name (given as strings, which fortunately are at least defined and use as constants... mostly!) and the type you think they have (calling one of the methods getFloat, getString, getBoolean, ... I guess you see the pattern here). Who needs type safety, anyway, let's just let the programmer decide whether he wants to retrieve the value with the proper type or get a ClassCastException, e.g. for trying to retrieve a String with the getInteger method... (that's a WTF in itself, but not the point of my post).

    With that out of the way, let's break that down, shall we?

    1. We retrieve an item from some object via a method that we know returns a Float (for those without Java background knowledge: that's basically just a wrapper for a primitive float value which allows you to treat it like an object, since primitives are not objects in Java).
    2. We stuff the Float into a Double by using the latter's copy constructor. However, since there really is no constructor for Double that takes a Float argument, only one taking a double (the primitive, not the wrapper object), Java implicitly does 2 things: it first unboxes the Float to a float, then upcasts it to a double, before finally creating our Double object.
    3. We ask the Double for it's floatValue, which just so happens to be a float.
    4. Finally, just it case the float we just asked for is not really a float (you never know what that sneaky method might try to pass off to you when you're not paying attention), we cast it to a float again just to be sure.

    All that just to assign a Float value to a float variable - which Java would do automatically for you thanks to auto-unboxing. Okay, this is legacy code which might have been written before 1.5, where you had to do the (un)boxing manually, but even then this abomination would have been overkill.

    You might now begin to understand why my forehead has begun to take on a flat, vaguely desk-shaped indentation.



  • @campkev said:

    @amischiefr said:

    @MiffTheFox said:

    Clarification:

    OP is getting an integer from the database, then using it as an index in the result of the same DB query.

     

    Oh? Because to me it looked like he was getting an int, converting it toString() just to convert it back to an Int32.

     

    Here's the deal

    <font size="2">dr.GetOrdinal(</font><font size="2" color="#a31515"><font size="2" color="#a31515">"numobjects"</font></font><font size="2">) returns the index of the column named "numobjects"</font>

    <font size="2">dr[dr.GetOrdinal(</font><font size="2" color="#a31515"><font size="2" color="#a31515">"numobjects"</font></font><font size="2">)] returns the value in the colum with the index of the column named "numobjects"</font>

    this is redundant because you can just do <font size="2">dr[</font><font size="2" color="#a31515"><font size="2" color="#a31515">"numobjects"</font></font><font size="2">]</font>

    calling ToString on it is pointless because the Convert.ToInt32() accepts an object.

     so this: 

    Convert<font size="2">.ToInt32(dr[dr.GetOrdinal(</font><font size="2" color="#a31515"><font size="2" color="#a31515">"numobjects"</font></font><font size="2">)].ToString())</font>

    <font size="2">could have been (in my opinion) more cleanly written as </font>

    <font size="2">Convert<font size="2">.ToInt32(dr[</font><font size="2" color="#a31515"><font size="2" color="#a31515">"numobjects"</font></font><font size="2">])</font></font>

     

    Or, more simply, it could be written as:

    dr["numobjects"] as Int32;

    Assuming that "numobjects" is an integer in the DB.



  • @campkev said:

    <font size="2">cool explanation</font>
     

    Alright, thanks!  I've never used this language so i wasn't sure what that particular class was doing.  Thanks for clearing that up.



  • @mallard said:

    Or, more simply, it could be written as:

    dr["numobjects"] as Int32;

    Assuming that "numobjects" is an integer in the DB.

    The "as" keyword only works with reference types or nullable types. Int32 is neither. But (int)dr["numobjects"] should do just fine. Or dr.GetInt32(dr.GetOrdinal("numobjects")), I suppose.



  • @db2 said:

    @mallard said:

    Or, more simply, it could be written as:

    dr["numobjects"] as Int32;

    Assuming that "numobjects" is an integer in the DB.

    The "as" keyword only works with reference types or nullable types. Int32 is neither. But (int)dr["numobjects"] should do just fine. Or dr.GetInt32(dr.GetOrdinal("numobjects")), I suppose.

     

    Fine, my C# is a little rusty... How about this:

    dr["numobjects"] as Int32?;



  • @mallard said:

    Fine, my C# is a little rusty... How about this:

    dr["numobjects"] as Int32?;

    Yeah, I bet that would work. Or probably int?, since I think they're the same thing, right?


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.