- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在一个函数中有这个 if/else 循环,我只是想知道这是否是一种“可接受的”做事方式。它做了我想要它做的事情,但我认为必须有一个更优雅的解决方案。
var signUpCheckAll = function(username, password, passwordconf, email){
if (emptyChecker(username, password)) {
if (emptyChecker(passwordconf, email)) {
if (lengthChecker(password)) {
if (passwordCheck(password, passwordconf)) {
return true;
}
else{
console.log("Passwords don't match!");
}
}
else{
console.log("Password isn't long enough!");
}
}
else{
console.log("Some fields are empty!");
}
}
}
谢谢
最佳答案
我个人(可能还有许多其他人)认为这更具可读性:
if (!emptyChecker(username, password)) {
console.log("Some fields are empty!");
}
else if (!emptyChecker(passwordconf, email)) {
//Where’s the message?
}
else if (!lengthChecker(password)) {
console.log("Password isn't long enough!");
}
else if (!passwordCheck(password, passwordconf)) {
console.log("Passwords don't match!");
}
else {
return true;
}
我还建议重命名您的函数。目前尚不清楚函数名称 passwordCheck
的用途。函数名称应始终包含表示函数操作或返回的动词。 passwordsMatch
更好。 (然后您可以将该行读作“else if(not)passwords(don't)match”。)
关于javascript - 这种 if/else 编程习惯不好吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32537458/
我编写的代码主要供个人使用,但我正在考虑发布我最初开发供个人使用的应用程序(科学模拟/可视化)。 我的一个习惯是在类中使用一个main方法来单独测试类的运行情况。我认为这在某种程度上可能是不好的(毫无
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: How do I convince programmers in my team to do TDD? 在从
假设我想测试是否有一个名为“Z”的驱动器。第一步是这样的; Get-PSProvider | Select-Object -Property Drives 这个给我; Drives: ... {C,
这是对 an old answer to a question about the necessity of functools.partial 的一种跟进: 虽然这个答案非常清楚地解释了这种现象及其
Perl 习惯很难改掉。两种语言之间的变量声明、作用域、全局/局部是不同的。是否有一组推荐的 python 语言习语可以使从 perl 编码到 python 编码的过渡不那么痛苦。 细微的变量拼写错误
我是一名优秀的程序员,十分优秀!