Skip to content

Know your framework

What is wrong with the following program?

using System;
using System.Net;
using System.IO;
using System.Text;
class Example { static void Main() { Console.WriteLine( getWebPage("http://example.com")); }
static String getWebPage(String url) { WebRequest request = WebRequest.Create(url); WebResponse response = request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(stream); return streamReader.ReadToEnd(); } }

At first glace there doesn’t appear to be anything wrong at all – the program compiles and runs. This program does exactly what it sets out to do.

So what is the problem? Perhaps this second example will help you see better what was wrong with the first one:

using System;
using System.Net;
class Example { static void Main() { Console.WriteLine( new WebClient().DownloadString("http://example.com")); } }

The problem is now quite apparent: the .NET framework has a method that allows you to retrieve a string response from a URL. If the developer who wrote the first example had known this they would have saved a lot of time and effort by using the framework method that accomplishes the task instead of creating their own.

This is a simple example of an all too common problem:

Many developers don’t know their frameworks.

Why is it important to know your framework? You write less code and spend less time working on algorithms that have already been designed and tested by someone else. Also the person who wrote the framework (let’s face it) is probably a lot smarter than you or me and was able to write a better implementation that we could have.

Finally, you must think about the readability of your code. What is easier to understand, a custom method that the reader must read to continue understanding your program or a framework method that they are most likely already familiar with? Consider the first example and the getWebPage method. Another programmer reading the the Main method of this program would have to read the entire implementation of the getWebPage method to understand what it does. This same programmer would most likely be familiar with the WebClient.DownloadString method from the .NET framework and would instantly understand what was going on and if they didn’t they would be able to consult the documentation of the framework.

So if you don’t already know your framework how do you go about learning it? You need to be constantly pushing yourself to learn new things. Does your IDE offer auto-complete information about the libraries you use? Try turning that off occasionally to see how much you remember. Other times leave it on and explore the APIs of your framework and see what methods are out there that you haven’t used before.

Before you begin designing a solution to a problem, search the internet for other folks who have solved the same problem. These searches will either lead you to framework documentation or code examples that will help you solve problems using existing functionality.

In conclusion it is important to remember that you won’t always get this right and you will re-implement framework functionality by mistake. But if you strive to find reusable components before writing your own you will make this mistake less often.

3 Comments

  1. Dan Leonard

    Nice article! Keep up the good work!

    Posted on 21-Oct-09 at 11:59 am | Permalink
  2. Amit Mohod

    superb… too well written..

    Posted on 26-Nov-09 at 9:28 am | Permalink
  3. Thanks, Amit!

    Posted on 28-Nov-09 at 4:28 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*