使用JavaScript进行自信编程 [英] Assertive programming with JavaScript

查看:121
本文介绍了使用JavaScript进行自信编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道为什么断言编程是好的,所以我想用JavaScript来使用它。但是,我不想向用户显示错误框,这是不寻常的事情。只是忽略它,让它们重试可能会更好。

I know why assertive programming is good, so I want to use it with JavaScript. However, I don't want to show users error boxes, and it's unusual thing. Just ignore it and make them retry could be better.

例如,此代码将生成一个错误框并中断用户。

For example this code will make an error box and interrupt users.

function getDomainFromURL(url) {
    assertTrue(url, 'URL should not be null');

    ...parsing
}

所以,我' d就是这样。

So, I'd make like this.

function getDomainFromURL(url) {
    if (!url) return;

    ...parsing
}

第二个是好的对于可用性,我认为,第一个是有利于发展。
因此,IMO最好将这些组合起来并使断言在生产代码上消失。

Second one is good for usability, I think, and first one is good for developing. Therefore, IMO it would be the best to combine these and make the assert disappear on production code.

问题1
您是这么认为的吗?或者有什么不同的想法?

Question 1 Do you think so? Or have any different idea?

问题2
如果您这么认为,有没有什么好方法可以使用Spring3框架?

Question 2 If you think so, is there any good way to do it with Spring3 framework?

推荐答案

我不认为第二个特别好。如果你从一个总是希望返回某个东西的方法中没有返回任何内容,代码可能会在其他地方失败,而不是接近实际问题的位置,即在 getDomainFromURL 内函数。

I don't think the second one is particularly good at all. If you return nothing from a method that is always expected to return something, the code will likely just fail somewhere else, instead of close to where the actual problem is i.e. within the getDomainFromURL function.

如果另一方面,当参数为null时,此方法完全没有返回任何内容,那么添加断言是没有意义的。

If on the other hand, it's perfectly OK for this method to return nothing when the parameter is null, then it doesn't make sense to add the assertion.

就个人而言,我会在开发和生产中随处使用第一种形式。如果断言在生产中失败,那么您将获得有关问题原因的非常有用的反馈(在浏览器,日志等中)。如果你删除断言,你仍然会有一个错误,但是跟踪它会更加困难

Personally, I would use the first form everywhere, in both development and production. If the assertion fails in production, then you'll get very useful feedback (in the browser, logs, etc.) about the cause of your problem. If you remove the assertion, you'll still have a bug, but it'll just be more difficult to track down

这种编程风格 - 检查前置条件 - 是所谓的基于合同的编程的一部分。我之前从未听说过自信编程一词。

This style of programming - checking pre-conditions - is part of what is known as contract-based programming. I've never heard of the term "assertive programming" before.

这篇关于使用JavaScript进行自信编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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