Wish Local variables should not (without warning) override global variables with the same name

Martin Larsen

Active Member
Licensed User
Longtime User
If you declare at local variable and a global variable exists with the same name, the local variable well override the global variable. This can lead to hard-to-debug errors.

Wish 1: Local variables should not override global variables. This is like it is in virtually all other programming languages.

Wish 2: Alternatively, the linter should warn you about the problem so you can choose another name.

See https://www.b4x.com/android/forum/threads/local-variable-overrides-global.114668/#post-716224 for background information.
 

sorex

Expert
Licensed User
Longtime User
it does warn you when you use a global variable as sub parameter variable tho which hints that it's always global scope based.

on the other hand the linker doesn't know what you're trying to do so for some people it will be useless warnings.
 

OliverA

Expert
Licensed User
Longtime User
I don't think you will get either approved. Why? Because this feature is used in existing code and therefore would break that. I've seen code similar to
B4X:
Sub Proccess_Globals
Public aList as List
End Sub

....
Sub Some_Sub
Dim aList
aList.Initialize
aList.Add("Something")
End Sub
Where the code in Some_Sub is a shorter version of
B4X:
Sub Some_Sub
Dim bList
bList.Initialize
bList.Add("Something")
aList = bList
End Sub

By giving you request#1, this would break the first code example. By giving you request#2, the IDE would give you a warning about valid code (as it stands right now).
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
As I wrote in the other thread, the behavior is exactly as designed.
There are many valid cases where you need to use Dim to "reset" a global variable.
The correct way to create a new object is to call Dim and Initialize.

B4X:
Sub Globals
 Dim arr() As String
 Dim btn As button
End Sub

Sub SomeSub
 Dim arr(100) As String
 If btn.IsInitialized Then btn.RemoveFromParent
 Dim btn As Button
 btn.Initialize("")
 Activity.Add(btn, ..)
End Sub

It is possible to add a warning in the case of primitive types.
 

Martin Larsen

Active Member
Licensed User
Longtime User
There are many valid cases where you need to use Dim to "reset" a global variable.

I agree, but even if you use Private instead of Dim it overrides the global variable. I tried Private after discovering the problem, hoping it would solve it.

It is possible to add a warning in the case of primitive types.

That would be awesome! That would cover most cases.
 

Martin Larsen

Active Member
Licensed User
Longtime User
Similarly, this will include warnings that are not very useful in many cases, such as those mentioned by @LucaMs in the other thread, such as counters i,j, ...

Why? If you in fact have a global variable "i" and use it locally as a counter, you could have a real problem because the counter will overwrite the value in the global "i". In other words, the value of global "i" is inconsistent after the local sub. So a warning here would be in place.

Warning are just warnings: you can choose to ignore them by adding an ' ignore comment.
 

LucaMs

Expert
Licensed User
Longtime User
in any case, they're just programming styles.
I remember good times when "programming style" was based on "conventions".

Using prefixes for the type of objects would be a good practice:
strMyVar
intMyVar
etc.

It is also true that now it is sufficient to pass the mouse cursor over the name of the variable to know a lot about it (even if it has been declared locally or globally)
 

Martin Larsen

Active Member
Licensed User
Longtime User
I know about this pitfall now so it isn't a big deal for me anymore. But my main concern is this:

As we all talked about in the "B4A is now free" thread, we want more developers to come to B4X because it is indeed an awesome programming environment. But those developers come with some expectations. One is, and I dare state that as a 100% fact, they will expect locally declared variables to be local, not global. It is like that in all other languages I know about. And I do know quite a lot.

Such quirks could put B4A in a bad light and we don't want that.

There are a few other places where I think B4X falls a bit short, but I will make some more wishes soon.

I really think Erel should implement a compiler warning.
 
Top