请解释下面的代码片段流程 [英] Please explain me the flow of following snippet
本文介绍了请解释下面的代码片段流程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
if(userid_validation(uid,5,12))
{
if(passid_validation(passid,7,12))
{
if(allLetter(uname))
{
if(alphanumeric(uadd))
{
if(countryselect(ucountry))
{
if(allnumeric(uzip))
{
if(ValidateEmail(uemail))
{
if(validsex(umsex,ufsex))
{
}
}
}
}
}
}
}
}
return false;
}
推荐答案
忽略括号错误,坦率地说......太可怕了。
它正在做的是一组验证,并且(大概)报告验证方法中的验证问题 - 它不是那么它完全没有任何用处。
基本上,如果验证失败,它不会做任何事情更多的 - 有时这是一个好主意,但没有以我喜欢或认为可读的方式实现。
方法应该做一件事:如果要验证,它应该返回OK / Not OK并将报告留到更高级别,因为它可以更具体关于它的报告。我这样做的方式是:
Ignoring that the brackets are wrong, that is frankly...horrible.
What it is doing is a set of validations, and (presumably) reporting a problem with the validation in the validation method - it is isn't then it's doing a grand total on nothing useful at all.
Basically, if it fails a validation, it doesn't do any more of them - which is a good idea sometimes, but not implemented in a way I like or consider readable.
A method should do one thing: if it is to validate, it should return OK / Not OK and leave teh reporting to the higher level as it can be more specific about it's report. The way I would do this is:
if(!userid_validation(uid,5,12))
{
//report problem
return false;
}
if(passid_validation(passid,7,12))
{
//report problem
return false;
}
if(allLetter(uname))
{
//report problem
return false;
}
if(alphanumeric(uadd))
{
//report problem
return false;
}
if(countryselect(ucountry))
{
//report problem
return false;
}
if(allnumeric(uzip))
{
//report problem
return false;
}
if(ValidateEmail(uemail))
{
//report problem
return false;
}
if(validsex(umsex,ufsex))
{
//report problem
return false;
}
return true;
需要更多空间,但它更清晰,验证程序成为通用目的,而不是焊接到现有代码。
It takes more space, but it's clearer, and the validation routines become general purpose instead of "welded" to the existing code.
这篇关于请解释下面的代码片段流程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文