是否应该在生产iOS应用程序中留下断言? [英] Should one leave asserts in production iOS apps?

查看:122
本文介绍了是否应该在生产iOS应用程序中留下断言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常的做法可能是在应用程序开发期间将断言放入代码中以检查输入参数,数据完整性等。

Common practice might be to put asserts in code to check input parameters, data integrity, and such, during app development.

我测试我的应用程序,但是,鉴于我不是Knuth(并且写了1美元的支票),我付不起如同一些医疗和太空系统软件公司一样,雇用一支庞大的全职QA团队,我认为所有应用程序总是会有很多在测试或质量保证期间从未见过的错误。假设否则看起来非常不智。因此,在测试应用程序(并显然删除导致任何先前看到的ASSERT失败的所有错误)并准备好将应用程序发送给Apple之后,应该对发布/分发构建中的所有ASSERT检查做些什么?离开还是不做?

I test my apps, BUT, given that I'm not Knuth (and he writes $1 checks), and I can't afford to employ a large team of full-time QA people as do some medical and space systems software companies, I assume that all apps will always have plenty of bugs that have never yet been seen during testing or QA. Assuming otherwise seems quite intellectually dishonest. So after testing an app (and obviously removing all bugs causing any previously seen ASSERT failures) and getting the app ready to ship to Apple, what should be done with all the ASSERT checks in the Release/Distribution build? Leave or no-op?

以下是让他们离开的一个理由:如果某个应用对某些用户行事不安,该应用可能会被这些用户评为1星没有人告诉开发人员为什么足够详细。但是如果应用程序因ASSERT失败而崩溃,那么该应用程序可能仍然会被评为1星级,但如果有足够多的用户选择进入,开发人员可能通过iTunes和iTunes Connect间接获得一些故障转储,以找出问题所在。如果苹果公司因全新的ASSERT崩溃而被Apple拒绝,这将阻止应用程序的恶劣版本进入客户的设备。

Here's one rationale for leaving them in: If an app acts wonky for some users, the app might get rated by those users as 1-Star without anyone ever telling the developer why in sufficient detail. But if the app crashes from an ASSERT failure, the app might still get rated 1-Star, but the developer could potentially get some crash dumps, indirectly via iTunes and iTunes Connect if enough users opts in, to figure out what is going wrong. And if the app gets rejected by Apple due to a brand new ASSERT crash, that will prevent a bad version of the app from ever getting onto one's customer's devices.

推荐答案

完全按照您指定的原因保留它们,但也因为在某些情况下它们充当注释(特别是在Objective-C中涉及类型的情况)。并且不要担心性能损失,除非它成为一个问题,或者你知道你处于性能危急情况,并且特定的断言将在主运行循环中运行数百或数千次。

Leave them in for exactly the reasons you specify, but also because in certain cases they act as comments (especially where types are concerned in Objective-C). And do not worry about the performance hit unless it becomes a problem or you know you're in a performance critical situation and a particular assert is going to be run hundreds or thousands of times on the main run-loop.

不能不提这个有关断言与NSAssert的文章。

就个人而言,我开始删除我为调试目的而放入的那些,但如果你使用断言来检查数据完整性,参数,资源依赖性和其他相关的事情 - 可以说,你可以自己抛出异常,这可能更明智 - 然后我会留下它们。

Personally, I start to remove the ones that I've put in for debugging purposes, but if you use asserts to check data integrity, parameters, resource dependencies and other related things -- arguably, you could throw Exceptions yourself instead, which might be wiser -- then I would leave them in.

注意:另一点是,只删除断言是完全愚蠢的,因为你的应用程序会崩溃或处于不一致的状态,这两种情况都比你可以识别的方式崩溃更糟糕崩溃日志(所以留下断言在)。另一方面,如果语句用替换断言可能是一件好事。

Note: A further point is that just removing asserts is utterly stupid, since your app will either crash or be in an inconsistent state, both of which are worse than crashing in a way that you can recognize from the crash logs (so leave the asserts in). Replace asserts with if statements, on the other hand, could be a good thing.

这篇关于是否应该在生产iOS应用程序中留下断言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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