Reputation: 17427
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?
Upvotes: 11
Views: 6482
Reputation: 56467
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...
Upvotes: 3
Reputation: 45445
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).
Upvotes: 4
Reputation: 659994
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.
Upvotes: 24
Reputation: 30865
IMHO there is no best practice in this case. The code should be readable and convenient for you and future coders.
Upvotes: 2
Reputation: 26782
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
Upvotes: 4
Reputation: 22580
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.
Upvotes: 1
Reputation: 273179
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.
Upvotes: 1
Reputation: 62472
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.
Upvotes: 5
Reputation: 160852
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.
Upvotes: 5