News for hackers. Stuff that matters.
Re:Tests aren’t enough: Case study after adding type hints t... by kennywinker on Monday October 18, 2021 @01:46PM (#28908304)
I've seen essentially this code in so many organically grown codebases (when they grew up without types). It's usually close the the UI, because someone had to quickly add an alternate path to support some new user interaction

    function find_user(person) {
        if user is string {
            query_by_name(person)
        } else {
            query_by_name(person.name)
        }
    }
and yeah, we all know it's kinda messy, but also that logic has to live somewhere and we need this feature asap so it passes code review. I wrote a test for it, ship it.
Re:Tests aren’t enough: Case study after adding type hints t... by sseagull on Monday October 18, 2021 @01:56PM (#28908540)
I came very close to writing almost this exact code just the other day (except it was username or user id for me), but came to my senses. It's just so tempting in a dynamic language...

In a static language, you either can't do it, have to really go out of your way to do it, or at least do function overloading (which is a bit cleaner)

Re:Tests aren’t enough: Case study after adding type hints t... by fiddlerwoaroof on Tuesday October 19, 2021 @12:05AM (#28914751)
Calling a function like this “ensureUser” is pretty idiomatic and useful, in lisp-style code bases. I think it’s a pattern related to “parse, don’t validate” in static-type lands: rather than _checking_ what the type is and throwing an error, you define a function that knows how to turn various representations of your type into its canonical shape.
Re:Tests aren’t enough: Case study after adding type hints t... by freemint on Monday October 18, 2021 @01:57PM (#28908557)
Sounds like a brilliant case for multiple-dispatch.
Re:Tests aren’t enough: Case study after adding type hints t... by kennywinker on Monday October 18, 2021 @07:21PM (#28913101)
Right so we have:

    function find_user(person: string)
and also:

    function find_user(person: Object)
how long before someone writes this:

    find_user(person: { name: "dave" })
meanwhile, someone else, not suspecting that they'll be handed a weird half-formed `User` object adds `person.id` somewhere in the body of the Object version of `find_user` and now we have a weird edge-case where very rarely `find_user` panics because the user object we're handed doesn't have an id??? Great, I just lost an hour trying to dig that out of the logs, and the users are starting to think of the product as flakey because the bug has been in prod for over a month before we finally believed them enough to look into it.

Just. Use. Types. Multiple dispatch won't save you on its own. You NEED compile-time types.

Re:Tests aren’t enough: Case study after adding type hints t... by david422 on Tuesday October 19, 2021 @09:24AM (#28917795)
Somebody downvoted you, I'm guessing because they think this is a silly example and have never actually seen something like this. I have, in a production code base.
Re:Tests aren’t enough: Case study after adding type hints t... by freemint on Wednesday October 20, 2021 @06:53AM (#28929196)
Multiple dispatch and compile time times are not exclusive at all.
Re:Tests aren’t enough: Case study after adding type hints t... by kennywinker on Thursday October 21, 2021 @03:32PM (#28949766)
I'm saying the problem isn't solved by multiple dispatch alone, but it is solved by compile time types alone. You can use both together, of course.
Re:Tests aren’t enough: Case study after adding type hints t... by freemint on Friday October 22, 2021 @06:41PM (#28963642)
The issue whether the language is interpreted or compildd (which would distinguish compile time types from strong types) is in my opinion completely orthogonal to the issue of how dispatch works. Strong types and multiple dispatch fix the issues I see even in an interpreted language.