We were not suggesting that the DBPro compiler should handle local variables differently,
we were simply pointing out that local variables declared in a function can
shield global variables by the same name and that currently there are no warnings
in place which can alert the user to this fact.
We agree that Local variables should take precedence over Global variables,
however the logic of this might not be evident to all user of DBPro
especially user who are using DBPro as their first programming language.
This is not necessarily a bug with the DBPro compiler as the code will still compile
but this is a lack of functionality in the DBPro compiler and or the existing DBPro IDE.
There are scenarios where a DBPro programmer could inadvertently break existing code
which was working simply be declaring a local variable by the same name as a global variable.
In these cases the IDE or compiler should prompt or warn the user about the naming conflict
and explain how DBPro handles scope. This would shorten the learning curve required
for someone who is new to programming and or the DBPro language. Below is an example of how
someone could inadvertently break a function which was previously working by declaring
a local variable by the same name as a Global variable.
rem Declaration of Global Variables
global x# as float
global y# as float
global z# as float
function UpdatePlayerPositon()
rem Updating Global Variables
x# = x# + 1.0
y# = y# + 1.0
z# = z# + 1.0
rem Additional Lines of Code
rem Inadvertent declaration of a local variable
rem by the same name as a global variable.
rem This declaration prevents the above code
rem from acting on the Global variable “x#” and
rem ends up breaking code which was previously working.
rem The DBPro IDE or compiler should recognize that the above code
rem is operating on a Global variable whose value
rem is now shielded by the declaration of a local variable and
rem provide the user with a warning message.
rem Without a warning message an inexperienced programmer may
rem not immediately recognize the naming conflict and
rem loose valuable programming time trying to debug a function
rem which is otherwise working correctly.
local x# as float
rem Some code working on the local var “x#”
endfunction
[img]
[/img]
WindowsXP SP3,Vista,Windows 7 SP1, DBpro v7.7RC7
Stab In The Dark Editor
The coffee is lovely dark and deep,and I have code to write before I sleep.