mit-cml/blockly-plugins

global variables dropdown should check for all blocks instead of only top blocks

Opened this issue · 3 comments

Hi,

i have created a block that creates an element and then returns its ID as an global variable.

However this works fine unless the block is inside of an top block see video: https://drive.google.com/file/d/1zgOjPKQKnw8HbemuX8CskOFd0HbikZAt/view?usp=sharing

Thanks

This is a bit of an unusual case. In most languages, global variables are created at the top-level of the program. This is the general practice because then you can clearly see where all the global variables are defined and initialized. Otherwise, they may be hidden in some deeply nested block. Moreover, it's possible that in some execution paths some global variable might be defined, but in others it won't be, which makes it even more difficult to reason about them and to understand and debug the program.

JavaScript is an outlier in how var works but in modern JavaScript programming it is considered bad practice to use var in non-top-level contexts.

Therefore, I would suggest that you switch to having your block introduce the variable as lexically scoped. Then the user can define their own top-level global variable and assign to it from within your block.

...i thought to make it more easier for the user to Use the ID without creating An extra variable outside of the block.

Sure, I get that, but I think that the risks that I outlined generally outweigh that advantage for anything but the very simplest of programs. Even top-level global variables tend to get overused and unwieldy in many block programs.

That said, your feature request is not completely unreasonable. Unfortunately, however, I'm afraid that it is unlikely to get implemented in the near future.