Add hasValue function to Nullable

Issue #263 wontfix
kick martens created an issue

Hi,

Could you add a function hasValue to the Nullable<T> type? It should be the opposite of isNull. It is so much more readable to write: if someVar.hasValue and otherVar.hasValue then doSomething(someVar.value, othervar)

instead of if (not somevar.isnull) and (not othervalue.isnull) then

Comments (9)

  1. kick martens reporter

    I am sorry, you are right. I meant adding an isNull method ;-) I manually added it and mixed up what was the original one. It seems redundant but having both functions makes code more readable instead of using "not"

  2. Stefan Glienke repo owner

    By that argumentation you could duplicate every boolean property ("Hey, you have Enabled, can you please Disabled which is the opposite? And while at it please also add Inactive and Invisible") ;)

    Also by the code you showed I cannot imagine many places where you explicitly check for IsNull than rather for HasValue given that you have functions like GetValueOrDefault where you can provide a value in case the nullable does not contain one.

  3. kick martens reporter

    By that argumentation you could duplicate every boolean property You have point there. Still "isNull" for a nullable seems a logical method to call.

    Also by the code you showed I cannot imagine many places where you explicitly check for IsNull than rather for HasValue I do have a lot of code where the value of properties is replaced by something else when property itself is null. I am currently porting this code from TMS Aurelius to the Spring4D framework. The Nullable<T> of Aurelius does have both 'isNull' and 'hasValue', so I added it to the Spring4D Nullable<T> to make my live easier. I guess I will have to refactor the code to make it only use hasValue.

  4. kick martens reporter

    My object has a property Inkoopkorting (discount), when it is set (has a value) I want to use its value, otherwise I calculate the value using other properties. My code is full of this kind of constructs. I could refactor them, and it would be equally readable to be honest, it's just a lot work ;-)

    function TBasisRegel.GetInkoopkorting: Single; 
    begin 
      if FInkoopkorting.IsNull then 
      begin 
         if (FBrutoInkprijs.ValueOrDefault<>0) and FNettoInkprijs.HasValue then 
            result := (1-(FNettoInkprijs.Value/FBrutoInkprijs.value))*100 
         else 
           result := 0; 
       end else 
         result := FInkoopkorting.value; 
    end;
    

    I could refactor to

    if FInkoopkorting.hasvalue then 
       result := FInkoopkorting.value 
    else 
    begin 
      if (FBrutoInkprijs.ValueOrDefault<>0) and FNettoInkprijs.HasValue then 
        result := (1-(FNettoInkprijs.Value/FBrutoInkprijs.value))*100 
      else 
        result := 0; 
    end
    

    I went through my code and the more complex if statements all use hasvalue. So it would make more sense to have a hasValue if you could have only one. So forget I asked, I will refactor my Aurelius code so I only use hasvalue.

  5. Stefan Glienke repo owner

    It's just reversing the true and false branch of the if statement then.

    FWIW you can also write this:

    if not FInkoopkorting.TryGetValue(Result) then 
    begin 
      if (FBrutoInkprijs.ValueOrDefault<>0) and FNettoInkprijs.HasValue then 
        result := (1-(FNettoInkprijs.Value/FBrutoInkprijs.value))*100 
      else 
        result := 0; 
    end
    

    I will close this as won't fix. I think for migrating your code it might be helpful to add the IsNull property and keep or refactor later. But imo there is no reason to add it to the library.

  6. Log in to comment