3

This is perfectly fine C# code and works fine provided correct URL. But the everything is just done at one line by reducing the readability of the code.

Here is the code :

         return new StreamReader(WebRequest.Create(urlName).GetResponse().GetResponseStream()).ReadToEnd();

I am just wondering what are the opinions of fellow developers on this kind of short cut way of writing code

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Shiva
  • 1,379
  • 1
  • 15
  • 32
  • What exactly is your question? – Ken White Mar 03 '09 at 20:47
  • -1 For not being a real question. – GregD Mar 03 '09 at 20:48
  • Well, it's very compact and very very sequential. You can make a lot of that into asynch workflows of Begin/End-invoke. – Henrik Mar 03 '09 at 20:49
  • I'm pretty sure this is a dupe of lots of questions on SO regarding line length vs readability. I edited your question so it was more clear, but if this isn't the question you want answered you might want to edit it again and be more explicit what kind of help you need. – Adam Davis Mar 03 '09 at 20:49
  • what you mean by the question is not complete ? please read the entire question before writing any comments. – Shiva Mar 03 '09 at 20:51
  • not a real question, voting to close – ine Mar 03 '09 at 20:52
  • "not a real question", I believe the question is... do we think chaining methods / expressions in a single line is a good practice. – Inisheer Mar 03 '09 at 21:02
  • I understand the question to mean, should we write code using compressed single line structures, or is it better to break this out a bit... Seems like a legitimate question... – Dscoduc Mar 03 '09 at 21:04

5 Answers5

5

Push it into a well-named method, and perhaps break it up so that single statment stretches over a couple lines. I'd also probably use WebClient:

return new WebClient().DownloadString(urlName);
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    You would think after 3.5 versions they would have at least made a static method for that :) – leppie Mar 03 '09 at 21:03
5

No, it's not really perfectly fine C# code. You should dispose the StreamReader, so at least have a using statement:

using (StreamReader reader = new StreamReader(WebRequest.Create(urlName).GetResponse().GetResponseStream()) {
   return reader.ReadToEnd();
}

That code may gain a bit readability by dividing it into more lines, but not very much.

Generally I prefer readable code before compact code. Having one statement on each line makes the code easier to read and understand. For example:

if (i <= 4) i = 4 - i;

This becomes more readable with the if statement on one line and the code inside it on a separate line, with the if statement always having brackets:

if (i <= 4) {
   i = 4 - i;
}

This code is of course rather readable even in the compact form, but the more complex the code is, the more it gains from putting each statement on a separate line.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • I agree with you, Of course , I should dispose the object that implements IDisposable interface. – Shiva Mar 03 '09 at 22:02
0

...YUCK.

I will sometimes combine a few things into one line, usually when I am dumping stuff to a stream, but never this much.

Most compilers (c++ compilers at least) will often inline variable definitions if the definition is used only once, so if you make a one time use, throw away variable. Your C# compiler will probably just replace its name with its definition.

James Matta
  • 1,562
  • 16
  • 37
0

In addition to the readability problem, you should dispose any IDisposble object you are using.

Juanma
  • 3,961
  • 3
  • 27
  • 26
0

One statement != one line , you can improve readability by improving formatting of your code. Of course you should not assume other people use high resolution monitors.

Yuan
  • 2,690
  • 4
  • 26
  • 38