Is it better to use local or global variables

Viewed 10520

Is it better to use local or global variables?

Let's say talking about 2000+ lines of android(java) service class, and all service is working on 'request' object and similar shared objects.

If I make everything local(keep inside of function), I need to pass many variables every time, or override same function many times. I need to make sure that objects, and sub objects are not null too.

If I make some variables global(across the class) I can share them, use them across the functions. Which I think will make everything easier.

What are the good sides and bad sides of defining variables inside of function or defining globally. In practice, and in theory(readability etc).

Is there suggested way?

Thank you.

5 Answers
Related