Re: Creating Full Name from given, initials, sn, when some might be null



Responding to Ssg31415926...

This forum is usually concerned with OOA/D issues rather that tactical coding practices in particular OOPLs, so you might get a better answer in the appropriate language or programming forum. But as long as we're here...

I have three attributes: givenName, initials and sn (surname) - one or
more could be null but not all three. I have to create a displayName
attribute from as many of the three as are present, with spaces
separating them. E.g.
givenName = Bob
initials = <empty>
sn = Smith
should produce Bob Smith whereas
givenName = Lisa
initials = B
sn = Smith
should produce Lisa B Smith

Is there a better way of writing it than this, which feels clunky to
me (if the attribute is <null>, it's not present in the Properties
collection, hence checking using Count):

StringBuilder sb = new StringBuilder();
bool previousValueSet = false;
if (src[0].Properties["givenName"].Count > 0)
{
sb.Append(src[0].Properties["givenName"]
[0].ToString());
previousValueSet = true;
}
if (src[0].Properties["initials"].Count > 0)
{
if (previousValueSet)
{
sb.Append(" ");
}
sb.Append(src[0].Properties["initials"]
[0].ToString());
previousValueSet = true;
}
if (src[0].Properties["sn"].Count > 0)
{
if (previousValueSet)
{
sb.Append(" ");
}
sb.Append(src[0].Properties["sn"][0].ToString());
}
string fullName = sb.ToString();
SSG

You could make it more compact by making the string operations generic (using appropriate syntax for whatever language you are using):

static String nameIdentifiers [3] = {"GivenName", "initials", "sn"};
StringBuilder sb = new StringBuilder();
bool previousValueSet = FALSE;
for (i = 0; i < 3; i++)
{
if (src[0].Properties[nameIndentifiers[i]].Count > 0)
{
if (previousValueSet)
sb.Append(" ");
sb.Append(src[0].Properties[nameIdentifiers[i]][0].ToString());
previousValueSet = TRUE;
}
}


*************
There is nothing wrong with me that could
not be cured by a capful of Drano.

H. S. Lahman
hsl@xxxxxxxxxxxxxxxxx
Pathfinder Solutions
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
"Model-Based Translation: The Next Step in Agile Development". Email
info@xxxxxxxxxxxxxxxxx for your copy.
Pathfinder is hiring: http://www.pathfindermda.com/about_us/careers_pos3.php.
(888)OOA-PATH



.