Is it better to use local or global variables
Asked Answered
O

5

6

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.

Operose answered 24/7, 2017 at 23:35 Comment(7)
This is an easy one. Globals are almost always a mistake and will bite you in unexpected ways. Also, passing values to functions is what Java does.Coleridge
"If I make some variables global(across the class)" -- ordinary fields in a class are not "global" in the way that most programmers would use that term. You may have better luck if you provide a minimal reproducible example demonstrating exactly what you mean by "make some variables global".Divergence
According to this train of thought fields are evil and should be avoided?Prospective
@efekctive Specifically, it's mutable fields which are evil. Mutability adds more complexity than it saves if used unnecessarily.At
@Divergence instead of function(x,x,null,null,null,null,null,null), if I set variable once, why not to use function()Operose
@MmtBkn If you have a function with that many parameters, you need to refactor the function, and perhaps define some new objects. Removing parameters will just make things worse.At
There are a bunch of ifs there. The OP is defining fileds as global. I don't stick to any hardline despite agreeing with what you sayProspective
H
16

Always prefer local over global. If you need to pass the data in as multiple parameters, so be it. At least then you're explicitly saying what data your function depends on. Having too many parameters is certainly a problem, but offloading some of them as globals isn't the answer.

If you rely on globals, it may not be as clear where certain data is coming from. If the globals are mutable, you'll have a mess on your hands as soon as you start to try to debug a difficult problem since it may not be obvious when certain global variables are being modified.

Note though that immutable constant globals aren't bad. If you have a constant that's needed in many functions (like PI for example), it makes sense to make it global. Immutable constants don't suffer from the drawbacks mentioned above since they can't change.

Hetaerism answered 24/7, 2017 at 23:38 Comment(0)
S
6

You wrote a 2000+ lines of service class. You completed the project. Cool ! Now after a month, you got a bug reported and are required to fix it.


Lets go through 2 different cases :

CASE 1

You are back on the service code. You see that the func1() uses globalVariabl1. Okay, but whats its value by now ? How does it change ? Who mutates the globalVariabl1 before it comes to this function ? What have been the sequence of all these mutations ? You would have no idea. It will be quite difficult to figure all this out.

CASE 2

You are back to you code, and see that the func0() fetches something and then passes it to func1(param1) as a parameter. You clearly know what the data is, how does it gets here.


In what case will it be easier to resolve the bug ? Most of the time, CASE 2 will make it lot easier.

Local variables

Local variables would always help you. Even when you write the code and using local variables, the call statement will itself tell you that this function depends on this particular data. It helps you to be careful about what you are passing around.

Global variables

Global variables are okay when they represent the state of the class/object, or even when they are Constant (which should in general be all UPPERCASE letters). They can also be good when you just need to access the value frequently, and you know that the variable will always be initialised when you use it (for example initialising it inside onCreate())

Semaphore answered 24/7, 2017 at 23:51 Comment(0)
G
2

There are no global variables in Java. You are referring to member variables. The basic rule is that variables should have the smallest possible enclosing scope.

Grume answered 25/7, 2017 at 1:30 Comment(0)
B
2

I know the question is already answered and I upvoted Carcigenicate's answer.

To elaborate on his point I would suggest you try Test Driven Development practices. As soon as you start writing your code in conjunction with Unit test you will realize how bad Global variables can be and you will realize that you are writing code that cannot be tested without having to implement unnecessary Dependency Injection.

One more thing. Global variables are a huge mistake any time you start dealing with multiple threads and concurrency. It doesn't sound like you are dealing with that but keep it in mind any time you decide to make a Global variable.

Bonney answered 25/7, 2017 at 1:35 Comment(0)
P
0

It all depends on the scope of the variable. If you feel that a certain variable will take multiple values by passing through various functions then use local variables and pass them in function calls. If you feel that a certain variable you need to use will have constant value, then declare it as a global variable.

Pathy answered 24/7, 2017 at 23:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.