15

I am of the firm belief that a function should always return just one type.

I think it's the most convoluted thing that a function should be allowed to return any kind of type.

I've seen shit like return a string when something is valid and then a boolean if it's not valid.

To me, that kind of flexibility has some funky code smell.

I'm looking at you WordPress 🤨

Comments
  • 7
    Agree. If the language allows this, it’s a clown language. If you think that this is a good idea, you are a clown.
  • 1
    A very good rule of thumb, very much fucked in the ass worldwide; excuse me while I violate every single orifice of reason by representing multiple possible data types using only one structure.
  • 2
    All functions return only one type, the type defined as the return type of the function. "a string or false" may be a valid type in your language.

    Rather, I would phrase this requirement as "all type unions should always be tagged, and it should never be possible to access the value without visually acknowledging the fact that it's in a union, preferably asserting the tag.
  • 7
    The problem is: A function can have error states. You always need a way to communicate the error. Union types are one way, so are exceptions, or the error type of go.

    Not all of those are equal, yet you'd be surprised how many developers hate using exceptions.
  • 4
    If you remove everything that is ugly, you’ll end up with Java, and Java is ugly, so you will end up with nothing
  • 2
    @Tounai quirks and imperfections are sometimes what make something beautiful.

    Without her vague lopsided grin, mona lisa woulda just been an average looking street whore with hobbyist modelling aspirations.
  • 1
    @Wisecrack but Java is more like Michael Jackson and less like Mona Lisa.
  • 0
    Java has pattern matching with custom matchers and immutable context, so you better get off its back right now because not a single popular language has adopted functional patterns as elegantly and effectively as Java.
  • 0
    Their caution in introducing language features has paid off, now C++ is struggling to come up with syntax for pattern matching that doesn't look grotesque and isn't already valid C++, because every wee fad was awarded the most concise imaginable syntax.
  • 1
    @lorentz
    > not a single popular language has adopted functional patterns as elegantly and effectively as Java.

    C#, Swift, Rust and Kotlin comes into mind.
  • 1
    @Lensflare you're right, that was a silly statement. I do still think though that java has gained a lot of really great features recently, and the reason they could do it is that they were abundantly cautious about extending the core language.
Add Comment