Considering I have a for
loop in a method of a class. Should the incremented variable be declared as member of the class
, or should it be declared in the method
it uses it(or even in the for
loop, directly)? While declaring it inside the method seems easier and cleaner, doesn't declaring it at the start of the class makes you have more control over it? Something like declaring it int var_used_for_loop_in_method_x
. What is good programming practice?
Note that I'm not asking specifically for the for
loop. I am asking if we should try to put most variables as class members as much as possible or not.