I have a dialog which populates my ConnectionStringData
object with the URL of the service that I'm trying to connect to. I then consume that URL with the following property:
/// <summary>
/// Where the Service is located.
/// </summary>
public string ServiceLocation
{
get
{
string fullUrl = ConnectionStringData.Location.Trim();
if (fullUrl.EndsWith("/"))
{
fullUrl += "Routes";
}
else
{
fullUrl += "/Routes";
}
Uri uri = null;
if(Uri.TryCreate(fullUrl, UriKind.Absolute, out uri))
{
return uri.ToString();
}
throw new UriFormatException(String.Format("{0} is an invalid url", fullUrl));
}
}
Should I be doing work like this in the getter? Can I do this better?
2 Answers 2
A much more readable version would be
public string ServiceLocation
{
get
{
string fullUrl = ConnectionStringData.Location.Trim().TrimEnd('/') + "/Routes";
Uri uri = null;
if (Uri.TryCreate(fullUrl, UriKind.Absolute, out uri))
{
return uri.ToString();
}
return String.Empty;
}
}
-
\$\begingroup\$ You are right, I prefer this style as opposed to the additional ternary. You've used backslashes however which is correctly for folder path's but not URL's. :D \$\endgroup\$aydjay– aydjay2014年11月13日 10:46:16 +00:00Commented Nov 13, 2014 at 10:46
-
\$\begingroup\$ Any reason especially for a new char[] when you can just pass in: '/' to TrimEnd. \$\endgroup\$aydjay– aydjay2014年11月13日 10:48:35 +00:00Commented Nov 13, 2014 at 10:48
-
1\$\begingroup\$ Not really, intellisense said char[] so I used it ;-) \$\endgroup\$Heslacher– Heslacher2014年11月13日 10:50:14 +00:00Commented Nov 13, 2014 at 10:50
-
\$\begingroup\$ Be aware that this behaves differently when the original string ends with multiple
/
s, thought I guess such URLs should be pretty rare. \$\endgroup\$svick– svick2014年11月14日 22:16:31 +00:00Commented Nov 14, 2014 at 22:16
This piece:
if (fullUrl.EndsWith("/"))
{
fullUrl += "Routes";
}
else
{
fullUrl += "/Routes";
}
can be rewritten to:
fullUrl += fullUrl.EndsWith("/") ? "Routes" : "/Routes";
or:
if (!fullUrl.EndsWith("/"))
fullUrl += "/";
fullUrl += "Routes";
Since you're using the TryCreate
method, there's no need to throw an exception. You just have to notify the caller whether the conversion succeeded or not. Plus, throwing an exception in a get statement is bad practice. This results in following:
if (Uri.TryCreate(fullUrl, UriKind.Absolute, out uri))
return uri.ToString();
//if no succesful creation:
return String.Empty;
If you do want to throw an exception, use a try/catch block and a regular Uri constructor. This should be placed in a method and not in the get statement then:
try
{
uri = new Uri(fullUrl, UriKind.Absolute);
return uri.ToString();
}
catch (UriFormatException ex)
{
throw;
}
My favor would still be with the TryCreate
resulting in following final code:
public string ServiceLocation
{
get
{
var fullUrl = ConnectionStringData.Location.Trim();
fullUrl += fullUrl.EndsWith("/") ? "Routes" : "/Routes";
Uri uri = null;
if (Uri.TryCreate(fullUrl, UriKind.Absolute, out uri))
return uri.ToString();
return String.Empty;
}
}
-
\$\begingroup\$ You don't need to initialize it with null, that value is never used. \$\endgroup\$ANeves– ANeves2014年11月13日 10:32:46 +00:00Commented Nov 13, 2014 at 10:32
-
\$\begingroup\$ See stackoverflow.com/questions/359732/… regarding your curly braces. Plugins like
StyleCop
specifically flag this structure \$\endgroup\$ediblecode– ediblecode2014年11月13日 10:39:08 +00:00Commented Nov 13, 2014 at 10:39 -
\$\begingroup\$ @ANeves please tell us why? \$\endgroup\$devops– devops2014年11月13日 10:40:14 +00:00Commented Nov 13, 2014 at 10:40
-
1\$\begingroup\$ Also shouldn't it be
fullUrl = ConnectionStringData.Location.Trim();
\$\endgroup\$ediblecode– ediblecode2014年11月13日 10:41:00 +00:00Commented Nov 13, 2014 at 10:41 -
2\$\begingroup\$ @dit you initialize with null, then immediately use it as an out parameter, which overrides the value. So the null value will always be overwritten without a chance to be used. (Quote from MSDN:
Although variables passed as out arguments do not have to be initialized before being passed, the called method is required to assign a value before the method returns.
) \$\endgroup\$ANeves– ANeves2014年11月13日 11:52:34 +00:00Commented Nov 13, 2014 at 11:52
uri
tonull
,out
will take care of that. \$\endgroup\$