Belt and Braces编程是好的做法还是只是引入了不必要的复杂性? [英] Is Belt and Braces programming good practice or just introducing needless complexity?

查看:124
本文介绍了Belt and Braces编程是好的做法还是只是引入了不必要的复杂性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否使用皮带和牙套(吊带)方法是否进行编程(尤其是进行数据验证)是否是一个好习惯.这来自以下示例.

I was wondering whether using a Belt and Braces (Suspenders) approach to programming - and to data validation in particular - was good practice or not. This came about from the following example.

我正在创建一个表单,并在所有字段中添加了侦听器,这意味着仅当表单中的所有字段都具有有效值时,才启用OK按钮.然后,我正在编写单击OK按钮时运行的代码.

I was creating a form and I added Listeners to all the fields which should mean that the OK button is only enabled if all the fields in the form have valid values. I was then writing the code which was run when the OK button is clicked.

我悲观的一面认为,Belt and Braces永远不会伤害任何人,并且如果我的表单逻辑中存在错误,再次验证表单也不会受到伤害.

The pessimistic side of me decided that Belt and Braces never hurt anyone and it couldn't hurt to validate the form again in case there's a bug in my form logic.

但是,如果验证失败,我不知道要输入什么.如果我做这样的事情:

But then I didn't know what to put in if the validation fails. If I do something like this:

if (! form.isValid()) {
  displayErrorMessage();
}

然后,我必须创建代码以显示错误消息,该错误消息永远不会显示.任何将来维护此代码的人都将为此担心,并可能在理论上不必要的对话中对此感到困惑.我想要的最后一件事是有人想知道为什么从未显示此特定对话框.

then I have to create code to display an error message which should never be shown. Anyone maintaining this code in future is then going to worry about and possibly be confused by this in theory needless dialog. The last thing I want is someone wondering why this particular dialog is never displayed.

比例尺另一端的选项是:

An option at the other end of the scale is:

if (! form.isValid()) {
  throw new RuntimeException("This should never happen!");
}

坦白说,即使键入该命令,我也感到很脏,但也许有充分的理由使用我错过的它.

Frankly, I feel dirty even typing that but perhaps there's a good reason to use it which I have missed.

所以最后我得到了:

assert form.isValid();

但是,这样做的缺点是它并不是真正的皮带和花括号,因为花括号在运行时不存在,因此,如果代码中有错误,我的表单裤子仍然会像往常一样掉下来.

However, the downside of that is that it's not really belt and braces since the braces aren't there at run-time so if there is a bug in the code my form's trousers are still going to fall down as it were.

所以也许我根本不应该进行额外的验证,但是我中仍有一部分人认为这样做不会造成伤害.

So maybe I shouldn't have the extra validation at all, but there's still a part of me which thinks it couldn't hurt.

我很想听听你在类似情况下的所作所为.

I'd be interested to hear what you do in similar situations.

(编辑:问题是询问确保表单返回有效数据的最佳方法是什么.假设表单的输出在最终存入数据库之前已被再次验证,依此类推)

(Edit: the question is asking what is the best way to ensure the form returns valid data. Assume that the output of the form is validated again before it ends up in the database and so on.)

推荐答案

我没有做太多的UI工作,但是最近我发现自己做的事情非常相似. 我留在皮带上(验证每个控件的变化情况)和大括号(在OK_Click上再次检查是否有效).

I don't do much UI work, but recently I found myself doing something very similar. I left in both the belt (validate each control as it changes) and the braces (check is valid again on OK_Click).

我把两者都留在了基础上,如果将来的某些更改错过了控件上的验证,则单击确定"按钮时将捕获它.

I left both in on the basis that if some future change missed validation on the control, it would be caught when the OK button is clicked.

在我脑海中,OK的检查是真实验证,而每个控件的验证则是糖,它可以增强用户体验.

In my head the check on OK is the real validation, and the per control validation is sugar that just enhances the users experience.

那表示我没有考虑太多,而且我也不经常做UI工作.

That said I haven't thought about it too much, and I don't often do UI work.

这篇关于Belt and Braces编程是好的做法还是只是引入了不必要的复杂性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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