not all code paths return a value?

H

HillBilly

Brainded and can't see how to resolve this nagging error...

public static bool IsUserAFollowedByUserB(string user_a, string user_b)
{
// Is user_a followed by user_b?
string followingURI =
"http://twitter.com/friendships/exists.xml?user_a={0}&user_b={1}";

try
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(string.Format(followingURI, user_a,
user_b));

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
XmlReader reader = XmlReader.Create(resp.GetResponseStream());
XElement following = XElement.Load(reader);
IEnumerable<XElement> enumerableList = from el in
following.Elements()
select el;
foreach (XElement e in enumerableList)
{
if (e.Name == "friends" && e.Value == "true")
{
return true;
}
else
{
return false;
}
}
}
catch (Exception exf)
{
throw new ApplicationException(string.Format("Twitter Exception"));
}
}
 
J

J.B. Moreno

Peter Duniho said:
-snip-
Seems to me that the code really ought to read more like this:

public static bool IsUserAFollowedByUserB(string user_a, string user_b)
{
// Is user_a followed by user_b?
string followingURI =
"http://twitter.com/friendships/exists.xml?user_a={0}&user_b={1}";

try
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(string.Format(followingURI, user_a,
user_b));

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
XmlReader reader = XmlReader.Create(resp.GetResponseStream());
XElement e = XElement.Load(reader).Elements().FirstOrDefault();

return (e != null && e.Name == "friends" && e.Value == "true");
}
catch (Exception)
{
throw new ApplicationException("Twitter Exception");
}
}

Except that puts him back to having not all code paths returning a
value.

public static bool IsUserAFollowedByUserB(string user_a, string user_b)
{
// Is user_a followed by user_b?
string followingURI =
"http://twitter.com/friendships/exists.xml?user_a={0}&user_b={1}";

XElement e = null;

try
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(string.Format(followingURI, user_a,
user_b));

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
XmlReader reader = XmlReader.Create(resp.GetResponseStream());
e = XElement.Load(reader).Elements().FirstOrDefault();

}
catch (Exception)
{
throw new ApplicationException("Twitter Exception");
}

return (e != null && e.Name == "friends" && e.Value == "true");

}
 
J

J.B. Moreno

Peter Duniho said:
[...]
public static bool IsUserAFollowedByUserB(string user_a, string user_b)
{
// Is user_a followed by user_b?
string followingURI =
"http://twitter.com/friendships/exists.xml?user_a={0}&user_b={1}";

try
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(string.Format(followingURI, user_a,
user_b));

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
XmlReader reader = XmlReader.Create(resp.GetResponseStream());
XElement e = XElement.Load(reader).Elements().FirstOrDefault();

return (e != null && e.Name == "friends" && e.Value == "true");
}
catch (Exception)
{
throw new ApplicationException("Twitter Exception");
}
}

Except that puts him back to having not all code paths returning a
value.

Did you try compiling it? Where in that code is it that you believe there
is a code path that doesn't return a value?

No, I didn't. I was thinking that there's no return if there's an
exception, but I guess the fact that the catch block throws it's own
exception eliminates the need for a return on that code path...
 
H

HillBilly

Peter Duniho said:
[...]
public static bool IsUserAFollowedByUserB(string user_a, string user_b)
{
// Is user_a followed by user_b?
string followingURI =
"http://twitter.com/friendships/exists.xml?user_a={0}&user_b={1}";

try
{
HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(string.Format(followingURI, user_a,
user_b));

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
XmlReader reader = XmlReader.Create(resp.GetResponseStream());
XElement e = XElement.Load(reader).Elements().FirstOrDefault();

return (e != null && e.Name == "friends" && e.Value == "true");
}
catch (Exception)
{
throw new ApplicationException("Twitter Exception");
}
}

Except that puts him back to having not all code paths returning a
value.

Did you try compiling it? Where in that code is it that you believe there
is a code path that doesn't return a value?

Peter's suggestion has no code path problems but I'm trying to figure out
how and why the reader object is not populated with data WHEN there is data
in the req and resp objects.
 
H

HillBilly

Peter Duniho said:
Who says it's not?

If you are asking why the compiler error happens, even though you know
your data always has something in it, it's because the compiler doesn't
know anything about your data. It has no way to know for sure that the
collection will always have at least one element, and so there's a
theoretical code path in which the loop body isn't executed, and in which
you didn't return a value.

The fact that you know for sure this won't (or shouldn't) happen is
immaterial. The compiler can't know that.

This is in fact part of what I described in my reply: if you are _sure_
that the collection will always have at least one element, then you can
use Enumerable.First() instead of Enumerable.FirstOrDefault(). This
causes invalid data to throw an exception rather than being handled as if
it were valid.

If you're not asking why the compiler error happens, but rather about
something else, you should be more specific. We have no clue about the
data, and have no example of a situation where "the reader object is not
populated with data WHEN there is data in the req and resp objects", so
the literal question you asked isn't possible to answer.

Pete

Well, I'm glad you brought that up Pete because I understand and agree with
your premise regarding enumerating over the collection --and-- what the
compiler will or will not know --and-- it is interesting to note there is in
fact one and only one element which Twitter would return [1]. So, I can use
the First() method as this part of their API will never change as its only
meant to return a single value used to confirm or deny if Twitter user_a is
being followed by Twitter user_b.

As per my concerns about not seeing data in the reader object I can not see
any data in the locals window when debugging. I break on each line of the
following:

HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(string.Format(followingURI, user_a,
user_b));
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
XmlReader rdr = XmlReader.Create(resp.GetResponseStream());
XElement el = XElement.Load(reader).Elements().First();

req: I see the entire URL with values passed to the QueryString
rsp: I see the header and response codes as expected
reader: debugger reports (none) and I can see no data in what I believed to
be a well formed instance of a reader object. So this would be why el
continues to be null and why I haven't gotten any work done yet being
f*cking stymied by this code snippet pattern I've seen used in so many
blogs. Why would there not be data in the reader object when the response
was okay?


[1] <friends>[true|false]</friends>
 
H

HillBilly

Peter Duniho said:
Who says it's not?

If you are asking why the compiler error happens, even though you know
your data always has something in it, it's because the compiler doesn't
know anything about your data. It has no way to know for sure that the
collection will always have at least one element, and so there's a
theoretical code path in which the loop body isn't executed, and in which
you didn't return a value.

The fact that you know for sure this won't (or shouldn't) happen is
immaterial. The compiler can't know that.

This is in fact part of what I described in my reply: if you are _sure_
that the collection will always have at least one element, then you can
use Enumerable.First() instead of Enumerable.FirstOrDefault(). This
causes invalid data to throw an exception rather than being handled as if
it were valid.

If you're not asking why the compiler error happens, but rather about
something else, you should be more specific. We have no clue about the
data, and have no example of a situation where "the reader object is not
populated with data WHEN there is data in the req and resp objects", so
the literal question you asked isn't possible to answer.

Pete

Well, I'm glad you brought that up Pete because I understand and agree with
your premise regarding enumerating over the collection --and-- what the
compiler will or will not know --and-- it is interesting to note there is in
fact one and only one element which Twitter would return [1]. So, I can use
the First() method as this part of their API will never change as its only
meant to return a single value used to confirm or deny if Twitter user_a is
being followed by Twitter user_b.

As per my concerns about not seeing data in the reader object I can not see
any data in the locals window when debugging. I break on each line of the
following:

HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(string.Format(followingURI, user_a,
user_b));
HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
XmlReader rdr = XmlReader.Create(resp.GetResponseStream());
XElement el = XElement.Load(reader).Elements().First();

req: I see the entire URL with values passed to the QueryString
rsp: I see the header and response codes as expected
reader: debugger reports (none) and I can see no data in what I believed to
be a well formed instance of a reader object. So this would be why el
continues to be null and why I haven't gotten any work done yet being
f*cking stymied by this code snippet pattern I've seen used in so many
blogs. Why would there not be data in the reader object when the response
was okay?

// Sequence contains no elements error (because reader is empty)
XElement el = XElement.Load(reader).Elements().First();

[1] <friends>[true|false]</friends>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top