疑惑 关于 多条件判断语句 的写法
写法1
if ( 条件1 && 条件2)
{
//代码
}
写法2
if ( 条件1 )
{
if( 条件2 )
{
//代码
}
}
我一直认为写法1要比写法2条理清晰,容易阅读.
不知其他人如何认为呢?
今天在微软的KB中阅读 使用 Visual Basic .NET 在采用基于表单身份验证的 ASP.NET 应用程序中实现基于角色的安全性 的时候
发现这样的写法
public void Application_AuthenticateRequest( Object src , EventArgs e )
{
if (!(HttpContext.Current.User == null))
{
if (HttpContext.Current.User.Identity.AuthenticationType == "Forms" )
{
System.Web.Security.FormsIdentity id;
id = (System.Web.Security.FormsIdentity)HttpContext.Current.User.Identity;
String[] myRoles = new String[2];
myRoles[0] = "Manager";
myRoles[1] = "Admin";
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id,myRoles);
}
}
}
{
if (!(HttpContext.Current.User == null))
{
if (HttpContext.Current.User.Identity.AuthenticationType == "Forms" )
{
System.Web.Security.FormsIdentity id;
id = (System.Web.Security.FormsIdentity)HttpContext.Current.User.Identity;
String[] myRoles = new String[2];
myRoles[0] = "Manager";
myRoles[1] = "Admin";
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(id,myRoles);
}
}
}
而且这则KB的VB版本也是这样的写的.
这样写有优越性?