Just because you can use null or undefined doesn’t mean you should. We talk about the problems that come up and how Optionals can help overcome them.

  • dneaves@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    1 year ago

    After some slight frustration with the “Monadic” (Haskell/Elm) approach to null when learning Elm, I actually fully embrace it now:

    unNullString : Maybe String -> String
    unNullString nullable = 
        case nullable of
            Just str ->
                str
            Nothing ->
                ""
    
    • A) Now I try to avoid nullables when possible, because handling it everywhere is often more annoying than just expecting the type (even in non-monadic languages like Python)
    • B) This is theoretically a better practice anyway. Why have a nullable string when you can just have an empty string? Or a nullable array/list/object/dict/etc when you can just have an empty one? Why have a nullable quantity of items when you can just have 0? If you really, really think you need a nullable bool, you don’t: just make a data (Haskell)/type (Elm)/enum (others) at this point. Most other things could just become an data/type/enum as well.

    We null things for no reason, too often. Usually laziness.