We are missing a lot of context but strong type systems give you the superpower to make unwanted state unrepresentable:
Here we can see the class member invalid being used as a flag to express that this method didn’t work out as expected. It is very easy for someone else down the line to ignore/forget (/never even knew about it) to check that invalid flag and just use the result regardless, which will likely lead to more failures later on.
Instead the author should directly return something that indicates invalidness. This could be as simple as returning a boolean or especially if this is a constructor it would be better to throw an error.
That way the invalid class never even gets instantiated and is therefore impossible to misuse. All validated by the type system.
I’d love to know why you say that. I’m learning the language and could use some insights
We are missing a lot of context but strong type systems give you the superpower to make unwanted state unrepresentable:
Here we can see the class member
invalidbeing used as a flag to express that this method didn’t work out as expected. It is very easy for someone else down the line to ignore/forget (/never even knew about it) to check thatinvalidflag and just use the result regardless, which will likely lead to more failures later on.Instead the author should directly return something that indicates invalidness. This could be as simple as returning a boolean or especially if this is a constructor it would be better to throw an error.
That way the invalid class never even gets instantiated and is therefore impossible to misuse. All validated by the type system.