snippetcsharpModerate
Is the new version of this code more difficult to read and understand than the original? How can it be improved?
Viewed 0 times
thiscanthenewimprovedreadversionoriginalthanmore
Problem
I was asked to do a code review for the following block of code. It implemented a bug fix to prevent some values being added to some drop down lists depending on the user's domain, in an ASP.NET WebForms app. The three lines adding items to the lists were from the pre-bug-fix code (not part of the fix), so I didn't review it.
I reviewed the code and suggested changing the code to the following:
```
//Only add corporate AD domain if the user is a member of it.
var currentPrincipal = Thread.CurrentPrincipal;
var hasDomain = currentPrincipal != null && currentPrincipal.Identity.Name.Contains("\\");
var domainName = hasDomain ? Utils.GetUserDomain(currentPrincipal) : null;
if (string.Equals(domainName, Config.AppSettings.Domain, StringComparison.OrdinalIgnoreCase))
{
domainUserDropDownList.Items.Add(new ListItem(Config.AppSettings.Domain, Config.AppSettings.Domain));
domainOfficeDropDownList.Items.Add(new ListItem(Config.AppSettings.Domain, Config.AppSettings.Domain));
domainTypeDropDown
//Only add corporate AD domain if the user is a member of it.
string domainName = "";
if (Thread.CurrentPrincipal == null)
{
//"Missing the thread identity";
}
else if (Thread.CurrentPrincipal.Identity.Name.IndexOf("\\") < 0)
{
//"No domain detected leave domain name blank.";
}
else
{
domainName = Utils.GetUserDomain(Thread.CurrentPrincipal).ToUpper();
}
if (domainName == Config.AppSettings.Domain.ToUpper())
{
domainUserDropDownList.Items.Add(new ListItem(Config.AppSettings.Domain, Config.AppSettings.Domain));
domainOfficeDropDownList.Items.Add(new ListItem(Config.AppSettings.Domain, Config.AppSettings.Domain));
domainTypeDropDownList.Items.Add(new ListItem(Config.AppSettings.Domain, Config.AppSettings.Domain));
}I reviewed the code and suggested changing the code to the following:
```
//Only add corporate AD domain if the user is a member of it.
var currentPrincipal = Thread.CurrentPrincipal;
var hasDomain = currentPrincipal != null && currentPrincipal.Identity.Name.Contains("\\");
var domainName = hasDomain ? Utils.GetUserDomain(currentPrincipal) : null;
if (string.Equals(domainName, Config.AppSettings.Domain, StringComparison.OrdinalIgnoreCase))
{
domainUserDropDownList.Items.Add(new ListItem(Config.AppSettings.Domain, Config.AppSettings.Domain));
domainOfficeDropDownList.Items.Add(new ListItem(Config.AppSettings.Domain, Config.AppSettings.Domain));
domainTypeDropDown
Solution
Disclaimer: I'm not too familiar with C#.
The modified form has expressions that are a little too complex for me, but either would be ok. My only comment is that the initial comment line is screaming for some notice:
Why not take all the tests and put them into a function?
Then, depending on how you both feel about early exits:
The modified form has expressions that are a little too complex for me, but either would be ok. My only comment is that the initial comment line is screaming for some notice:
// Only add corporate AD domain if the user is a member of it.Why not take all the tests and put them into a function?
if ( IsMemberOfCorporateDomain( Thread.CurrentPrincipal ) ) ...Then, depending on how you both feel about early exits:
private bool IsMemberOfCorporateDomain( IPrincipal currentPrincipal )
{
if ( currentPrincipal == null ) return false;
if ( !currentPrincipal.Identity.Name.Contains("\\") ) return false;
var domainName = Utils.GetUserDomain(currentPrincipal);
return string.Equals( domainName, Config.AppsSettings.Domain, StringComparison.OrdinalIgnoreCase )
}Code Snippets
// Only add corporate AD domain if the user is a member of it.if ( IsMemberOfCorporateDomain( Thread.CurrentPrincipal ) ) ...private bool IsMemberOfCorporateDomain( IPrincipal currentPrincipal )
{
if ( currentPrincipal == null ) return false;
if ( !currentPrincipal.Identity.Name.Contains("\\") ) return false;
var domainName = Utils.GetUserDomain(currentPrincipal);
return string.Equals( domainName, Config.AppsSettings.Domain, StringComparison.OrdinalIgnoreCase )
}Context
StackExchange Code Review Q#26401, answer score: 16
Revisions (0)
No revisions yet.