News for hackers. Stuff that matters.
Re:Strong Types and Their Impact on Testing by TeeWEE on Tuesday November 18, 2014 @12:50PM (#8625144)
Imho types should make the programmer live easier, not more difficult. Looking at the first code, and at the second peace of code. Imho the second piece of code is easier to read, but it does lack validation logic & testing.

First of all, from a type-theory point of view, both Email and WebForm have the same type. (same signature).

Also from a theoretical point of view, i think a type should not have an instance if its not valid and "EmailAddress" of value "aasda" should not exists. However it is allowed in his types.

Also, I don't see how defining lots of very specific types such as FromAddress and ToAddress are really making live easier.

Something like this does the job, and is typesafe (in golang):

  type EmailAddress string

  type Email struct {
    to EmailAddress
    from EmailAddress
    subject string
    body string
  }

  func sendEmail(webForm string) err = {
    email, err := validateEmailForm(form) # performs validation
    if err {
      return err
    }
    err := email_service.sendEmail(email)
    if err {
      return err
    }
  }

Tests will simply use the returnValue of sendMail to validate its inner working.
Re:Strong Types and Their Impact on Testing by dllthomas on Tuesday November 18, 2014 @01:31PM (#8625396)
Having a separate FromAddress and ToAddress serves a somewhat similar purpose to labeled arguments. You can't mix them up, and you are informed if they change.
Re:Strong Types and Their Impact on Testing by xwowsersx on Tuesday November 18, 2014 @12:54PM (#8625167)
Good point. Having a separate FromAddress and ToAddress was probably overkill. You're right about the WebForm and Email having the same type. The form submission could probably just produce an Email directly.

I don't know much about go. What is "err"?

Re:Strong Types and Their Impact on Testing by icebraining on Tuesday November 18, 2014 @12:58PM (#8625186)
Go follows the C tradition of returning errors instead of using exceptions. 'err' is a variable of type "error"[1], which should either be nil or have an error value.

[1] http://golang.org/pkg/builtin/#error

Re:Strong Types and Their Impact on Testing by xwowsersx on Tuesday November 18, 2014 @01:02PM (#8625213)
so the return type of this little program is "error" and nil if no error?
Re:Strong Types and Their Impact on Testing by icebraining on Tuesday November 18, 2014 @01:11PM (#8625283)
Yes. It's either a value that implements the 'error' interface or nil.
Re:Strong Types and Their Impact on Testing by badbath on Tuesday November 18, 2014 @01:17PM (#8625318)
That's nuts. All of your code now has to check if err != nil, etc.

Furthermore, the sendEmail and validateEmailForm values are essentially ignored and used only for their possible error values.

Why should I wrote my program as if the thing I'm concerned primarily with is errors? Much better to encode the failure into the type. You could have, for example, some function that returns Either[Error, SendEmailAction] where SendEmailAction is a function that, when called, will perform the effect of sending an email. Now I can deal with the happy path by mapping on the right-biased Either.

Re:Strong Types and Their Impact on Testing by icebraining on Tuesday November 18, 2014 @01:39PM (#8625462)
This has been discussed in pretty much every golang thread here on HN :)
Re:Strong Types and Their Impact on Testing by TeeWEE on Thursday November 20, 2014 @12:45PM (#8636986)
When I started go, i also thought it was nuts. And it indeed clutters your code a lot. But in the end, i like it.

Golang is a really simple language, but all the simple concepts, and the lack of OOP, forces you to think more about your code. Also you are forced to think about error conditions.

I'm still playing around with it. So i dont have a final opinion on go.

Re:Strong Types and Their Impact on Testing by ecopoesis on Tuesday November 18, 2014 @01:39PM (#8625461)
Not if you use Scala. With Scala, you'd just map or for comprehension over the result.
Re:Strong Types and Their Impact on Testing by masklinn on Tuesday November 18, 2014 @01:58PM (#8625611)
That's what he covers in his third paragraph...
Re:Strong Types and Their Impact on Testing by codygman on Tuesday November 18, 2014 @04:30PM (#8626477)
Well, to be fair it's if you use any language that uses some equivalent of the Maybe Monad.