Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

reuse of variables

Tags:

variables

c#

I'm working on project that need do call the same method several times, but with different arguments.

Can I use the same variable or do I have to declare another variable?

For example:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(domains["ServiceLogin"]);
            req.Method = "GET";
            req.Referer = "Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0";
            req.CookieContainer = new CookieContainer();
            HttpWebResponse response = (HttpWebResponse)req.GetResponse();
            CookieCollection cookies = response.Cookies;
            response.Close();

etc..

Do I use the req variable or declare req2 for example

  req = (HttpWebRequest)WebRequest.Create(domains["ServiceLogin"]);
  req.Method = "POST";
  req.CookieContainer = myCookieContainer;

What's the best way/practice to do this?

like image 608
The Mask Avatar asked Sep 27 '11 14:09

The Mask


1 Answers

Local variables are cheap; there's no compelling benefit to re-using them unnecessarily. Therefore: write your code so that each variable has a clear purpose and that purpose is described by its name. Once each variable's name describes its purpose, it will become more clear whether you need one variable or multiple variables.

like image 159
Eric Lippert Avatar answered Sep 21 '22 21:09

Eric Lippert