你使用断言吗? [英] Do you use assertions?

查看:214
本文介绍了你使用断言吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这不是一个问题所以我正在制作CW。

This is not really a "question" so I'm making it CW.

assert

关键字很棒!

应该让你觉得自己对你编写的代码更有信心,但是直到今天我创建一个小型测试类(< 20行)时才意识到从来没有使用过它。

It should make, feel your self more confident with the code you wrote, but, until today when I was creating a small test class ( < 20 lines ) I realize a never use it since it was introduced.

哎呀!我几乎没有使用记录器,这确实非常有用,但直到今天我才意识到我不使用断言。

Heck! I barely use logger which is very useful indeed, but it wasn't until today I realize I don't use assertions.

你使用断言吗?如果不是,那是什么原因?

Do you use assertions? If no, what's the reason?

推荐答案

我被教导在90年代使用了很多断言,他们很有道理。这是一个很好的防御性编码。

I was taught to use lots of assertions back in the 90s and they made great sense. It's good defensive coding.

但是,我认为现在已经被单元测试所取代,这实际上迫使代码运行并告诉它在哪里被破坏。调试断言需要有人实际运行调试代码并查看ui或日志。单元测试可以自动执行此操作。

However, I think this has now been superceded by unit testing that actually forces the code to run and tells where it is broken. Debug asserts required someone to actually be running debug code and looking at the ui or logs. Unit testing can automate this.

这篇关于你使用断言吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆