Skip to main content
2 of 2
removed off-topic "what is best practice?" question
200_success
  • 144.2k
  • 22
  • 188
  • 473

Adding some properties to an Active Directory user

I was writing the below section of code which adds some user properties to an Active Directory user, and got wondering - surely we can improve the readability of this :

var firstname = newUser.FirstName;
var lastname = newUser.LastName;
var email = firstname + lastname + "@mydomain.com";
var dotname = $"{firstname}.{lastname}";
var fullname = $"{ firstname} { lastname}";
        
// Create the new user
var user = myLdapConnection.Children.Add($"CN={firstname} {lastname}", "user");
// Add properties
user.Properties["userprincipalname"].Add(email);
user.Properties["samaccountname"].Add(dotname);
user.Properties["sn"].Add(lastname); 
user.Properties["givenname"].Add(firstname);
user.Properties["displayname"].Add(fullname);
user.Properties["description"].Add(newUser.JobTitle);
user.Properties["mail"].Add(email); 
user.Properties["homedirectory"].Add($@"\\File\Home$\{dotname}");
user.Properties["homedrive"].Add("H:");
user.CommitChanges();

So I added some tabs after my property-name calls like so :

// Add properties
user.Properties["userprincipalname"].Add(email);
user.Properties["samaccountname"]   .Add(dotname);
user.Properties["sn"]               .Add(lastname); 
user.Properties["givenname"]        .Add(firstname);
user.Properties["displayname"]      .Add(fullname);
user.Properties["description"]      .Add(newUser.JobTitle);
user.Properties["mail"]             .Add(email); 
user.Properties["homedirectory"]    .Add($@"\\File\Home$\{dotname}");
user.Properties["homedrive"]        .Add("H:");
user.CommitChanges();

Now I think that makes the code a lot more readable, but I have not seen many people take this approach when formatting. Is there any reason for this? I generally do this in both PowerShell and C#, but are there any languages where this is not acceptable for reasons other than the code wont run?

Bassie
  • 559
  • 7
  • 20