Bug? Local variable overrides global

Martin Larsen

Active Member
Licensed User
Longtime User
I have a global and local variable with the same name. If I change the local variable, it overrides the global one. Example below. Demo is also attached.

This is not how it works in other languages. A local variable will never override a global variable.

Bug or "feature"?

Local variable overrides global:
Sub Globals
    Private myvar As String = "Global"
    Private EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("1")
    EditText1.Text = "Local"
End Sub

Sub Button1_Click
    Private myvar As String = EditText1.Text ' Shouldn't override global myvar!
    ToastMessageShow ("Local variable myvar set to '" & myvar & "'", True)
End Sub

Sub Button2_Click
    MsgboxAsync (myvar, "Showing global myvar")
End Sub
 

Attachments

  • vartest.zip
    9.1 KB · Views: 247
Last edited:

Martin Larsen

Active Member
Licensed User
Longtime User
Not a bug. There could never be a local variable with the same name as a global variable, this means that myvar is a global variable. Even if you declared it again.

This is very unexpected and unfortunate. I have yet to see another programming language that won't allow local variables with the same name as globals. (That's one of the main ideas behind local variables after all - to avoid name conflicts)

Could you perhaps make a compiler warning: Warning: Local variable overrides global with the same name.

This could have saved me two hours of debugging.
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User
take a look at main.java in your Objects folder. you'll see how the global myvar is declared and then, further down in the button_click() sub, the java translation essentially ignores the local myvar. it assigns your edittext.text value to the already existant global myvar. it is what it is.
 

Sandman

Expert
Licensed User
Longtime User
Just curious, I have to ask:

I've never heard of anyone that knowingly and willingly uses the same variable name globally and locally. Is this really something people do?
 

Martin Larsen

Active Member
Licensed User
Longtime User
I've never heard of anyone that knowingly and willingly uses the same variable name globally and locally.

It was not exactly knowingly. It was this statement:

B4X:
Dim js As String = $"document.querySelector("#edit-submit").click()"$

I also used a js variable in Globals with another content which I use several places, hence the global declaration.

Is this really something people do?

Your question misses the point. The point is, you should never have to wonder if you somewhere have declared a global variable named the same as your local one. Never!

That said, global variables should be avoided if possible (because of issues like this and similar) but you can't always avoid it.
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
I too would prefer it not to be so (and I think it would be very useful to have also local static variables), but this is the situation, you just have to be careful.

I personally don't use common variable names. Just the classic "i", "j" as cycle counters, but using them like this, they are initialized every time.

For module-level variables, I always use names that make sense and letter "m" as a prefix ("g" for process globals).
 

Martin Larsen

Active Member
Licensed User
Longtime User
Well, I'm all for having the linter in the IDE help out more. Post a proper wish and I'll give it a like.

Done!
 

drgottjr

Expert
Licensed User
Longtime User
run the attached and check the log. it's user larsen's test done in inline java. global is global and global is local and global global keeps his identity even after local global is assigned. Schrödinger's cat is alive and dead at the same time.
 

Attachments

  • larsen.zip
    9.4 KB · Views: 228

sorex

Expert
Licensed User
Longtime User
I've never heard of anyone that knowingly and willingly uses the same variable name globally and locally. Is this really something people do?

sure why not?

languages like for example PHP support this and you can use GLOBAL myvar in a sub when you want to use the global one. otherwise it will create a sub based one.

so yes, I understand Martin's confusion.
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Not specifically on-topic, but, I "grew up" in a place that had a "shop standard" that required every variable to have a two-character code prefix to indicate the scope and type of the variable to assist in peer code review. For example, if you saw "gsMyVar" in a statement you would know it was a global-scope string variable without having to go find its declaration, and if that variable didn't make sense in that statement it was a clue to look deeper.

Having coded that way for an eternity it's second-nature to me now and it has saved me countless hours of debugging. Your mileage may vary.
 
Top