4

Very few books and articles ever go into detail about a) why multiple methods are provided to complete a specific task in a specific scenario and b) what considerations a developer should take when choosing between methods.

My specific case in point is the simple opening of a text file (classic first section of any IO chapter). Most books/articles just say "Hey, here's how to open a text file: StreamReader with file path. Tada and end of section!"

Some do at least list the fact that there are multiple classes that allow you to do this e.g:

  • File class
  • FileInfo class
  • FileStream class
  • StreamReader class
  • etc, etc, etc

Fewer discuss that there are even more overloads so you can use each class in several different ways.

Even fewer even try to enter into any discussion about how to select the best method for the task at hand. (Some of course do and these are the ones that usually end up in the top three when someone posts a poll-like question about "best" books.)

This lack of discussion drives me mad. I feel like I should toss a coin.

This question has the joint purposes of:

  • Finding out if anyone thinks this is a worthwhile topic in the first place
  • Offering people an opportunity to list any interesting articles or books that specifically go into these kinds of discussions

I've made this a wiki because other similar questions were made into wikis. And if people feel this question has no merit, I apologize and feel free to vote it closed.

Another Example

I am currently reading about BinaryWriter and BinaryReader. The last 20 articles I have hit using google and at least 5 books on O'Reilly Safari have shown me how to write a single integer and read it back. A couple included an example of how to use WriteString and ReadString. Exactly ONE let me know that WriteString() and ReadString() use a special encdoding meaning if you do WriteString() and ReadChar() you get a prefix. There are lots of ways to use these classes. Why do hardly any articles discuss this?

One More

Another example of what I mean is when a book or article lists class members and descriptions word for word from MSDN without adding any value. I do not need a list of class members because I can get that from MSDN. From an author, I need experience, real world knowledge i.e. stuff that will expand me as a developer. Not stuff I can look up in five seconds. All the member lists and simple i-don't-do-anything-useful examples in the world aren't going to really help anyone who wants to learn their stuff.

Last One - I promise

Just reading about StringWriter and StringReader. Same thing. Lots of articles and books giving simple i-don't-do-anything-useful examples. Some at least say something vague like I may want to use StringWriter if I want to write characters to an underlying buffer. How would I know if I want to do that? What real world scenarios would require it? I could go on all night.

OK So I Lied...

This question has answers from Merhdad and jalf that both explain how you would choose between two methods of generating a random number and demonstrates (I think) the kind of information I am talking about. jalf says, something along the lines of "In most cases use Option A. But in this specific case when you are trying to achieve Result X, use Option B. And by the way, this will have Negative Effect M."

X vs Y articles

I figured out I can get what I want by searching for X vs Y articles e.g. this one that discusses how to choose between the Directory and DirectoryInfo classes (and File and FileInfo classes).

So now this question is:

Can you list any useful X vs Y articles?

Community
  • 1
  • 1
J M
  • 1,877
  • 2
  • 20
  • 32

4 Answers4

4

In some cases, a duplicate way of performing certain tasks is available because they further abstracted it in a subsequent version. Think about the WebClient class which I believe was introduced in .Net 2.0. It basically wraps a lot of the common tasks you used to have to do with several different classes (HttpWebRequest, HttpWebResponse, StreamReader, etc). The old classes are obviously still there if you need to do things a little differently, and for backward compatibility, but if all you need to do is download a resource from the web as a string, you've got WebClient.DownloadString.

Also, you're usually using .Net because you're not splitting hairs over cpu cycles, so the solution with the least amount of code is the preferred method if you're not concerned with performance. If you're programming a server task where no user is going to be "waiting" on a process and you need to open one file and read it into a string, go with the static method that lets you do the whole thing in one line.

Rich
  • 36,270
  • 31
  • 115
  • 154
  • This is good because it gives an explanation to how you would choose in most scenarios e.g. go for the least code wherever possible. I guess for this to be useful, the community would maybe describe scenarios in their experience where the simplest didn't cut it and how they selected a better way. – J M Mar 20 '09 at 23:19
2

If you're looking for some of the "why"s in print, you might pick up Bill Wagner's recent More Effective C#.

Ðаn
  • 10,934
  • 11
  • 59
  • 95
  • +1 thanks for this - maybe I shoud change this to actually be a list of books and links that discuss the why's - that sounds like it would be more useful – J M Mar 21 '09 at 19:27
1

I suppose a fairly obvious example would be string concatenation.

When concatenating just a handful of strings, you may as well just use the + operator or String.Concat. More than that and you should really consider using StringBuilder for performance reasons.

Is that the kind of thing you're thinking of?

Ian Nelson
  • 57,123
  • 20
  • 76
  • 103
  • This is a classic example of what I mean. However, I'm worried that this could become a repeat "best practices" question. That worry made me hesitate quite a bit before posting it. I'm very interested in scenarios that aren't covered by the majority of best practice lists if there are any..... – J M Mar 20 '09 at 23:10
1

One example that took a while for me to straighten out was the different means of executing code asynchonously. There are BackgroundWorkers, Delegate.BeginInvoke/EndInvoke, and manual thread manipulation. In that case I've found that using Delegates is a good balance of ease of implementation and control; however using manual threads buys you extra control wihtout too much more work--but there is the tradeoff that creating new threads (as opposed to using the recycled ones in the threadpool) is an additional overhead.

STW
  • 44,917
  • 17
  • 105
  • 161
  • This is great. I was just reading about this topic a couple of days ago and trying to determin how to choose between using an asynchronous e.g. BeginRead or calling the synchronous method aysnchronously via a delegate. My thought was the asynchronous method seem to have extra checks as have been provided because those checka are required? But that's as far as I've got with it. – J M Apr 22 '09 at 07:46
  • Methods that are named "Begin...Async" and return an IAsyncResult are most often wrappers which handle the delegate manipulation for you. One important tip: If you call Begin...Async then you should *always* call the corresponding End... method. If the async method (on a worker thread) returns a value or raises an exception then your main thread will not be notified until calling End... if you never call end then the background thread is never freed since it hasn't notified you of its result – STW Apr 22 '09 at 12:12
  • btw, below is the MSDN article regarding working with delegates--it also applies largely to working with Begin...Async/End... methods. You might want to read-up on the proper practices for using that IAsyncResult object, if you only execute a given method asyncronously once then it isn't terribly important but it is still a significant part of the model. http://msdn.microsoft.com/en-us/library/2e08f6yc%28VS.80%29.aspx – STW Apr 22 '09 at 12:17