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

查看:31
本文介绍了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天全站免登陆