Go Static Typing and Testing

sunfmin · 2013年08月28日 · 最后由 bhuztez 回复于 2013年08月29日 · 2980 次阅读

http://thechangelog.com/100/

[44:09] There is a feeling in the community from Python or Ruby world that static typing is bad idea, Because they learn what static typing is from languages that did it badly. Like C, C++, and Java, where it feels like there are some bureaucratic form every time you start writing a program.

I had a bit of epiphany about a year ago when I realize that the last decade testing has become hugely recognized as a critical part of software development. Ironically it was NOT all that big of the deal (there were advocates for it, but didn’t catch on as an important feature) until last decade or so.

And the realization I had was it was triggered by the dynamic language community. using tests to do the type checking for them. They write in this dynamic language, that they write a test to make sure that strings never turn into lists and vice versa. and it’s kind of backwards.

Static checking gives you that at compiles time, You program won’t compile if you do that kind of thing. That means the testing can be functional rather than type checking. That actually makes you write fewer tests and just get the same good coverage.

@sunfmin 其实我还想请教一下, 有没有对 Golang 中错误的处理的一些例子. 在 Golang 中高频率的 error 返回有的时候让我好烦...

#1 楼 @wppurking 这个那个音频里面也有讲到,还有这里: http://golang.org/doc/articles/error_handling.html

@sunfmin 这里有看过. 举个例子, 比如碰到 io 方面的 error 时, 我真的超级想全部写成 bytes, _ := ioutil.ReadAll(xx) 不过还是得写一个

if err != nil {
    return xxx, err
}

因为有时候, 只想把 error 弄到上层去, 可这不是 panic 所以只能一直这样 return 上去.

想请教下你们在编写 Qortex 时, 错误是如何处理的呢? 是否可以举一个例子呀?

#3 楼 @wppurking 我们就是这样 return 上去的。到了前面,就都要处理掉这些错误。

@sunfmin - -|| thanks ... 看来是真跑不掉了......

#4 楼 @sunfmin 返回 error 是为了减少长跳转优化性能, 把出错处理释放资源的棘手问题扔给用户, 更像个 premature optimization, 过几年硬件和 VM 发展了大概就没意义了...

如果你不处理又要继续往上扔, 层层 return 不如直接 panic 啊

一般不会一扔到底吧,如果有一层我就需要处理掉某一类的错误呢?那不是要 panic 后的 recover 里面,再去 panic。。。

而且如果你在设计接口,给别人调用,那基本上就要返回 error 吧,你的接口里面出个 panic,一点不明确啊,返回 error 的话,用你借口的人知道调用这个东西需要处理错误,会去想如果返回错误了,我的系统应该怎么处理。

再比如,调用你的方法,是通过 RPC 调用的,error 就更容易返回给 socket 的另外一端。

#9 楼 @sunfmin 所以要用 Erlang 啊

#10 楼 @bhuztez Erlang 是最棒的,我终于领悟到了。

#9 楼 @sunfmin 这就是 go 没有 typed panic 的缺点, 不能捕捉特定类型的异常. RPC 的话其他语言都会把异常也序列化的...

#12 楼 @luikore Exception 不该抛到一个 object 外面去

需要 登录 后方可回复, 如果你还没有账号请 注册新账号