3 ms·
In this example, the main point is to have the type system do work for you. Some languages have bad type systems and I agree in those languages it is often more
by smadge 9y ago
In this example, the main point is to have the type system do work for you. Some languages have bad type systems and I agree in those languages it is often more work than it's worth to write type safe code. However, in general, I would much rather have an EmailAddress type and if I am passed an EmailAddress I can be confident it is actually an email address.
For example I could have the only way to construct an email address be (in Haskell)
makeEmailAdress : String -> Maybe EmailAddress
With the validation logic in the constructor. This is no more complicated than your solution.
As an added benefit my type signatures are now also doubling as documentation.
- crimsonalucard 9y agoI know Haskell. We don't even need to address replacing exceptions with monads to show how and why OOP and patterns are in many many cases a bad solution.
- smadge 9y agoThe point has nothing to do with functional vs oop, or exceptions versus monads. It has to do with using the features of your language of choices type system to prevent bugs. The thing going on here is putting validation logic in your type constructors so it's impossible to construct an invalid type without triggering a runtime "exception" or error.
- crimsonalucard 9y agoOk I get your point. You can still have a function take in an address object rather than inserting into another object with a constructor. You are correct in that building types leads to a more safe program but I was more addressing the flaw with objects rather then types.