Representative line



  • This is VB.Net. We're inside a function (renamed and removed other parameters irrelevant to the wtf, to protect the guilty) declared like:

    Private Sub Foo(bar As Integer)

    and the line that gets added to the code is:

    If bar = 0 Then bar = Nothing

    I have three other programmers workin in the same team as me, and that was done by "the smart one". Pity me!



  • You'll have to be a bit more detailed, as I see no wtf here. There are certainly valid uses for that - if 0 means "no such thing", but the value is passed to a function taking an Object and expecting a Nothing for "no such thing".

    Is it too early to start talking about lightbulbs?



  • You're correct in assuming that the value is later passed to a function taking an object as a parameter, and that what he was trying to achieve (and failed at) was calling the function with Nothing if the value if bar was 0. 

    You're not a .Net developer I take it. The result of assigning Nothing (equivalent to null in the C family of languages) to an Integer (primitve type like int) is that the value will end up as 0.

    What he could have done was either:

    If bar = 0 Then
      DoSomething(Nothing)
    Else
      DoSomething(bar)
    End If

    Or he could have looked at the function and realized there was an overload prepared to take one of the Nullable objects (a generic wrapper around value types like ints and structs so you can pass null/Nothing to functions, there is even a convenient shorthand, "Integer?", for a nullable int. Something like (verbose, could be shortened to a oneliner):

    Dim tmpBar As Integer?
    If bar = 0 Then
      tmpBar = Nothing
    Else
      tmpBar = bar
    End If
    DoSomething(tmpBar)

    Or perhaps even have changed the type of bar, to simplify it further. Instead he introduced a no-op thet may even be optimized away by the compiler, failed to test it properly himself not discovering that 0 was still passed on through the system. Not that it mattered because the database procedure that eventually received the value was written to treat 0 and NULL the same for that value.

    And the "too early" depends not only on your timezone but also on your latitude. Is it dark?



  • @arbitrary said:

    You're not a .Net developer I take it. The result of assigning Nothing (equivalent to null in the C family of languages) to an Integer (primitve type like int) is that the value will end up as 0.


    I am a .Net developer, but I didn't know that. I suspect it's limited to VB.Net. C# would just give a compile-time error.



  • @pjt33 said:

    @arbitrary said:

    You're not a .Net developer I take it. The result of assigning Nothing (equivalent to null in the C family of languages) to an Integer (primitve type like int) is that the value will end up as 0.


    I am a .Net developer, but I didn't know that. I suspect it's limited to VB.Net. C# would just give a compile-time error.

    Nothing is both the Null and the default() of VB.Net.

    When used on an object it's Null.

    When used on a non-object (primitive, structure) it's default(T)



    Ofcourse, default(Int32) is just plain 0, so it's like saying: if (bar == 0) { bar = 0; }



  • You can use the Integer? type in VB? I thought you had to use Nullable(Of Integer), since Integer? was only supported in C#...

    Also, was that Foo function seriously declared without a return type? Ugh...



  • Well I didn't know that it was actually defined to be the same as the default value, <rant>I thought it was just an artifact of the "It's VB, we can't possibly give a compile error if it's likely that the programmer has done something he didn't understand, we'll just hide the error and let their customers find it" policy at Microsoft. If we want the default what is wrong with making us actually type "default"?</rant>

    I just checked the docs to make sure, and it's been this way since the beginning. Why, for the love of Ritchie, would they use the keyword Nothing, when "Empty" or "Default" would describe it so much better.

    ekolis:

    The Integer? syntax has been in VB for a while, since VS2008/.Net3.5 at least. (On msdn it's mentioned in the 3.5 docs but not on the same page of the 3.0 docs).

    And yes, no return type. (Sub, not Function, same as a void function in C#). It should either succeed or throw an exception the way it has been designed, no wtf there that I can see.



  • Oh, duh, I missed that it was a sub...



  • @pjt33 said:

    C# would just give a compile-time error.
    As god intended...


Log in to reply