开发者

reuse of variables

开发者 https://www.devze.com 2023-04-08 13:25 出处:网络
I\'m working on project that need do call the same method several times, but with different arguments.

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

Can I use t开发者_如何学Che 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?


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.


You can reuse the variable - you might consider not doing this though and name them each so you know what the responsibility of each request is, i.e. reqLogin and reqData. In the long run this makes for more readable code in my opinion.


I'd go for declaring a new variable. That way, if someone happens to quickly eyeball the code then they're not going to be confused or have to spend long working out that they're two different requests.


I think I would extract one or different methods, would have to see more code e.g.

var getDataRequest = CreateGetDataRequest();

var postRequest = CreatePostRequest();

or maybe this could be one method with a few parameters


Variables are cheap, except when they are confusing. One of a variable's most important characteristics is its scope, which gets obfuscated when it is reused. Don't focus on reusing something because it has the same type; that is much like reusing a napkin (eew!). Focus on the problem they solve (a spill on my shirt vs. my friend's).


I'll be categorical: never reuse variables like this. It prevents proper isolation of concerns and makes the code harder to read and harder to change. Think about if you want to refactor the code later (you should already do this) and encapsulate each piece in its own method. Reusing the same variable just gets in the way...


IMHO there is no best practice in this case. The code should be readable and convenient for you and future coders.


If they don't overlap you might as well use the same variable. It makes no real difference, so choose what is most readable.

You are creating a new object with each call to CreateWebRequest, that is what matters.


It really depends on the answer to the question "Is it important that I still have a reference to req later?" In other words, do you ever need to call anything off of req again? If the answer is no, don't declare a new variable. If the answer is yes, then store all of the requests in a List<HttpWebRequest> or some other data structure that can manage the requests for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消