Normally I would agree



  • I was given a task to incorporate some code written by a colleague to a web application I am working with. I got his code and among the other WTFs I found the following:

            if ((int)DateTime.Now.AddDays(additionaldays).DayOfWeek == 7)
            {
                ....
            }
    

    For those who doesn't know DayOfWeek is an enumeration type, which is defined as:

          public enum DayOfWeek {  Sunday = 0,  Monday = 1,  Tuesday = 2,  Wednesday = 3,  Thursday = 4,  Friday = 5,  Saturday = 6 }
    

    so it can't be equal 7. The way of comparing by converting enum type to integer and then comparing it with a number is WTF itself.

    But it's alone was not so interesting so that I would post it here. But the following chat with the author made me speechless. I told him that this line of code will not ever execute, and received the following reply:

       -- normaly I would agree but I was seeing 7 during testing. 
    

    So I sent them the same enum definition I post here and got the following:

       -- I did't think so either until  I saw it.
    

    Among the other gems in the same piece of code is a interesting way to get a Date part from the DateTime structure (I am not even mentioning local variables prefixed with underscore and using upper camel casing and brilliant string.ToString() construction):

                string _StartDate = StartBusinessDate.ToString("MM/dd/yyyy");
                if (DateTime.TryParse(_StartDate.ToString(), out StartDate))
                {
                      ....
                }
    

    , using Session variable to pass a variable between functions in the same postback, not using "using" statement or at least "try/finally" block for the IDisposable objects, like IDataReader or SqlConnection, using strange conditions like:

       if (!IsPostBack == true)
       {
           ...
       }
    

    etc., etc.



  • Holy fuck, if it weren't for you telling me your coworker was a guy, I would swear that I once took over their old work...

    Especially the string.ToString() constructs throughout the code, and my personal favourite was casting null to a specific type:

    <FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    myType = (</FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyType</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>)</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>null</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;

    </FONT></FONT>

    Also littered throughout the code were methods which would return a bool from a DB call, only to convert that bool to an integer:

    <FONT size=2 face=Consolas><FONT size=2 face=Consolas></FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>bool</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> returnValue = cmd.DbCall();</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>if</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> (returnValue == </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>true</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>)

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>  return</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> 1;</FONT></FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>else

    </FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>  return</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> 0;

    </FONT></FONT>

    Naturally, the calling code would then do checks like if (value == 1), etc...



  • @C-Octothorpe said:

    and my personal favourite was casting null to a specific type:

    <font size="2" face="Consolas"><font size="2" face="Consolas"> </font></font>

    <font size="2" face="Consolas"><font size="2" face="Consolas">myType = (</font></font><font size="2" color="#2b91af" face="Consolas"><font size="2" color="#2b91af" face="Consolas"><font size="2" color="#2b91af" face="Consolas">MyType</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">)</font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">null</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">;</font></font>

     

    If you have two methods with the same name, each of which takes a parameter of a nullable type, you might actually need to do this.

     



  • @Someone You Know said:

    @C-Octothorpe said:

    and my personal favourite was casting null to a specific type:

    <FONT size=2 face=Consolas><FONT size=2 face=Consolas></FONT></FONT>

    <FONT size=2 face=Consolas><FONT size=2 face=Consolas>myType = (</FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyType</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>)</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>null</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;</FONT></FONT>

     

    If you have two methods with the same name, each of which takes a parameter of a nullable type, you might actually need to do this.

    No, you're assuming too much.  I've also seen it being applied to the Session object, like this:

    <FONT size=2 face=Consolas>Session["KeyName"] = (SomeType)null;</FONT>



  •  Sweet. I hope you're going to try to deploy this outside the US, where TryParse() will start behaving erratically. Then you can assign the ticket to the original author and tag it "told-you-so".


  • Trolleybus Mechanic

    @C-Octothorpe said:

    Also littered throughout the code were methods which would return a bool from a DB call, only to convert that bool to an integer:

    <font size="2" face="Consolas"><font size="2" face="Consolas"></font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">bool</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> returnValue = cmd.DbCall();</font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> </font></font>

    <font size="2" face="Consolas"><font size="2" face="Consolas"></font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">if</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> (returnValue == </font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">true</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">)</font></font>

    <font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">  return</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> 1;</font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> </font></font>

    <font size="2" face="Consolas"><font size="2" face="Consolas"></font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">else</font></font></font>

    <font size="2" face="Consolas"><font size="2" face="Consolas"> </font></font>

    <font size="2" face="Consolas"><font size="2" face="Consolas"></font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">  return</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> 0;</font></font>

    Naturally, the calling code would then do checks like if (value == 1), etc...

     

     

    Wait, your co-worker used ints? Luxury. I had to refactor a ton of this:

     

    @AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA said:

    Also littered throughout the code were methods which would return a bool from a DB call, only to convert that bool to an integer:

    <font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">bool</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> returnValue = cmd.DbCall();</font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> </font></font>

    <font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">if</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> (returnValue.ToString() == </font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">"True"</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">)</font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">
      return</font></font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"> "True";
    <font size="2" color="#0000ff" face="Consolas">else
      return</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> "False";</font></font>




     



  • @Lorne Kates said:

    @C-Octothorpe said:

    my example

    Wait, your co-worker used ints? Luxury. I had to refactor a ton of this:@AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA said:

    worse example

    Yeah, I saw a few of those too.  Another favourite would be the return void, but have a out msg parameter, which could return either an empty string or null, but the calling code would only check if its null.  Or sometimes, the "data access layer" would construct html tables by concatentating strings...  Inline style and all...  );



  • @Someone You Know said:

    @C-Octothorpe said:

    and my personal favourite was casting null to a specific type:

    <font size="2" face="Consolas"><font size="2" face="Consolas">
    </font></font>

    <font size="2" face="Consolas"><font size="2" face="Consolas">myType = (</font></font><font size="2" color="#2b91af" face="Consolas"><font size="2" color="#2b91af" face="Consolas"><font size="2" color="#2b91af" face="Consolas">MyType</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">)</font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">null</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">;</font></font>

     

    If you have two methods with the same name, each of which takes a parameter of a nullable type, you might actually need to do this.

     

    Another situation is that C# requires the two results of the ternary operator to be the same type, so if one is null, it must be cast to the type of the other or a compile-time error results.

    It sounds like that's not your problem here, though.



  • @corgimonster said:

    @Someone You Know said:

    @C-Octothorpe said:

    and my personal favourite was casting null to a specific type:

    <FONT size=2 face=Consolas><FONT size=2 face=Consolas></FONT></FONT>

    <FONT size=2 face=Consolas><FONT size=2 face=Consolas>myType = (</FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyType</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>)</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>null</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;</FONT></FONT>

     

    If you have two methods with the same name, each of which takes a parameter of a nullable type, you might actually need to do this.

     

    Another situation is that C# requires the two results of the ternary operator to be the same type, so if one is null, it must be cast to the type of the other or a compile-time error results.

    It sounds like that's not your problem here, though.

    You guys are a lot more forgiving than I am...  I try to give the benefit of the doubt whenever I can, but if you've seen some of the horrors I've seen (particularly with the developer I speak of), you would understand. 

    I've already created several posts in the past regarding the pile of garbage I've inherited, and I think might dig up the now decomissioned code for a few more examples.



  • @corgimonster said:

    Another situation is that C# requires the two results of the ternary operator to be the same type, so if one is null, it must be cast to the type of the other or a compile-time error results.

    No, the following is compiled without any problem in C#:

        SomeType val;
        .....
        SomeType st = condition ? val : null;
    


  • @gribunin said:

    @corgimonster said:

    Another situation is that C# requires the two results of the ternary operator to be the same type, so if one is null, it must be cast to the type of the other or a compile-time error results.

    No, the following is compiled without any problem in C#:
        SomeType val;
        .....
        SomeType st = condition ? val : null;
    

    This won't though:

    <FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>

    int</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>? test = (</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>true</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>) ? 1 : </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>null</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;

    </FONT></FONT>

    Your example works with objects, but won't work with structs.

    <FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>

    TestStruct</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>? s = (</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>true</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>) ? </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>new</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>TestStruct</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>() : </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>null</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;

    </FONT></FONT>

  • Trolleybus Mechanic

    @gribunin said:

    @corgimonster said:

    Another situation is that C# requires the two results of the ternary operator to be the same type, so if one is null, it must be cast to the type of the other or a compile-time error results.
    No, the following is compiled without any problem in C#:
        SomeType val;
        .....
        SomeType st = condition ? val : null;

     
    Try this:
    @this will will not compile said:

    OleDbParameter orv = new OleDbParameter("wtf", OleDbType.Double);
    orv.Value = user_value < 0 ? DBNull.Value : user_value;

     
    Even worse is VB.Net, which doesn't short-circuit its lame-ass ternary function:
    @this will crash and fuck you up said:

    value = IIf(user_value = 0, 0, 42 / user_value)

    Even worse is when it doesn't crash, and unexpectedly evaluates the second value.
    @this will just fuck you up said:

    Partial Class test_stupid
        Inherits System.Web.UI.Page
        Public SomeValue As Integer = 5
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                Dim value As Integer = 5
                value = IIf(value = SomeValue, SomeValue, IncreaseByOne())
                ' Both are 5, so both should end up as 5, right? Right?        
                Response.Write("Value is " & value & " and some SomeValue is " & SomeValue)
                ' 5 and 6. OH SHIZNIT!
        End Sub
     
        Protected Function IncreaseByOne() As Integer
            SomeValue = SomeValue + 1
            Return SomeValue
        End Function
    End Class




  • @C-Octothorpe said:

    @gribunin said:

    @corgimonster said:

    Another situation is that C# requires the two results of the ternary operator to be the same type, so if one is null, it must be cast to the type of the other or a compile-time error results.

    No, the following is compiled without any problem in C#:
        SomeType val;
    .....
    SomeType st = condition ? val : null;

    This won't though:

    <font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"> </font></font></font>

    <font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">int</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">? test = (</font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">true</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">) ? 1 : </font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">null</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">;</font></font>

    Your example works with objects, but won't work with structs.

    <font color="#2b91af" size="2" face="Consolas"><font color="#2b91af" size="2" face="Consolas"><font color="#2b91af" size="2" face="Consolas"> </font></font></font>

    <font color="#2b91af" size="2" face="Consolas"><font color="#2b91af" size="2" face="Consolas"><font color="#2b91af" size="2" face="Consolas">TestStruct</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">? s = (</font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">true</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">) ? </font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">new</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> </font></font><font color="#2b91af" size="2" face="Consolas"><font color="#2b91af" size="2" face="Consolas"><font color="#2b91af" size="2" face="Consolas">TestStruct</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">() : </font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">null</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">;</font></font>

    I should have clarified... the Nullable struct is the issue. This works:

    int? test = (true) ? new Nullable<int>(1) : new Nullable<int>();

    as do probably a dozen other ways to write the same thing.



  • Actually pretty much all of those won't work because you're all using the assignment operator for equivalence.



  • @corgimonster said:

    This won't though:

    <font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">
    </font></font></font>

    <font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">int</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">? test = (</font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">true</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">) ? 1 : </font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">null</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">;</font></font>

    I would write:

    <font size="2" face="Courier New" color="black"><font color="#0000ff">int</font>? i = (<font color="#0000ff">true</font>) ? (<font color="#0000ff">int</font>?)5 :<font color="#0000ff">null</font>;</font>


  • @users complaining about ternary operator said:

    blah blah

    RTFM

    "Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other."

    From http://msdn.microsoft.com/en-US/library/ty67wk28(v=VS.100).aspx



  • @serguey123 said:

    @users complaining about ternary operator said:

    blah blah

    RTFM

    "Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other."

    From http://msdn.microsoft.com/en-US/library/ty67wk28(v=VS.100).aspx

    Really? Who was complaining?

    RTFP



  • @C-Octothorpe said:

    @Someone You Know said:

    @C-Octothorpe said:

    and my personal favourite was casting null to a specific type:

    <font size="2" face="Consolas"><font size="2" face="Consolas"></font></font>

    <font size="2" face="Consolas"><font size="2" face="Consolas">myType = (</font></font><font size="2" color="#2b91af" face="Consolas"><font size="2" color="#2b91af" face="Consolas"><font size="2" color="#2b91af" face="Consolas">MyType</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">)</font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">null</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">;</font></font>

     

    If you have two methods with the same name, each of which takes a parameter of a nullable type, you might actually need to do this.

    No, you're assuming too much.  I've also seen it being applied to the Session object, like this:

    <font size="2" face="Consolas">Session["KeyName"] = (SomeType)null;</font>

     

    Oh. Well, yeah, that's pretty dumb. Someone doesn't really understand the concept of compile-time types and runtime types.

     


  • Considered Harmful

    @Someone You Know said:

    @C-Octothorpe said:

    and my personal favourite was casting null to a specific type:

    <font face="Consolas" size="2"><font face="Consolas" size="2">
    </font></font>

    <font face="Consolas" size="2"><font face="Consolas" size="2">myType = (</font></font><font color="#2b91af" face="Consolas" size="2"><font color="#2b91af" face="Consolas" size="2"><font color="#2b91af" face="Consolas" size="2">MyType</font></font></font><font face="Consolas" size="2"><font face="Consolas" size="2">)</font></font><font color="#0000ff" face="Consolas" size="2"><font color="#0000ff" face="Consolas" size="2"><font color="#0000ff" face="Consolas" size="2">null</font></font></font><font face="Consolas" size="2"><font face="Consolas" size="2">;</font></font>

     

    If you have two methods with the same name, each of which takes a parameter of a nullable type, you might actually need to do this.

     

    myType = default(MyType);



  • @joe.edwards said:

    @Someone You Know said:

    @C-Octothorpe said:

    and my personal favourite was casting null to a specific type:

    <FONT size=2 face=Consolas><FONT size=2 face=Consolas></FONT></FONT>

    <FONT size=2 face=Consolas><FONT size=2 face=Consolas>myType = (</FONT></FONT><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas><FONT color=#2b91af size=2 face=Consolas>MyType</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>)</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>null</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;</FONT></FONT>

     

    If you have two methods with the same name, each of which takes a parameter of a nullable type, you might actually need to do this.

    myType = default(MyType);

    Not to be combative, but what are you trying to say?  I think what he meant was this:

    <FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>

    private</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>void</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> foo(</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>int</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>? var1) { }

    </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>

    private</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>void</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> foo(</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>bool</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>? var1) { }

    </FONT></FONT>

    Now try and call foo with a null value as a parameter.


  • Considered Harmful

    foo( default(int?) );

    foo( default(bool?) );

    Well, that was easy.



  • @joe.edwards said:

    foo( default(int?) );

    foo( default(bool?) );

    Well, that was easy.

    Ok, so you'd use the default keyword instead of null (which I think affects readability negatively)...  I don't think that adds anything to the conversation other than telling us your preference in coding style.  Or am I just missing something profound?

  • Considered Harmful

    From my perspective:


    Person A: My coworker casts null to specific types! That's silly.

    Person B: Sometimes a cast is necessary because it's the only way to call an overloaded function with a null parameter.

    Me: You guys are overlooking a built-in operator which allows you to have strongly-typed null values without casting.


    Maybe it comes from a C++ background, but casting has a bit of a design smell to me, and I equate default to a literal value. I do realize the compiler will produce the same thing, but semantically using the literal value is cleaner than typecasting (to my mind).



  •  Not that I'm the most reasonable person in the world, but when I read something like

     @C-Octothorpe said:

    <font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">private</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> </font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">void</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> foo(</font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">int</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">? var1) { }</font></font>

    <font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"> </font></font></font>

    <font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">private</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> </font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">void</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> foo(</font></font><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas"><font size="2" color="#0000ff" face="Consolas">bool</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">? var1) { }</font></font>

     

    It makes it look like it's not sure of what type the arguments must be, or that maybe it takes an int or bool....  Why on earth would you pick syntax like that?

    Ok, so I read the documentation (TRWTF?) and it just means that it's a nullable version of an int... which means that it's not really an int, but a structure that contains an int and a flag indicating if the value should be used or not.  I suppose that's a useful syntax/semanic shorthand, but I fear that in hiding what's really going on it will leave all kinds of odd unintended consequences.


  • Considered Harmful

    There are lots of semantic sugar and black magic which make the Nullable class play well with null values. As far as leaking abstractions, I use it every day and it's never bitten me.

    As far as discoverability goes, I'd bet the Nullable class gets a lot more widespread use because you don't have to learn a new namespace or class name, just tack on a character. It also saves quite a bit of typing.

    For a while there was talk of adding a ! suffix which would make classes non-nullable, to complement the ? suffix that makes structs nullable. I was in favor, but it never took off.



  • @Lorne Kates said:

     
    Even worse is VB.Net, which doesn't short-circuit its lame-ass ternary function:
    @this will crash and fuck you up said:

    value = IIf(user_value = 0, 0, 42 / user_value)

     value = If(vb_has_real_ternary, "life_is_good", "puppies_die")

     You may have just been complaining about the original IIf function, but if not enjoy: http://msdn.microsoft.com/en-us/library/bb513985.aspx



  • @Lorne Kates said:

    Even worse is VB.Net, which doesn't short-circuit its lame-ass ternary function:
    @this will crash and fuck you up said:

    value = IIf(user_value = 0, 0, 42 / user_value)

    Even worse is when it doesn't crash, and unexpectedly evaluates the second value.
    @this will just fuck you up said:

    Partial Class test_stupid
        Inherits System.Web.UI.Page
        Public SomeValue As Integer = 5
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
                Dim value As Integer = 5
                value = IIf(value = SomeValue, SomeValue, IncreaseByOne())
                ' Both are 5, so both should end up as 5, right? Right?        
                Response.Write("Value is " & value & " and some SomeValue is " & SomeValue)
                ' 5 and 6. OH SHIZNIT!
        End Sub
     
        Protected Function IncreaseByOne() As Integer
            SomeValue = SomeValue + 1
            Return SomeValue
        End Function
    End Class



    SIGH… In VB.NET (and VB for that matter), IIf is a FUNCTION. Therefore, if you put an expression into it, the expression will be evaluated BEFORE the Iif (function) is executed. Which explains all of the behaviour you cited. C# (and Excel for that matter) don't do this; VB and its progeny do.



  • @gribunin said:

    I was given a task to incorporate some code written by a colleague to a web application I am working with. I got his code and among the other WTFs I found the following:

            if ((int)DateTime.Now.AddDays(additionaldays).DayOfWeek == 7)
            {
                ....
            }
    

    For those who doesn't know DayOfWeek is an enumeration type, which is defined as:

          public enum DayOfWeek {  Sunday = 0,  Monday = 1,  Tuesday = 2,  Wednesday = 3,  Thursday = 4,  Friday = 5,  Saturday = 6 }
    

    so it can't be equal 7. The way of comparing by converting enum type to integer and then comparing it with a number is WTF itself.

    But it's alone was not so interesting so that I would post it here. But the following chat with the author made me speechless. I told him that this line of code will not ever execute, and received the following reply:

       -- normaly I would agree but I was seeing 7 during testing. 
    

    So I sent them the same enum definition I post here and got the following:

       -- I did't think so either until  I saw it.
    

     Absolutely possible to get a 7 (or any other possible integer. Enums are *NOT* guaranteed to be valid values!   One should always (ideally as a CodeContract) verify with Enum.IsDefined  [I encapsulatre to make it easyt to work with any enum type] otherwise strange things may occur.

    See:  http://blogs.msdn.com/b/brada/archive/2003/11/29/50903.aspx 

    (While I agree with the above arcticle about eliminating causes, the reality is that for public contracts, you cna only minimize the damage, and not prevent the root causes)


  • ♿ (Parody)

    @TheCPUWizard said:

    Absolutely possible to get a 7 (or any other possible integer. Enums are NOT guaranteed to be valid values!

    Wow, that seems crazier than Java generics type erasure. Likely causes fewer problems, but a lot harder to figure out, because most people will assume that an enum is actually type safe.



  • @TheCPUWizard said:

    Absolutely possible to get a 7 (or any other possible integer.

    I know that intentionally it's possible to put an invalid value to the Enum typed value. But do you seriosly claim that DayOfWeek property of DateTime structure (which is part of .NET) will ever return a value, which is not any day of week? And that it so possible, that a programmer would write a special "if" statement to deal with that?



  • @joe.edwards said:

    From my perspective:


    Person A: My coworker casts null to specific types! That's silly.

    Person B: Sometimes a cast is necessary because it's the only way to call an overloaded function with a null parameter.

    Me: You guys are overlooking a built-in operator which allows you to have strongly-typed null values without casting.


    Maybe it comes from a C++ background, but casting has a bit of a design smell to me, and I equate default to a literal value. I do realize the compiler will produce the same thing, but semantically using the literal value is cleaner than typecasting (to my mind).

    Ah, I see, my bad.  Personally, I would prefer the casting a null because when you're scanning the code

    something = (type)null

    seems more verbose and obvious than

    something = default(sometype).


  • BINNED

    @C-Octothorpe said:

    Also littered throughout the code were methods which would return a bool from a DB call, only to convert that bool to an integer:

    <font size="2" face="Consolas"><font size="2" face="Consolas"></font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">bool</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> returnValue = cmd.DbCall();</font></font><font size="2" face="Consolas"><font size="2" face="Consolas">
    </font></font>

    <font size="2" face="Consolas"><font size="2" face="Consolas"></font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">if</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> (returnValue == </font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">true</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas">)</font></font>

    <font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">  return</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> 1;</font></font><font size="2" face="Consolas"><font size="2" face="Consolas">
    </font></font>

    <font size="2" face="Consolas"><font size="2" face="Consolas"></font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">else</font></font></font>

    <font size="2" face="Consolas"><font size="2" face="Consolas">
    </font></font>

    <font size="2" face="Consolas"><font size="2" face="Consolas"></font></font><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas"><font color="#0000ff" size="2" face="Consolas">  return</font></font></font><font size="2" face="Consolas"><font size="2" face="Consolas"> 0;</font></font>

    Naturally, the calling code would then do checks like if (value == 1), etc...

    Do the redundant comparisons to true bother anyone else, or is it just me?

  • Considered Harmful

    Always make extra sure.

    if( ( ( ( ( ( ( thisString.ToString().ToString().ToString().ToString().Equals( thatString.ToString().ToString().ToString().ToString() ) ) == true ) == true ) == true ) == true ) == true ) != false ) {



  • @PedanticCurmudgeon said:

    Do the redundant comparisons to true bother anyone else, or is it just me?
     

    This is usually a sign that the person doesn't understand the concept of expressions returning a value. It seems common among people who have "learned" programming in a class, or from a book, but haven't actually coded anything useful.



  • @Someone You Know said:

    @PedanticCurmudgeon said:

    Do the redundant comparisons to true bother anyone else, or is it just me?
     

    This is usually a sign that the person doesn't understand the concept of expressions returning a value. It seems common among people who have "learned" programming in a class, or from a book, but haven't actually coded anything useful.

    Or that they coded a lot of C/C++


  • @Someone You Know said:

    This is usually a sign that the person doesn't understand the concept of expressions returning a value

    Indeed. Incidentally, I was trying to explain this the other day at work, when I was abusing our coding standards by writing things like (the following is in straight C)

    if( (a = func()) > lower_limit)

    And the reviewer wasn't sure if this would always execute or not.

    The way I explained it was giving the example that an assignment must return a value, since the construct

    a = b = c = ... = 0;

    does what you would expect



  • @Lorne Kates said:

    Try this:@this will will not compile said:

    OleDbParameter orv = new OleDbParameter("wtf", OleDbType.Double);
    orv.Value = user_value < 0 ? DBNull.Value : user_value;
    Yes, but... DBNull.Value is not exactly the same thing as null, ya know?

     



  • @boomzilla said:

    @TheCPUWizard said:
    Absolutely possible to get a 7 (or any other possible integer. Enums are NOT guaranteed to be valid values!

    Wow, that seems crazier than Java generics type erasure. Likely causes fewer problems, but a lot harder to figure out, because most people will assume that an enum is actually type safe.

    It's for compatibility with C/C++ where enums are merely glorified constants, and also allows you to use the [Flags] attribute to OR enum values together. However, it is at least well-documented behaviour - see the "Note" here. Personally I think this was a poor design decision by the .NET team, but it is what it is.



  • @Sutherlands said:

    Or that they coded a lot of C/C++
     

    Why? I think I've seen this kind of thing quite often in C/C++



  • @dargor17 said:

    @Sutherlands said:

    Or that they coded a lot of C/C++
     

    Why? I think I've seen this kind of thing quite often in C/C++

    The way I can usually tell if someone came from a C/C++ background was if I saw code like this:

    <FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>

    bool</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> val = </FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>false</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas>;</FONT></FONT>

    <FONT size=2 face=Consolas><FONT size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>

    if</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> (</FONT></FONT><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas><FONT color=#0000ff size=2 face=Consolas>true</FONT></FONT></FONT><FONT size=2 face=Consolas><FONT size=2 face=Consolas> == val)

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


  • @dargor17 said:

    @Sutherlands said:

    Or that they coded a lot of C/C++
     

    Why? I think I've seen this kind of thing quite often in C/C++

    Um... yes... if you had some sort of reading comprehension skill, you would understand that was EXACTLY my point.


  • @too_many_usernames said:

    The way I explained it was giving the example that an assignment must return a value, since the construct

    a = b = c = ... = 0;

    does what you would expect

     

    Not if you've been working in PL/I, where a = b = c; means compare b to c, and if they're equal, set a to a (boolean) value of "true" ('1'b in PL/I), otherwise set a to false ('0'b).

     



  • @da Doctah said:

    @too_many_usernames said:

    The way I explained it was giving the example that an assignment must return a value, since the construct

    a = b = c = ... = 0;

    does what you would expect

     

    Not if you've been working in PL/I, where a = b = c; means compare b to c, and if they're equal, set a to a (boolean) value of "true" ('1'b in PL/I), otherwise set a to false ('0'b).

     Not to mention an industrial process control language popular in the early 1980's [forget the exact name, but it was along the line of MachTran (MachineTranslation with the alliteration of "Mach" speed)....

     There it meant...

       a = b  then  b = c  then c = 0

    i.e. each of the values got shifted left in the sequence of variables.



  • too_many_usernames: Please refrain from using that example in the future.  Even though you mentioned that the language you were using and referring to was C, there are other languages where that's not how the assignment operator works.  Therefore, you cannot use it as an example in C to show how functions return values.



  • I'm afraid I wasn't clear. I admit that "this kind of thing" is vague, after all.

    What I meant was that the use of expressions returning a value seems common in C++ to me, and I thought your "Or that they coded a lot of C/C++" referred to "This is usually a sign that the person doesn't understand the concept of expressions returning a value. It seems common among people who have "learned" programming in a class, or from a book, but haven't actually coded anything useful."

    That is, I thought you meant that people who code a lot in C/C++ do not understand a concept that seemed quite common to me, and asked why. You know, I assumed your post was about the post it quoted, and not about the previous one.



  • @dargor17 said:

    You know, I assumed your post was about the post it quoted, and not about the previous one.

    It was.  Read it as a continuation of the thought.

    @Sutherlands said:

    @Someone You Know said:

    @PedanticCurmudgeon said:

    Do the redundant comparisons to true bother anyone else, or is it just me?
     

    This is usually a sign that the person doesn't understand the concept of expressions returning a value ...

    ... Or that they coded a lot of C/C++

    If you want to put something in your signature or location to signify that you're an ESL, I'll make sure to explain things instead of being harsher than I need to be.


Log in to reply