NSAssert vs. assert:您使用哪个?何时使用? [英] NSAssert vs. assert: Which do you use, and when?

查看:106
本文介绍了NSAssert vs. assert:您使用哪个?何时使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我阅读了两个非常有趣的建议:

I've read two really interesting pieces of advice, recently:

  1. 在对此StackOverflow答案的评论中,@迈克·韦勒(Mike Weller)说,在生产代码中放下您的主张……对性能的影响到底是什么?有什么理由不把他们留在里面吗?
  2. 文森特·盖布尔的博客中,他指出您应该选择assert而不是NSAssert ...是否有任何理由不使用assert? (少于字母:))
  1. In the comments to this StackOverflow answer, @Mike Weller says to leave your asserts on in production code... what's the performance hit, really? Is there any reason NOT to leave them in?
  2. In Vincent Gable's blog, he states that you should prefer assert over NSAssert... is there any reason NOT to use assert? (it's less letters :))

推荐答案

要回答两个问题:

  1. 除非断言中的实际操作很耗时(例如,assert([obj calculateMeaningOfLife] == 42)),否则断言对性能的影响应该很小.在性能方面,一个断言应该与一个额外的if语句没有什么不同.在发行版本中剔除断言的原因是它们本质上是一种调试工具-它们在运行时捕获不一致的内部程序状态.从开发人员的角度来看,一旦出现问题,使应用程序崩溃会更好,但是从用户的角度来看,如果应用程序不崩溃,则烦恼就可以减少(除非让应用程序以异常状态运行会导致可怕的事情发生) ),并且在错误消息中公开开发细节可能会令人反感.双方都有很好的论据-如果我没记错的话, Code Complete 建议将其删除,但 The Pragmatic Programmer 建议将其保留.在任何情况下,断言都不是替代适当的错误处理,并且仅应用于编程错误.

  1. There should be very little performance hit for leaving in an assertion, unless the actual action in the assertion is time consuming (e.g. assert([obj calculateMeaningOfLife] == 42)). An assertion should be no different than an extra if statement, performance-wise. The reason for stripping out assertions in release builds is that they are essentially a debugging tool--they catch inconsistent internal program state at runtime. From a developer perspective, it's much better for an app to crash as soon as something goes wrong, but from a user perspective it's arguably less annoying if the app doesn't crash (unless letting the app run with abnormal state causes something horrible to happen), and exposing development details in error messages can be off-putting. There are good arguments on both sides--if I remember correctly, Code Complete recommends stripping them out but The Pragmatic Programmer recommends leaving them in. In any case, assertions are not a substitute for proper error handling and should only be used for programming errors.

NSAssert和常规assert之间的基本区别在于,当NSAssert失败而assert只是使应用程序崩溃时,NSAssert会引发异常. NSAssert还可以让您提供更详细的错误消息并进行记录.实际上,我真的认为两者之间没有太大区别-我想不出处理断言引发的异常的理由. (要弄乱头发,我认为NSAssert通常涉及较少的键入操作,因为您不必包括assert.h,但这既不在这里也不在那里.)

The basic difference between an NSAssert and a regular assert is that an NSAssert raises an exception when it fails while an assert just crashes the app. NSAssert also lets you supply fancier error messages and logs them. Practically, I really don't think there's much difference between the two--I can't think of a reason to handle an exception thrown by an assertion. (To split hairs, I think NSAssert usually involves less typing because you don't have to include assert.h, but that's neither here nor there.)

这篇关于NSAssert vs. assert:您使用哪个?何时使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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